RSA segment encryption and decryption [solve the "incorrect length" exception]

Source: Internet
Author: User
Tags asymmetric encryption

Http://www.cnblogs.com/zys529/archive/2012/05/24/2516539.html

RSA is a common asymmetric encryption algorithm. Recently, an "incorrect length" exception occurred. The study found that the data to be encrypted is too long.

. NET Framework provides the following RSA algorithm rules:

The number of bytes to be encrypted cannot exceed the length value of the key divided by 8 and then minus 11 (rsacryptoserviceprovider. keysize/8-11), and the number of bytes of the encrypted ciphertext is exactly the length value of the key divided by 8 (that is, rsacryptoserviceprovider. keysize/8 ).

Therefore, if you want to encrypt long data, you can use the multipart encryption and decryption method. The implementation method is as follows:

RSA is a common asymmetric encryption algorithm. Recently, an "incorrect length" exception occurred. The study found that the data to be encrypted is too long.

. NET Framework provides the following RSA algorithm rules:

The number of bytes to be encrypted cannot exceed the length value of the key divided by 8 and then minus 11 (rsacryptoserviceprovider. keysize/8-11), and the number of bytes of the encrypted ciphertext is exactly the length value of the key divided by 8 (that is, rsacryptoserviceprovider. keysize/8 ).

Therefore, if you want to encrypt long data, you can use the multipart encryption and decryption method. The implementation method is as follows:

Namespace macroresolute. rsacryptoservice
{
Public static class rsacrypto
{
Private Static readonly encoding encoder = encoding. utf8;
 
Public static string encrypt (this string plaintext)
{
X509certificate2 _ x509certificate2 = rsacrypto. retrievex509certificate ();
Using (rsacryptoserviceprovider rsacryptography = _ x509certificate2. publickey. Key as rsacryptoserviceprovider)
{
Byte [] plaintextdata = rsacrypto. encoder. getbytes (plaintext );
Int maxblocksize = rsacryptography. keysize/8-11; // Maximum length of the encrypted Block
 
If (plaintextdata. Length <= maxblocksize)
Return convert. tobase64string (rsacryptography. Encrypt (plaintextdata, false ));
 
Using (memorystream plaistream = new memorystream (plaintextdata ))
Using (memorystream crypstream = new memorystream ())
{
Byte [] buffer = new byte [maxblocksize];
Int blocksize = plaistream. Read (buffer, 0, maxblocksize );
 
While (blocksize> 0)
{
Byte [] toencrypt = new byte [blocksize];
Array. Copy (buffer, 0, toencrypt, 0, blocksize );
 
Byte [] cryptograph = rsacryptography. Encrypt (toencrypt, false );
Crypstream. Write (cryptograph, 0, cryptograph. Length );
 
Blocksize = plaistream. Read (buffer, 0, maxblocksize );
}
 
Return convert. tobase64string (crypstream. toarray (), base64formattingoptions. None );
}
}
}
 
Public static string decrypt (this string ciphertext)
{
X509certificate2 _ x509certificate2 = rsacrypto. retrievex509certificate ();
Using (rsacryptoserviceprovider rsacryptography = _ x509certificate2. privatekey as rsacryptoserviceprovider)
{
Byte [] ciphertextdata = convert. frombase64string (ciphertext );
Int maxblocksize = rsacryptography. keysize/8; // Maximum length of the decrypted Block
 
If (ciphertextdata. Length <= maxblocksize)
Return rsacrypto. encoder. getstring (rsacryptography. decrypt (ciphertextdata, false ));
 
Using (memorystream crypstream = new memorystream (ciphertextdata ))
Using (memorystream plaistream = new memorystream ())
{
Byte [] buffer = new byte [maxblocksize];
Int blocksize = crypstream. Read (buffer, 0, maxblocksize );
 
While (blocksize> 0)
{
Byte [] todecrypt = new byte [blocksize];
Array. Copy (buffer, 0, todecrypt, 0, blocksize );
 
Byte [] plaintext = rsacryptography. decrypt (todecrypt, false );
Plaistream. Write (plaintext, 0, plaintext. Length );
 
Blocksize = crypstream. Read (buffer, 0, maxblocksize );
}
 
Return rsacrypto. encoder. getstring (plaistream. toarray ());
}
}
}
 
Private Static x509certificate2 retrievex509certificate ()
{
Return NULL; // retrieve the x509certificate2 certificate used for RSA Encryption
}
}
}

Note: The string type returned by the preceding encryption method is the original base-64. To use it for URL transmission, you need to handle it separately!

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.