简体   繁体   中英

convert aes encryption in JAVA to node

I have a script in java

private static final String ALGO = "AES";

public static String encrypt(String Data,String secretKeyPhrase) throws Exception {

    Key key = new SecretKeySpec(secretKeyPhrase.getBytes(), ALGO);

    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = Base64.getEncoder().encodeToString(encVal);
    encryptedValue= URLEncoder.encode(encryptedValue, "UTF-8");
    return encryptedValue;
}


public static void main(String [] ar) throws Exception{
    String result2=AESUtil.encrypt("3483", "BDFHJLNPpnljhfdb");
    System.out.println(result2);
}

i need to replicate this in node i do this

var ciphertext = CryptoJS.AES.encrypt('3483', 'BDFHJLNPpnljhfdb').toString();

console.log(ciphertext);

in the java result show me

3483

sAllhJ7zLxBKr8hJ7tLf9w%3D%3D

but in node

3483

U2FsdGVkX19Z/mnjJy3hYDcXfWnH5eayYngWMtUT1lw=

what i am doing wrong?

thanks in advance.

hi i can encrypt with node this is the script

function encrypt(plaintext) {
  var data = plaintext;
  var iv = new Buffer(0);
  const key = 'BDFHJLNPpnljhfdb'
  var cipher = crypto.createCipheriv('aes-128-ecb',new Buffer(key),new Buffer(iv))
  var crypted = cipher.update(data,'utf-8','base64')
  crypted += cipher.final('base64')
  return crypted;
}

and the result is sAllhJ7zLxBKr8hJ7tLf9w==

this is another way with cripto-js

function encrypt(plaintext) {
  var keyUtf8 = CryptoJS.enc.Utf8.parse("BDFHJLNPpnljhfdb")
  var result = CryptoJS.AES.encrypt(plaintext, keyUtf8,{ mode: CryptoJS.mode.ECB,  
  keySize: 128 }).toString();
  return result;
}

function decrypt(ciphertext) {
  var keyUtf8 = CryptoJS.enc.Utf8.parse("BDFHJLNPpnljhfdb")
  var encrypted=ciphertext;
  encrypted = CryptoJS.AES.decrypt(encrypted, keyUtf8, { mode: CryptoJS.mode.ECB,  
  keySize: 128 });
  var result = CryptoJS.enc.Utf8.stringify(encrypted).toString(); 
  return result;
}

var ciphertext=encrypt("3483");
// result sAllhJ7zLxBKr8hJ7tLf9w==
var ciphertext=decrypt("sAllhJ7zLxBKr8hJ7tLf9w==");
// result 3483

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