System. Security. cryptography. cryptographicexception: The data to be decrypted exceeds the maximum fo

Source: Internet
Author: User
Tags modulus asymmetric encryption

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.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.