我使用算法RSA / ECB / PKCS1Padding通过Java代码加密字符串现在同样需要使用node.js加密。我不知道如何使用算法RSA / ECB / PKCS1Padding通过node.js加密。 有什么建议? Java代码是:
public static String encrypt(String source, String publicKey)
throws Exception {
Key key = getPublicKey(publicKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] b = source.getBytes();
byte[] b1 = cipher.doFinal(b);
return new String(Base64.encodeBase64(b1), "UTF-8");
}
答案 0 :(得分:0)
node js代码:
const crypto = require('crypto')
const encryptWithPublicKey = function(toEncrypt) {
var publicKey = '-----BEGIN PUBLIC KEY-----****' //your public key
var buffer = Buffer.from(toEncrypt, 'utf8');
var encrypted = crypto.publicEncrypt({key:publicKey, padding : crypto.constants.RSA_PKCS1_PADDING}, buffer)
return encrypted.toString("base64");
}