gpt4 book ai didi

node.js - 使用带 Node 的 ursa 加密和解密字符串会引发解码错误

转载 作者:搜寻专家 更新时间:2023-11-01 00:32:57 25 4
gpt4 key购买 nike

我正在尝试使用 ursa 库在我当前的 nodejs 项目中使用 RSA 制作加密和解密逻辑。我真的在任何地方都找不到这方面的帮助,所以我希望有人能提供帮助。

每当我运行这段代码时,我都会收到以下与填充相关的解码错误(仅当DECRYPTING时。Encrypting does not break 虽然我不确定它是否正确):

“错误:0407A079:rsa例程:RSA_padding_check_PKCS1_OAEP:oaep解码错误”

我尝试将填充更改为 ursa.RSA_PKCS1_PADDING 而不是使用默认的 ursa.RSA_PKCS1_OAEP_PADDING 但 id 没有帮助。我尝试用一​​个填充加密并用另一个填充解密,但它没有用。我相信我为缓冲区和加密/解密方法尝试了所有编码组合(二进制、utf8 和 base64),但效果不佳。

这是我要运行的全部测试代码:

var ursa = require("ursa");

var privateKey = ursa.generatePrivateKey(2048, 65537);

//var pub = privateKey.toPublicPem('base64');
//var priv = privateKey.toPrivatePem('base64');

var clearText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

var encrypted = encrypt(clearText, 256);
var decrypted = decrypt(encrypted, 256);


function encrypt(text, keySizeBytes){
var buffer = new Buffer(text);
var maxBufferSize = keySizeBytes - 42;
var bytesCopied = 0;
var finalString = "";

//loops through all data buffer encrypting piece by piece
while(bytesCopied < buffer.length){
//calculates next maximun length for temporary buffer and creates it
var amountToCopy = Math.min(maxBufferSize, buffer.length - bytesCopied);
var tempBuffer = new Buffer(amountToCopy);

//copies next chunk of data to the temporary buffer
var nextEnd = bytesCopied + amountToCopy;
buffer.copy(tempBuffer, 0, bytesCopied, nextEnd);

//encrypts current chunk and concatenates with encrypted parts
finalString += privateKey.encrypt(tempBuffer);

bytesCopied += amountToCopy;
}

return finalString;
}

function decrypt(text, keySizeBytes){
var buffer = new Buffer(text);
var maxBufferSize = keySizeBytes - 1;
var bytesCopied = 0;
var finalString = "";

//loops through all data buffer encrypting piece by piece
while(bytesCopied < buffer.length){
//calculates next maximun length for temporary buffer and creates it
var amountToCopy = Math.min(maxBufferSize, buffer.length - bytesCopied);
var tempBuffer = new Buffer(amountToCopy);

//copies next chunk of data to the temporary buffer
var nextEnd = bytesCopied + amountToCopy;
buffer.copy(tempBuffer, 0, bytesCopied, nextEnd);

//drecrypts current chunk and concatenates with decrypted parts
finalString += privateKey.decrypt(tempBuffer);

bytesCopied += amountToCopy;
}

return finalString;
}

我可能在这里做了一些非常愚蠢的事情,但老实说,此时我找不到问题所在。谢谢 =)

最佳答案

其中一个问题是,如果您未指定编码,ursa 加密和解密方法会返回一个 Buffer,因此我的串联会产生奇怪的结果。此外,我意识到任何使用 N 字节 key 加密的字符串都将具有 N 字节。考虑到这些,我改进了代码,这是我对任何需要使用 ursa 加密和解密任何大小的字符串的人的最终建议。

var ursa = require("ursa");

var clearText = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
var bigText = "";
for(var i = 0; i < 100; i++){
bigText += i +" " + clearText + "\n";
}

var keySizeBits = 1024;
var keyPair = ursa.generatePrivateKey(keySizeBits, 65537);

var encrypted = encrypt(bigText, keySizeBits/8);
console.log(encrypted);

var decrypted = decrypt(encrypted, keySizeBits/8);
console.log(decrypted);

function encrypt(clearText, keySizeBytes){
var buffer = new Buffer(clearText);
var maxBufferSize = keySizeBytes - 42; //according to ursa documentation
var bytesDecrypted = 0;
var encryptedBuffersList = [];

//loops through all data buffer encrypting piece by piece
while(bytesDecrypted < buffer.length){
//calculates next maximun length for temporary buffer and creates it
var amountToCopy = Math.min(maxBufferSize, buffer.length - bytesDecrypted);
var tempBuffer = new Buffer(amountToCopy);

//copies next chunk of data to the temporary buffer
buffer.copy(tempBuffer, 0, bytesDecrypted, bytesDecrypted + amountToCopy);

//encrypts and stores current chunk
var encryptedBuffer = keyPair.encrypt(tempBuffer);
encryptedBuffersList.push(encryptedBuffer);

bytesDecrypted += amountToCopy;
}

//concatenates all encrypted buffers and returns the corresponding String
return Buffer.concat(encryptedBuffersList).toString('base64');
}

function decrypt(encryptedString, keySizeBytes){

var encryptedBuffer = new Buffer(encryptedString, 'base64');
var decryptedBuffers = [];

//if the clear text was encrypted with a key of size N, the encrypted
//result is a string formed by the concatenation of strings of N bytes long,
//so we can find out how many substrings there are by diving the final result
//size per N
var totalBuffers = encryptedBuffer.length / keySizeBytes;

//decrypts each buffer and stores result buffer in an array
for(var i = 0 ; i < totalBuffers; i++){
//copies next buffer chunk to be decrypted in a temp buffer
var tempBuffer = new Buffer(keySizeBytes);
encryptedBuffer.copy(tempBuffer, 0, i*keySizeBytes, (i+1)*keySizeBytes);
//decrypts and stores current chunk
var decryptedBuffer = keyPair.decrypt(tempBuffer);
decryptedBuffers.push(decryptedBuffer);
}

//concatenates all decrypted buffers and returns the corresponding String
return Buffer.concat(decryptedBuffers).toString();
}

还是谢谢!如果有人有任何改进的想法,我们非常欢迎。

谢谢!

关于node.js - 使用带 Node 的 ursa 加密和解密字符串会引发解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21951304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com