在AES加密和解密中使用相同的加密算法和密钥,以确保兼容性。
示例代码:
Java AES加密:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AesEncryptionJava {
private static final String ALGO = "AES";
private static final byte[] keyValue = "ThisIsASecretKey".getBytes();
public static String encrypt(String data) throws Exception {
SecretKeySpec key = new SecretKeySpec(keyValue, ALGO);
Cipher cipher = Cipher.getInstance(ALGO);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = cipher.doFinal(data.getBytes());
return Base64.encodeBase64String(encVal);
}
}
Node.js AES解密:
const crypto = require('crypto');
const ALGO = 'aes256';
const SECRET_KEY = 'ThisIsASecretKey';
const decrypt = (encryptedData) => {
const decipher = crypto.createDecipher(ALGO, SECRET_KEY);
let decrypted = decipher.update(encryptedData, 'base64', 'utf-8');
decrypted += decipher.final('utf-8');
return decrypted;
};
module.exports = {
decrypt,
};
在两种语言中使用相同的密钥和加密算法(例如上述示例中使用的AES-256算法和"ThisIsASecretKey"密钥),则加密的数据可以在Java中加密,然后在Node.js中解密并使用。
上一篇:ae数据库和缓存是啥
下一篇:AES加密 C# -> Java