简体   繁体   中英

AES encryption in javascript and decrypting in java

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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