首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我正在尝试将Java加密解密转换为NodeJs。这就是我迄今所尝试过的

我正在尝试将Java加密解密转换为NodeJs。这就是我迄今所尝试过的
EN

Stack Overflow用户
提问于 2019-11-13 06:48:13
回答 1查看 1.7K关注 0票数 1

加密:

代码语言:javascript
运行
复制
private static String secretKey = "boooooooooom!!!!";
private static String salt = "ssshhhhhhhhhhh!!!!";

public static String encrypt(String strToEncrypt, String secret) 
{
    try 
    {
        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    } 
    catch (Exception e) 
    {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

解密:

代码语言:javascript
运行
复制
private static String secretKey = "boooooooooom!!!!";
private static String salt = "ssshhhhhhhhhhh!!!!";

public static String decrypt(String strToDecrypt, String secret) {
    try 
    {
        byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        IvParameterSpec ivspec = new IvParameterSpec(iv);

        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
        KeySpec spec = new PBEKeySpec(secretKey.toCharArray(), salt.getBytes(), 65536, 256);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    } 
    catch (Exception e) {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}

关于这件事我搜了很多遍。我有这些关于堆栈溢出的问题

Difference in key lengths between crypto.pbkdf2 (node.js) and PBEKeySpecAES/CBC/PKCS5PADDING IV - Decryption in NodeJs (Encrypted in Java) --我试过这两种方法,但都没有解决我的问题。

到目前为止,我试过的是,

代码语言:javascript
运行
复制
function encrypt(plainText, secretKey,randomeString) {

    const key = crypto.pbkdf2Sync(secretKey, salt, 65536, 16, digest);
    const iv = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    let encrypted = cipher.update(plainText, 'utf8', 'base64')
    encrypted += cipher.final('base64');      
    return encrypted;
};

function decrypt(strToDecrypt, secretKey, randomeString) {

    const key = crypto.pbkdf2Sync(secretKey, salt, 65536, 16, digest);

    const iv = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);

    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    let decrypted = decipher.update(strToDecrypt, 'base64');
    decrypted += decipher.final();
    return decrypted;
}
EN

Stack Overflow用户

回答已采纳

发布于 2019-11-13 08:31:59

这个Node.js代码应该产生与您的Java代码相同的结果。我还将secretKey变量作为加密/解密函数的参数。

代码语言:javascript
运行
复制
const crypto = require('crypto');

const algorithm = 'aes-256-cbc';
const salt = "ssshhhhhhhhhhh!!!!";
const digest = 'sha256';

function encrypt(plainText, secretKey) {

    const key = crypto.pbkdf2Sync(secretKey, salt, 65536, 32, digest);
    const iv = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);

    const cipher = crypto.createCipheriv(algorithm, key, iv);
    let encrypted = cipher.update(plainText, 'utf8', 'base64')
    encrypted += cipher.final('base64');
    return encrypted;
};

function decrypt(strToDecrypt, secretKey) {

    const key = crypto.pbkdf2Sync(secretKey, salt, 65536, 32, digest);
    const iv = Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]);

    const decipher = crypto.createDecipheriv(algorithm, key, iv);
    let decrypted = decipher.update(strToDecrypt, 'base64');
    decrypted += decipher.final();
    return decrypted;
}

const key = "boooooooooom!!!!";
const cipherText = encrypt("test", key);
console.log("Ciphertext:", cipherText);
const plainText = decrypt(cipherText, key);
console.log("Plaintext:", plainText);
票数 5
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58831677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档