I have searched quite a bit and have yet to gain any clarity on my particular issue. I have a process using node.js that encrypts some data elements and stores the hex string output. So as to not go into great detail on that particular process, the results are the same as the following online tool here.
我已经搜索了很多,并且还没有明确我的具体问题。我有一个使用node.js的进程,它加密一些数据元素并存储十六进制字符串输出。为了不详细介绍该特定过程,结果与此处的以下在线工具相同。
If you were to enter the following into that tool:
如果您要在该工具中输入以下内容:
Enter text to be Encrypted: "666326911"
Select Mode: "CBC"
Key Size in Bits: "256"
Enter IV: (Leave blank)
Enter Secret Key: "c88ba867994f440963f55b727cdd3cb7"
Output Text Format: "Hex"
The Encryption output would give you "C08F3DD7F5F7ACD0FC3710ADDFBF596C". This result matches my process.
加密输出将为您提供“C08F3DD7F5F7ACD0FC3710ADDFBF596C”。此结果与我的流程相符。
I now have a need to use java to encrypt data the same way. My code gives me completely different results, and I can't seem to pinpoint where my error occurs. Here is the java code I am using:
我现在需要使用java以相同的方式加密数据。我的代码给了我完全不同的结果,我似乎无法确定我的错误发生在哪里。这是我正在使用的java代码:
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
Key key = new SecretKeySpec(Hex.decodeHex("c88ba867994f440963f55b727cdd3cb7"), "AES");
IvParameterSpec iv = new IvParameterSpec(new byte[16]);
cipher.init(Cipher.ENCRYPT_MODE,key,iv);
byte[] testString = "666326911".getBytes("UTF-8");
byte[] encrypted = cipher.doFinal(testString);
System.out.println("Encrypt Hex: "+Hex.encodeHexString(encrypted));
This code gives me a result of "DA6711D88635E82B68673D9C077B070F". Can anyone tell me where my obvious mistake or incorrect assumption is?
此代码给出了“DA6711D88635E82B68673D9C077B070F”的结果。谁能告诉我我的明显错误或错误的假设在哪里?
Thanks, Scott
EDIT:
Changing the code to:
将代码更改为:
SecretKeySpec key = new SecretKeySpec("c88ba867994f440963f55b727cdd3cb7".getBytes("UTF-8"), "AES");
results in an "InvalidKeyException: Illegal key size"
导致“InvalidKeyException:Illegal key size”
1 个解决方案
#1
2
The problem seems to be with the SecretKeyspec.
问题似乎与SecretKeyspec有关。
On the online tool you are using it as a plain String.
在在线工具上,您将其用作纯字符串。
Here you are treating it as a hex number, and decode it first.
在这里,您将其视为十六进制数,并首先对其进行解码。
If you try
如果你试试
Key key = new SecretKeySpec("c88ba867994f440963f55b727cdd3cb7".getBytes(), "AES");
then you should get the same results.
那么你应该得到相同的结果。
This is a copy-paste of the code I'm using:
这是我正在使用的代码的复制粘贴:
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
Key key = new SecretKeySpec("c88ba867994f440963f55b727cdd3cb7".getBytes("UTF-8"), "AES");
IvParameterSpec iv = new IvParameterSpec(new byte[16]);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] testString = "666326911".getBytes("UTF-8");
byte[] encrypted = cipher.doFinal(testString);
System.out.println("Encrypt Hex: " + Hex.encodeHexString(encrypted));
} catch (Exception e) {
System.err.println("Uh-ohh...");
e.printStackTrace();
}
And the output is:
输出是:
Encrypt Hex: c08f3dd7f5f7acd0fc3710addfbf596c
Process finished with exit code 0
#1
2
The problem seems to be with the SecretKeyspec.
问题似乎与SecretKeyspec有关。
On the online tool you are using it as a plain String.
在在线工具上,您将其用作纯字符串。
Here you are treating it as a hex number, and decode it first.
在这里,您将其视为十六进制数,并首先对其进行解码。
If you try
如果你试试
Key key = new SecretKeySpec("c88ba867994f440963f55b727cdd3cb7".getBytes(), "AES");
then you should get the same results.
那么你应该得到相同的结果。
This is a copy-paste of the code I'm using:
这是我正在使用的代码的复制粘贴:
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
Key key = new SecretKeySpec("c88ba867994f440963f55b727cdd3cb7".getBytes("UTF-8"), "AES");
IvParameterSpec iv = new IvParameterSpec(new byte[16]);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] testString = "666326911".getBytes("UTF-8");
byte[] encrypted = cipher.doFinal(testString);
System.out.println("Encrypt Hex: " + Hex.encodeHexString(encrypted));
} catch (Exception e) {
System.err.println("Uh-ohh...");
e.printStackTrace();
}
And the output is:
输出是:
Encrypt Hex: c08f3dd7f5f7acd0fc3710addfbf596c
Process finished with exit code 0