Nodejs在java中加密AES 256 / decrypt

时间:2014-02-20 10:34:37

标签: java node.js encryption

我如何解密我在nodejs中加密的东西?
以下是我在 node.js

中的代码片段
function encrypt(cryptkey, iv, cleardata) {
    var encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv),
        encryptdata  = encipher.update(cleardata, 'utf8', 'binary');

    encryptdata += encipher.final('binary');
    var encode_encryptdata = new Buffer(encryptdata, 'binary').toString('base64');
    return encode_encryptdata;
}

以下是我尝试使用 Java 解密node.js输出:

public static String decrypt(String src) {
    String decrypted = "";
    try {
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, makeKey(), makeIv());
        decrypted = new String(cipher.doFinal(Base64.decode(src)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return decrypted;
}

static AlgorithmParameterSpec makeIv() {
    try {
        return new IvParameterSpec(ENCRYPTION_IV.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

static Key makeKey() {
    try {
        MessageDigest md = MessageDigest.getInstance("SHA-256");
        byte[] key = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
        return new SecretKeySpec(key, "AES");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}

提前感谢您的帮助

0 个答案:

没有答案