我有以下Java代码。
String KEY = 'AAABBBCCC'
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom securerandom = SecureRandom.getInstance("SHA1PRNG");
securerandom.setSeed(KEY.getBytes());
kgen.init(256, securerandom);
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] byteContent = content.getBytes("utf-8");
byte[] cryptograph = cipher.doFinal(byteContent);
String enc1 = Base64.getEncoder().encodeToString(cryptograph);
return enc1;
我需要在JavaScript / Node.js中实现它,但是我只能像下面这样找出js的后半部分
'use strict';
const crypto = require('crypto');
const ALGORITHM = 'AES-256-ECB';
const key = 'AAABBBCCC'
// missing part in JS
function encrypt(plaintext, key) {
const cipher = crypto.createCipheriv(ALGORITHM, key, Buffer.alloc(0));
return cipher.update(plaintext, 'utf8', 'base64') + cipher.final('base64');
}
对于上一个Java部分(从 KeyGenerator 生成的 KEY 到 key ),我不知道如何在JavaScript中实现它,我也不知道JavaScript世界中是否存在像 KeyGenerator 这样的东西可以帮助我完成繁重的工作。