dongmo8943 2015-03-12 14:50
浏览 87

使用hybrid-rsa-stream在Nodej中进行加密,并使用OpenSSL在C#中进行解密

Hy, I'm trying to encrypt a file in node and decrypt it in C#. To encrypt the the file I use Hybrid-rsa-stream that internally uses rsa-stream, And then I use ManagedOpenSSL to decrypt the file. this is my Node code

var enc = rsa.encrypt(pubkey);
var out = fs.createWriteStream(path.join(localPath, "crypt-" + fileName), {flags: 'w'});
var input = fs.createReadStream(path.join(localPath, fileName));
input.pipe(enc).pipe(out);

Since I'm using a 1024 bit key, the modulus length should be 128 bytes, I tried setting different modulus length as a param to rsa.encrypt but nothing changed decypting it. This is my c# code

 CryptoKey cryptoKey = CryptoKey.FromPrivateKey(_privateKey, null);
 RSA _rsa = cryptoKey.GetRSA();
 BinaryReader br = new BinaryReader(File.Open(toDecrypt, FileMode.Open));
 List<byte> result = new List<byte>();
 while (br.BaseStream.Position != br.BaseStream.Length)
 {
     result.AddRange(_rsa.PrivateDecrypt(br.ReadBytes(_rsa.Size), RSA.Padding.None));
 }

But when I try to decrypt the file I get an error

error:04065084:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data too large for modulus

I don't think it has nothing to deal with the padding setting, I tried changing it but nothing changed.

The private key has a nelinw character at the end( ), I tried removing it but nothing changed,

EDIT: I noticed that cripting blocks of rsa.size - 1 dosn't throw any error, and the file gets written, but It's damaged and I can't open it

EDIT 2: The problem seems to be the hybrid-rsa-stream, wich dosn't crypt the blocks of bytes with the public key, but crypts all the file with AES-256-CBC and a random key that he generates, then at the end of the operation, hybrid-rsa-stream adds the random generated key crypted with the public key. how can I "revert" this operations in c#?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?