javascript中的AES加密和java中的解密 [英] AES encryption in javascript and decrypting in java
问题描述
我已经阅读了所有关于这样做的主题,但没有找到任何有帮助的解决方案。
Javascript总是以不同的方式进行加密,我找不到为什么。
这是排除的java代码:
public static String encrypt(String data)throws异常{
byte [] keyValue = encryptionKey.getBytes();
Key key = new SecretKeySpec(keyValue,AES);
密码c = Cipher.getInstance(AES / ECB / PKCS5Padding);
c.init(Cipher.ENCRYPT_MODE,key);
byte [] encVal = c.doFinal(data.getBytes());
String encryptedValue = new BASE64Encoder()。encode(encVal);
return encryptedValue;
}
这是我倾向于使用的JavaScript代码,但提供了不同的加密CryptoJS):
var encrypted = CryptoJS.AES.encrypt(data,encryptionKey);
或其中之一(GibberishAES):
//默认为256位加密
var encrypted = GibberishAES.enc(data,encryptionKey);
//更改位加密
GibberishAES.size(128);
var encrypted = GibberishAES.enc(data,encryptionKey);
GibberishAES.size(192);
var encrypted = GibberishAES.enc(data,encryptionKey);
我无法在java中改变实现或者我们做安全的方式。有人有更多的经验可以告诉我我在这里做错什么?
你正在看加密算法,但您也关心块模式和填充,否则不会创建兼容的结果。根据 code.google.com ,CryptoJS的默认值为$ code> CBC 和 PKCS7
,而您的Java代码使用 ECB
和 PKCS5
。
你必须让它匹配。您可以设置CryptoJS使用 ECB
。关于填充更加棘手,因为CryptoJS不支持 PKCS5
,Java不会列出 PKCS7
,实际上,它列出的很少,所以可能实现依赖于AES提供商支持哪些填充算法,但至少$ code> NoPadding 是由Java和CryptoJS两者支持的。
I have an excisting webservice which encrypts and decrypts with AES, now i have to encrypt in the same way as java but in javascript. I have read all the topics about doing this in javascript but haven't found any helpfull solution. Javascript always encrypts in a different way and i can't find why.
This is the excisting java code :
public static String encrypt(String data) throws Exception {
byte[] keyValue = encryptionKey.getBytes();
Key key = new SecretKeySpec(keyValue, "AES");
Cipher c = Cipher.getInstance("AES/ECB/PKCS5Padding");
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
and this is the javascript code i tend to use but gives a different encryption (CryptoJS) :
var encrypted = CryptoJS.AES.encrypt(data, encryptionKey);
or either one of these (GibberishAES) :
// Defaults to 256 bit encryption
var encrypted = GibberishAES.enc(data, encryptionKey);
// change the bit encrytion
GibberishAES.size(128);
var encrypted = GibberishAES.enc(data, encryptionKey);
GibberishAES.size(192);
var encrypted = GibberishAES.enc(data, encryptionKey);
I can't change the implementation in java or the way we do security. Does someone have more experience in this who can tell me what i'm doing wrong here ?
You are looking at the encryption algorithm only but you have care for the block mode and padding too, otherwise you will not create compatible results. According to code.google.com CryptoJS has the defaults of CBC
and PKCS7
while your Java code uses ECB
and PKCS5
.
You have to bring that to match. You can setup CryptoJS to use ECB
. Regarding the padding it’s more tricky as CryptoJS does not list PKCS5
as supported, and Java does not list PKCS7
, in fact, it lists very little, so it might be implementation depended which padding algorithms the AES provider supports, but at least NoPadding
is supported by both, Java and CryptoJS.
这篇关于javascript中的AES加密和java中的解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!