rsa 不同的公钥加密码 可以用不同的私钥解密?
//npm install node-jsencryptJSEncrypt = require("node-jsencrypt");
// 创建RSA加密对象
var encrypt = new JSEncrypt();
// 公钥
pub_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyFMjUfalWn/hx4n+acT1D8lYUW/3V068a4swOBiQfSQbWNx8kX1K6vq1klB1QiT3ZealVH3eO22X5l3jCMuUO7sr0xPbEztclB84Dzq4NX4wlwgcjHFvhdjL0jWEyVmLPyDTMpTTguz5fAmJaT+E85/rWDdK4UjSRRPX+om3EzFObQc1OpqzxV1fv5XMgNhpz7AxBkgMEiIDKKSPi1Wc1Zw7P3zqI3YsKppz5XK3un8gitWBZ3gJ2xdCvRAibH2RCp9KBFnAk8U4qB/fYS+fHhp1fO//JFYLo2xS4FVAcQCPMUFkyjWq32CjNkExBVhWcq5uXmwrruZDDoayTmq7EQIDAQAB";
// 私钥
pri_key = 'MIIEogIBAAKCAQEAyFMjUfalWn/hx4n+acT1D8lYUW/3V068a4swOBiQfSQbWNx8kX1K6vq1klB1QiT3ZealVH3eO22X5l3jCMuUO7sr0xPbEztclB84Dzq4NX4wlwgcjHFvhdjL0jWEyVmLPyDTMpTTguz5fAmJaT+E85/rWDdK4UjSRRPX+om3EzFObQc1OpqzxV1fv5XMgNhpz7AxBkgMEiIDKKSPi1Wc1Zw7P3zqI3YsKppz5XK3un8gitWBZ3gJ2xdCvRAibH2RCp9KBFnAk8U4qB/fYS+fHhp1fO//JFYLo2xS4FVAcQCPMUFkyjWq32CjNkExBVhWcq5uXmwrruZDDoayTmq7EQIDAQABAoIBAATpW5YrX3OAFizlGmqadyMLczxlQTcP9E5eC8Zxp71Bu4qVYVfUhBfjIhIzzHmHhtSl7+DJ8z8Zbp4p0s1ePG9YeqMYELOAgu0TdOA+ttHXmwu2RF5B0ROtiesd/82grJQq0wghnlaj8Rv+7zJi6c4VWi5VW2MkUholJJL9JGghLrjMcdflqM36XN6e9kESVOJrN8IPbE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sa57kB/+zd6u910EQtYd4EuKLco28r8DxsACpn00D1V1vZryAavDm5UP2dnDJjPg9CZytH4juyL3J4EbRu4HE2P9VBqRUmH/0gKtQDh9DdCaizUUFP3bwAIzFyob5T2cvRjQvnYYRcQFTa5aNlcvsCgYEAzIU4tjsDAC5CTgQDBaxiQR2IGZUps81eICxtdSly4/ROBHW9jLIeMyI0TCGfbI8l9frIBF2pEQriEH63iCPIXQgCfvqwAPqHbCQgu3MrIlR15TZG2Tl3yelKSJIzmMyyNwueHJBkK19SQPq7mE43Qfp5YuD5MKZ2M+7AmDiclX8CgYBlr34NeOB1C/pBE85W0iQZopwSKsiSL7ZudRZOIiEtvvKE1OfY8z7L69MormA1TpqIt3MvVM1e4q1yLKjaRlhRjqw4+iiyAYC2SZoh25Am2DclaWXDuKK4wfGQ7kOwt+dX3VzamaE2SB+Fv7mn26B3TOLojq/8vJuga7cRwM1m+wKBgQCaFXh/moANP8zGeDFKsfAofLZQB9guVXgnpzEkBwE/Co1IwOf4uy0Tex6JiP9SS5re8nyFBYddhqSApudhEwxeQq2WuwmlAxrJ94INl5wUtitIcBb6i1hbb35+pF9pLDbMT91kScieAKe4uu5gtfTdqXaQb0xuQctmHudNB8G4wwKBgBkTAObd2dslpUaRt4JXHqpOjfrCy+WV0SjB1AK+azU8yY0TQVxnA7hU00HZSalZiB59Xfh3hjls/zSA8Fxig3L1ngjzjvdAASsUmuAOSNlR7BgyAppORJUZHG+kVuatv9Z/hF7UgtK5K/6xVRNJ9pFf0suKgHZ0XlGzlQ17hQL9';
data='神奇';
//使用公钥
encrypt.setPublicKey(pub_key);
//使用私钥
encrypt.setPrivateKey(pri_key);
res=encrypt.encrypt(data);
console.log('加密结果');
console.log(res);
console.log('解密');
console.log(encrypt.decrypt(res));
//以上是测试代码
公私钥是对应的 本帖最后由 kittylang 于 2024-6-27 22:38 编辑
什么意思?你不妨说得明白些。
如果你是指,用公玥A加密,可以用私玥B解密?答案是:如果AB是一对的,那确实,因为这就非对称加密,用A加密,必须A解密的那叫对称加密,例如DES
如果你是指,用公玥A加密,可以用私玥B解密,可以用C解密吗,那当然不行,公私钥是对应的。要是随便一个私玥都能解密那还有啥意义 私钥已经包含公钥,同个变量导入就覆盖了 密钥对啊
公钥跟私钥是一对一的 kittylang 发表于 2024-6-27 22:33
什么意思?你不妨说得明白些。
如果你是指,用公玥A加密,可以用私玥B解密?答案是:如果AB是一对的 ...
加了公钥私钥 的 注释pub_key 和 pri_key的是第一组 没加注释的是第二组 用第一组私钥和 第二组的公钥 放在一起运行 能正常加解密, gunxsword 发表于 2024-6-27 22:20
公私钥是对应的
是的,当想验证 不同组的 公钥 和 私钥是不能混着用的,出现了这个情况, Vvvvvoid 发表于 2024-6-28 06:17
密钥对啊
公钥跟私钥是一对一的
是的,就是想验证 这个理论,不是同一组的 公钥 和 私钥是不能混着用的,但测试时出现了这个情况,没找到问题所在,所以发出来请教
懂你说的了。这不是 rsa 的问题,而是 `node-jsencrypt` 这个库的问题,这玩意最后一版都 7 年前了。它并没有完全实现 rsa
出现这个问题的原因是你写了如下代码
```javascript
//使用公钥
encrypt.setPublicKey(pub_key);
//使用私钥
encrypt.setPrivateKey(pub_key);
```
其结果是将 pub_key 和 pub_key 当成了一对。即使它们并不是通过 openssl 生成的一对。
使用如下代码
```javascript
const JSEncrypt = require('node-jsencrypt');
// 创建RSA加密对象
const encrypt = new JSEncrypt();
// 公钥(你给的第一个公钥)
const pub_key =
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyFMjUfalWn/hx4n+acT1D8lYUW/3V068a4swOBiQfSQbWNx8kX1K6vq1klB1QiT3ZealVH3eO22X5l3jCMuUO7sr0xPbEztclB84Dzq4NX4wlwgcjHFvhdjL0jWEyVmLPyDTMpTTguz5fAmJaT+E85/rWDdK4UjSRRPX+om3EzFObQc1OpqzxV1fv5XMgNhpz7AxBkgMEiIDKKSPi1Wc1Zw7P3zqI3YsKppz5XK3un8gitWBZ3gJ2xdCvRAibH2RCp9KBFnAk8U4qB/fYS+fHhp1fO//JFYLo2xS4FVAcQCPMUFkyjWq32CjNkExBVhWcq5uXmwrruZDDoayTmq7EQIDAQAB';
const data = '神奇';
//使用公钥
encrypt.setPublicKey(pub_key);
const res = encrypt.encrypt(data);
console.log('加密结果');
console.log(res);
```
得到一个加密后的 res
然后再
```javascript
const JSEncrypt = require('node-jsencrypt');
// 创建RSA加密对象
const encrypt = new JSEncrypt();
// 私钥(将你的第一个或者第二个私钥)
const pri_key = 'xxxxxxxxxxxxxx';
//使用公钥
encrypt.setPublicKey(pub_key);
const res = encrypt.decrypt(`之前的结果`);
console.log('解密结果:');
console.log(res);
```
你就能发现,密钥真的需要配对才行(狗头)
最后,并不建议使用 `node-jsencrypt` 这个老掉牙的
在 node 中可以直接使用以下代码(新版本可能需要`node --security-revert=CVE-2023-46809 index.js` 加上安全忽略标志)
或者第三方库直接用 jsencrypt(node-jsencrypt 就是用的这个的老版本)。
```javascript
const { publicEncrypt, constants, privateDecrypt } = require('crypto');
function rsaEncrypt(publicKey, text) {
return publicEncrypt(
{
key: `-----BEGIN PUBLIC KEY-----\n${publicKey}\n-----END PUBLIC KEY-----`,
padding: constants.RSA_PKCS1_PADDING,
},
Buffer.from(text, 'utf8')
).toString('base64');
}
function rsaDecrypt(privateKey, text) {
return privateDecrypt(
{
key: `-----BEGIN RSA PRIVATE KEY-----\n${privateKey}\n-----END RSA PRIVATE KEY-----`,
padding: constants.RSA_PKCS1_PADDING,
},
Buffer.from(text, 'base64')
).toString('utf8');
}
// 公钥
const pub_key =
'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyFMjUfalWn/hx4n+acT1D8lYUW/3V068a4swOBiQfSQbWNx8kX1K6vq1klB1QiT3ZealVH3eO22X5l3jCMuUO7sr0xPbEztclB84Dzq4NX4wlwgcjHFvhdjL0jWEyVmLPyDTMpTTguz5fAmJaT+E85/rWDdK4UjSRRPX+om3EzFObQc1OpqzxV1fv5XMgNh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yhsIJ41go/KDMEvfp7bNejAKPkOIZK/X6oL3hrtlAkvnbpBGRovck8VQMuBX/IleX4mWj/AwwW4znWcEqGn9IPqE0gLyIE2ZanEa3qP3A+FdsVm+2hfSggnChzIRmulg1ajBg4FMdDnKrG+56DBelBxaDsCgYB4t2duL63J3g1hThheveLg7iB1mzY0mfz0R8Aib2BNgV1LGhjplJvjAXbOC7yn8Aqx748BFT3Ud9d549XHSpnL3clFhcunW2hR0iad/fW5V1r9RNpwjrhsszITiyq6jmy4cT/Y4+HeMEXCM9gBDlM4kNoy04LCc3kZHCwFV6d6FwKBgC7ZAuJe3/jZWFWEsWx1bYBevwK3bCdN2TCL9OgZVeBYXIjZC5uroUJrOeL2aDYCUAlYzSg18GorOa5lgyVh6nB7eIcG+39SulPM4GJuigcTYdx8ThgdZPxcq62vf2i+yrEMwazlqRDHA2jF+zJRuZo5an367lOQNeBFeE2BdoHzAoGACkqOsBKDMvX6sObd0j/Hwx2AVePfJqbIEQNH/kncKKdI1i1hjDTgZ4VV5q+PzpaFOc0LkZCpXhopqHQ7O7qMmE79fPdllCam1E1kRjN0M9YX5/WeaxKFh9wMG1VOfQg3MoLPbM8afI5hdvqvc70nMH/tyu4hpKyFyeFEmOLoSoY=';
const data = '神奇';
const res = rsaEncrypt(pub_key, data);
console.log('加密结果:');
console.log(res);
console.log('解密结果:');
console.log(rsaDecrypt(pri_key, res));
``` 也许可以尝试换成这个顺序
data='神奇';
//使用公钥
encrypt.setPublicKey(pub_key);
res=encrypt.encrypt(data);
console.log('加密结果');
console.log(res);
//使用私钥
encrypt.setPrivateKey(pri_key);
console.log('解密');
console.log(encrypt.decrypt(res));
页:
[1]
2