When using the C # asymmetric encryption rsacryptoserviceprovider class, an exception occurs: system. Security. cryptography. cryptographicexception: The data to be decrypted exceeds the maximum for this modulus of 128 bytes. Exception details: system. Security. cryptography. cryptographicexception: The data to be decrypted exceeds the maximum value of this module by 128 bytes. The error occurs inRSA. decrypt This line. Usually asymmetric encryption process: 1. Data on Client A is encrypted with the public key and transmitted over the network. 2. Data on client B is decrypted with the private key. However, in. net, RSA encryption can only operate on up to 117 bytes of data (128-bit minus a random number). As a result, 128-bit data has to be processed in two parts, so the encrypted data continues to expand. For more details, refer to the post stackoverflow.
Solution
There is an article in codeproject that can solve this problem well. Download biginteger class first.
Rsahelper
Public Static Class Rsahelper
{
/// <Summary>
/// RSAs the encrypt.
/// </Summary>
/// <Param name = "datatoencrypt"> The datatoencrypt. </Param>
/// <Param name = "exponent"> The exponent. </Param>
/// <Param name = "modulus"> The modulus. </Param>
/// <Returns> </returns>
Public Static Byte [] Rsaencrypt ( Byte [] Datatoencrypt, Byte [] Exponent, Byte [] Modulus)
{
VaR Original = New Biginteger (datatoencrypt );
VaR E = New Biginteger (exponent );
VaR N = New Biginteger (modulus );
VaR Encrypted = original. modpow (E, N );
Return Hexstringtobyte (encrypted. tohexstring ());
}
/// <Summary>
/// RSAs the decrypt.
/// </Summary>
/// <Param name = "encrypteddata"> The encrypteddata. </Param>
/// <Param name = "D"> The d. </Param>
/// <Param name = "modulus"> The modulus. </Param>
/// <Returns> </returns>
Public Static Byte [] Rsadecrypt ( Byte [] Encrypteddata, Byte [] D, Byte [] Modulus)
{
VaR Encrypted = New Biginteger (encrypteddata );
VaR Dd = New Biginteger (d );
VaR N = New Biginteger (modulus );
VaR Decrypted = encrypted. modpow (DD, N );
Return Hexstringtobyte (decrypted. tohexstring ());
}
/// <Summary>
/// Generate random bytes with Given length
/// </Summary>
/// <Param name = "bytelength"> </param>
/// <Returns> </returns>
Public Static Byte [] Generaterandombytes ( Int Bytelength)
{
VaR Buff = New Byte [Bytelength];
VaR RNG = New Rngcryptoserviceprovider ();
RNG. getbytes (buff );
ReturnBuff;
}
}
Encrypt
// Encrypt with Public Key
VaR RSA = New Rsacryptoserviceprovider ();
RSA. importparameters (_ publickey /* Type: rsaparameters */ );
Byte[] Encrypteddata = rsahelper. rsaencrypt (encoding. Unicode. getbytes (stringdatatoencrypt/*Type: String*/), Data. Parameters. exponent, Data. Parameters. modulus );
ReturnConvert. tobase64string (encrypteddata );Decrypt
// Decrypt
VaR RSA = New Rsacryptoserviceprovider ();
// Import Private Key
RSA. importparameters (_ privatekey /* Type: rsaparameters */ );
Byte [] Encrypteddata = rsahelper. rsadecrypt (convert. frombase64string (encryptedbase64string /* Type: string, but base64 format */ ), _ Privatekey. D, _ privatekey. modulus );
Return Encoding. Unicode. getstring (encrypteddata); More
For more details, refer to the post stackoverflow.