The data to be decrypted exceeds the maximum for t

2019-02-20 23:21发布

i do not know how to solve this exception while decrypting a file using private key from a X509 certificate. "The data to be decrypted exceeds the maximum for this modulus of 128 bytes."

byte[] data = File.ReadAllBytes("F:\\enc test\\file1.txt");
X509Certificate2 cer = new X509Certificate2(
    "E:\\fileManagementSrvc\\certificate\\cerpfx.pfx", "12345",
    X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cer.PrivateKey;

byte[] d = rsa.Decrypt(data, false); //GETTING THE EXCEPTION HERE
string s = Encoding.Default.GetString(d);

2条回答
放我归山
2楼-- · 2019-02-20 23:49

RSA cannot encrypt data longer than modulus of RSA key. And if you use some kind of a padding, this value became even smaller.

Normally encryption of large data is performed with symmetric block ciphers like AES or Triple-DES as they are faster than asymmetric ciphers such as RSA and designed to encrypt data longer than their key size. If you still want to use asymmetric encryption then encrypt AES/Tripple-DES key, which is shorter than 128 bytes with RSA at the end of the process.

查看更多
戒情不戒烟
3楼-- · 2019-02-21 00:04

See this discussion: RSA Encrypt / Decrypt Problem in .NET In short, there is indeed a fixed size for a message you can encrypt with rsa. Its not designed to encrypt the whole big file.

查看更多
登录 后发表回答