RSA私钥无法解密数据
RSA private key unable to decrypt data
我正在使用 JavaScript 和 Node.js 来处理一种消息传递项目。创建用户时,服务器使用 Node.js crypto
库生成 RSA 密钥对。私钥使用用户密码加密。在 webapp 上,当用户 A 向用户 B 发送消息时,数据使用用户 B 的 public 密钥加密。当用户 B 收到消息时,使用他们的私钥和密码对其进行解密。
我的问题是,虽然应用程序似乎加密了数据,但我无法解密数据。抛出的错误消息字面上是 "unable to decrypt data",这没有帮助。
用于生成密钥对的服务器端代码:
// Generate public/private keypairs
exports.generateKeyPair = (password) => {
return new Promise((resolve, reject) => {
crypto.generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: password
}
}, (err, publicKey, privateKey) => {
if (err) reject(err);
else resolve([publicKey, privateKey]);
});
})
}
解密和加密的客户端代码(Node加密库使用Browserify发送给客户端):
var crypto = require('crypto');
window.encrypt = function (data, publicKey) {
let buffer = Buffer.from(data);
let encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString('base64');
}
window.decrypt = function(data, privateKey, password) {
let buffer = Buffer.from(data, 'base64');
let decrypted = crypto.privateDecrypt({
key: privateKey,
passphrase: password
}, buffer);
return decrypted.toString('utf8');
}
与UI交互的客户端代码:
var sendMsg = function() {
let token = $('#token').val();
let toUser = $('#toUser').val();
let message = btoa($('#sendMessage').val());
fetch(`/keypairs/public/${toUser}?token=${token}`)
.then((res) => res.json())
.then((json) => {
if (json.code != 200) throw 'Error';
else return json.data.pubKey;
})
.then((pubKey) => encrypt(message, pubKey))
.then((encrypted) => $('#send-result').html(encrypted))
.catch((err) => alert(err));
};
var decryptMsg = function() {
let token = $('#token').val();
let encrypted = $('#decrypt-text').val();
let password = $('#decrypt-pass').val();
fetch(`/keypairs/private?token=${token}`)
.then((res) => res.json())
.then((json) => {
if (json.code != 200) throw 'Error';
else return json.data.privKey;
})
.then((privKey) => decrypt(encrypted, privKey, password))
.then((decrypted) => $('#decrypt-result').html(decrypted))
.catch((err) => (console.error(err), alert(err))); // <-- this line gives useless info
};
我将加密数据粘贴到一个字段中 #'decrypt-text
。我还传递了用户 B 的密码。在登录过程中检索令牌以将用户与数据库中的密钥相关联。正如我提到的,我得到的唯一错误是 "Unable to decrypt data"。有任何想法吗?也许我需要生成 pkcs1
类型的密钥?
我明白了。这个问题是当我创建 public 键时,类型是 spki
和 pkcs8
。由于某种原因,将两者都更改为 pkcs1
并重新生成密钥能够修复它。我还尝试将 type: 'pkcs8
传递给解密函数,但这没有用。
我正在使用 JavaScript 和 Node.js 来处理一种消息传递项目。创建用户时,服务器使用 Node.js crypto
库生成 RSA 密钥对。私钥使用用户密码加密。在 webapp 上,当用户 A 向用户 B 发送消息时,数据使用用户 B 的 public 密钥加密。当用户 B 收到消息时,使用他们的私钥和密码对其进行解密。
我的问题是,虽然应用程序似乎加密了数据,但我无法解密数据。抛出的错误消息字面上是 "unable to decrypt data",这没有帮助。
用于生成密钥对的服务器端代码:
// Generate public/private keypairs
exports.generateKeyPair = (password) => {
return new Promise((resolve, reject) => {
crypto.generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: password
}
}, (err, publicKey, privateKey) => {
if (err) reject(err);
else resolve([publicKey, privateKey]);
});
})
}
解密和加密的客户端代码(Node加密库使用Browserify发送给客户端):
var crypto = require('crypto');
window.encrypt = function (data, publicKey) {
let buffer = Buffer.from(data);
let encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString('base64');
}
window.decrypt = function(data, privateKey, password) {
let buffer = Buffer.from(data, 'base64');
let decrypted = crypto.privateDecrypt({
key: privateKey,
passphrase: password
}, buffer);
return decrypted.toString('utf8');
}
与UI交互的客户端代码:
var sendMsg = function() {
let token = $('#token').val();
let toUser = $('#toUser').val();
let message = btoa($('#sendMessage').val());
fetch(`/keypairs/public/${toUser}?token=${token}`)
.then((res) => res.json())
.then((json) => {
if (json.code != 200) throw 'Error';
else return json.data.pubKey;
})
.then((pubKey) => encrypt(message, pubKey))
.then((encrypted) => $('#send-result').html(encrypted))
.catch((err) => alert(err));
};
var decryptMsg = function() {
let token = $('#token').val();
let encrypted = $('#decrypt-text').val();
let password = $('#decrypt-pass').val();
fetch(`/keypairs/private?token=${token}`)
.then((res) => res.json())
.then((json) => {
if (json.code != 200) throw 'Error';
else return json.data.privKey;
})
.then((privKey) => decrypt(encrypted, privKey, password))
.then((decrypted) => $('#decrypt-result').html(decrypted))
.catch((err) => (console.error(err), alert(err))); // <-- this line gives useless info
};
我将加密数据粘贴到一个字段中 #'decrypt-text
。我还传递了用户 B 的密码。在登录过程中检索令牌以将用户与数据库中的密钥相关联。正如我提到的,我得到的唯一错误是 "Unable to decrypt data"。有任何想法吗?也许我需要生成 pkcs1
类型的密钥?
我明白了。这个问题是当我创建 public 键时,类型是 spki
和 pkcs8
。由于某种原因,将两者都更改为 pkcs1
并重新生成密钥能够修复它。我还尝试将 type: 'pkcs8
传递给解密函数,但这没有用。