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!