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

使用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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害