java加密方法等效于节点js crypto

时间:2017-07-14 13:49:19

标签: javascript java node.js encryption cryptography

我有java加密功能代码

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;
}

现在我正在尝试使用crypto模块在NodeJ中实现相同的功能 代码是: -

//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);
}

但两个函数返回的加密数据与我做错了不同?如果有人可以帮助!!提前致谢。

0 个答案:

没有答案