[英]How to encrypt through node.js of encrypted string using algorithm RSA/ECB/PKCS1Padding
I have encrypted string using algorithm RSA/ECB/PKCS1Padding through Java code now the same need to be encrypted using node.js.我已经通过 Java 代码使用算法 RSA/ECB/PKCS1Padding 加密了字符串,现在同样需要使用 node.js 进行加密。 I don't know how to encrypt through node.js using algorithm RSA/ECB/PKCS1Padding .
我不知道如何使用算法 RSA/ECB/PKCS1Padding 通过 node.js 进行加密。 Any suggestions?
有什么建议? the Java code is:
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");
}
node js code using the cryto library:使用cryto库的节点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"); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.