admin管理员组文章数量:1389903
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 ?
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 ?
Share asked Dec 2, 2013 at 8:27 Cenz0Cenz0 231 silver badge4 bronze badges3 Answers
Reset to default 3You are looking at the encryption algorithm only but you have care for the block mode and padding too, otherwise you will not create patible results. According to code.google. 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.
Here is working solution to implement "AES/ECB/PKCS5Padding" but in JavaScript (Node.js) using ezcrypto
module
const Crypto = require('ezcrypto').Crypto;
let whatToEncryptAsUtf8 = Crypto.charenc.UTF8.stringToBytes(whatToEncrypt);
let keyAsUtf8 = Crypto.charenc.UTF8.stringToBytes(key);
let encrypted = Crypto.AES.encrypt(whatToEncryptAsUtf8, keyAsUtf8, {
mode: new Crypto.mode.ECB(Crypto.pad.pkcs7)
});
To decrypt hash generated using JAVA AES/ECB/PKCS5Padding, you need to create a decrypt cipher also with padding in js(nodejs).
const crypto = require('crypto');
function decrypt(data){
var decipher = crypto.createCipheriv("aes-128-ecb", 'SAME_KEY_AS_JAVA', '');
var dec = decipher.update(data,'base64','utf8');
dec += decipher.final('utf8');
return dec
}
本文标签: AES encryption in javascript and decrypting in javaStack Overflow
版权声明:本文标题:AES encryption in javascript and decrypting in java - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744652966a2617796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论