RSA fragment plus decryption "resolves" an incorrect length "exception"

Source: Internet
Author: User

Method 1:

RSA is a common asymmetric encryption algorithm. The recent use of an "incorrect length" anomaly has been found to be due to the extra length of data being encrypted.

The RSA algorithms provided in the. NET Framework stipulate that:

The number of bytes to be encrypted cannot exceed the length of the key by 8 minus 11 (i.e.:RSACRYPTOSERVICEPROVIDER.KEYSIZE/8-11), and the number of bytes encrypted after encryption is exactly the length of the key divided by 8 (that is: RSACRYPTOSERVICEPROVIDER.KEYSIZE/8).

Therefore, if you want to encrypt longer data, you can use the method of fragmentation and decryption, implemented as follows:

Code:

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 encryption block length limit

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; Decryption block Maximum length limit

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; Retrieving the X509CERTIFICATE2 certificate for RSA encryption
}
}
}

Method 2:

RSACryptoServiceProvider RSA = new
RSACryptoServiceProvider ();


byte[] data = ...;
The data to encrypt


String PublicKey = ....; //Get public key


Rsa. Fromxmlstring (PublicKey);


int keySize = RSA. KEYSIZE/8;


int buffersize = keySize-11;


byte[] buffer = new
Byte[buffersize];


MemoryStream msinput = new
MemoryStream (data);


MemoryStream msouput = new
MemoryStream ();


int readlen = msinput.read (buffer, 0,
buffersize);


while (Readlen > 0)


{


byte[] Datatoenc = new
Byte[readlen];


Array.copy (buffer, 0, Datatoenc,
0, Readlen);


byte[] Encdata =
Rsa. Encrypt (Datatoenc, false);


Msoutput.write (encdata, 0,
Encdata.length);


Readlen = msinput.read (buffer, 0,
buffersize);


}


Msinput.close ();


Byte[] result = Msoutput.toarray ();
Get encrypted results


Msoutput.close ();


Rsa. Clear ();



Decryption must also use fragment decryption, the algorithm is as follows:


RSACryptoServiceProvider RSA = new
RSACryptoServiceProvider ();


byte[] key = ...; //Load private key
String Privatekey =
Encoding.ASCII.GetString (key);
byte[] Dataenc = ...; //Loading ciphertext


Rsa. Fromxmlstring (Privatekey);


int keySize = RSA. KEYSIZE/8;


byte[]
Buffer = new Byte[keysize];


MemoryStream msinput = new
MemoryStream (DATAENC);


MemoryStream msouput = new
MemoryStream ();


int readlen = msinput.read (buffer, 0,
KeySize);


while (Readlen > 0)


{


byte[] Datatodec = new
Byte[readlen];


Array.copy (buffer, 0, Datatodec,
0, Readlen);


byte[] Decdata =
Rsa. Decrypt (Datatodec, false);


Msoutput.write (decdata, 0,
Decdata.length);


Readlen = msinput.read (buffer, 0,
KeySize);


}


Msinput.close ();


Byte[] result = Msoutput.toarray ();
Get decrypted results


Msoutput.close ();


Rsa. Clear ();

RSA fragment plus decryption "resolves" an incorrect length "exception"

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.