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#?