简体   繁体   English

AES / CBC / PKCS5Padding加密在java解密ruby中

[英]AES/CBC/PKCS5Padding encrypt in java decrypt in ruby

I am trying to encrypt data in java and decrypt data in ruby. 我试图加密java中的数据并解密ruby中的数据。

I found almost same asks, but my case is little bit different. 我发现几乎相同的问题,但我的情况有点不同。

My code is ... Encrypt in java 我的代码是...在java中加密

import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Hex;

class Crypt {
    public static void main(String[] args) throws Exception {
        Map<String, String> node = new HashMap<String, String>();
        node.put("timestamp", "1377499097199");
        JSONObject jsonObject = JSONObject.fromObject(node);
        String json = jsonObject.toString();
        System.out.println(json);

        //key
        String skeyString = "97128424897797a166913557a6f4cc8e";
        byte[] skey = Hex.decodeHex(skeyString.toCharArray());
        System.out.println("key : " + skeyString);

        //iv
        String ivString = "84e8c3ea8859a0e293941d1cb00a39c3";
        byte[] iv = Hex.decodeHex(ivString.toCharArray());
        System.out.println("iv : " + ivString);

        //encrypt
        SecretKeySpec skeySpec1 = new SecretKeySpec(skey, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec1, new IvParameterSpec(iv));
        byte[] encrypted = cipher.doFinal(json.getBytes());
        String encryptedString = Hex.encodeHexString(encrypted);
        System.out.println("=============>");
        System.out.println("encrypted string: " + encryptedString);
        }
}

Result is 结果是

{"timestamp":"1377499097199"}
key : 97128424897797a166913557a6f4cc8e
iv : 84e8c3ea8859a0e293941d1cb00a39c3
=============>
encrypted string: 395f6c0e8ad27f57c4a5a8975aa633e5b26f288d37ce18c6971779951f3b3527

and I hope to decrypt (encrypted string) in Ruby 我希望在Ruby中解密(加密字符串)

Ruby code is ...(got error) Ruby代码是......(得到错误)

require 'openssl'

key = "97128424897797a166913557a6f4cc8e"
iv = "84e8c3ea8859a0e293941d1cb00a39c3"
encrypted_string = "395f6c0e8ad27f57c4a5a8975aa633e5b26f288d37ce18c6971779951f3b3527"

de_cipher = OpenSSL::Cipher::Cipher.new("AES-256-CBC")
de_cipher.decrypt
de_cipher.key = key
de_cipher.iv = iv

de_cipher.update(encrypted_string) << de_cipher.final

I expected to get 我期望得到

{"timestamp":"1377499097199"}

But it returns error 但它返回错误

`final': bad decrypt (OpenSSL::Cipher::CipherError)

I think the problem is cipher.padding and type of key/iv. 我认为问题是cipher.padding和key / iv的类型。 But I don't know exactly how to complete ruby code. 但我不知道如何完成ruby代码。

Please let me know How to complete this code. 请告诉我如何完成此代码。

Thank you. 谢谢。

There's two problems with your Ruby code. 您的Ruby代码存在两个问题。

First, you're using AES 256 when you should be using AES 128. Java uses AES 128 or 256 based on the size of the key you use, and you're using a 128 bit key. 首先,当您使用AES 128时,您使用的是AES 256. Java根据您使用的密钥大小使用AES 128或256,并且您使用的是128位密钥。

Second, you need to Hex decode your key , iv , and encrypted_string values in Ruby. 其次,你需要在Ruby中解码你的keyivencrypted_string值。 OpenSSL Cipher is expecting binary, not hex strings. OpenSSL Cipher期待二进制,而不是十六进制字符串。

require 'openssl';

key = "97128424897797a166913557a6f4cc8e";
iv = "84e8c3ea8859a0e293941d1cb00a39c3";
encrypted_string = "395f6c0e8ad27f57c4a5a8975aa633e5b26f288d37ce18c6971779951f3b3527";

de_cipher = OpenSSL::Cipher::Cipher.new("AES-128-CBC");
de_cipher.decrypt;
de_cipher.key = [key].pack('H*');
de_cipher.iv = [iv].pack('H*');

puts de_cipher.update([encrypted_string].pack('H*')) << de_cipher.final;

Output: 输出:

{"timestamp":"1377499097199"}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM