java Encrypt method equivalent in node js crypto [duplicate]

假如想象 提交于 2019-12-22 01:27:43

问题


I have java encryption function code as

public String encrypt(String Data, String keySet) throws Exception {
    byte[] keyByte = keySet.getBytes();
    Key key = generateKey(keyByte);

    Cipher c = Cipher.getInstance("AES");

    c.init(Cipher.ENCRYPT_MODE, key); //2
    byte[] encVal = c.doFinal(Data.getBytes()); //1
    byte[] encryptedByteValue = new Base64().encode(encVal); //3
    String encryptedValue = new String(encryptedByteValue); //4
    return encryptedValue;
}

private static Key generateKey(byte[] keyByte) throws Exception {
    Key key = new SecretKeySpec(keyByte, "AES");
    return key;
}

Now I am trying to implement the same in NodeJs using crypto module code is: -

//buf is string data that i want to encrypt

function makeEncrypt(buf, callback) {
    var enckey = "encryptionkey";
    var cipher = crypto.createCipher(algorithm, enckey)
    var crypted = cipher.update(buf, 'utf8', 'base64')
    crypted += cipher.final('base64');

    console.log("encrypted data is : " + crypted.toString());
    callback(null, crypted);
}

But the encrypted data returned by both functions is different what I am doing wrong? if anyone can help!! thanks in advance.

来源:https://stackoverflow.com/questions/45104691/java-encrypt-method-equivalent-in-node-js-crypto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!