RSA encryption of data error beyond 117 bytes (ZT)

xiaoxiao2021-03-06  82

Article Source: http://www.cnblogs.com/zhenyulu

The previous array of public security bureaus is used to use public key encryption technology and symmetric key encryption technology. The information is encrypted by 3DES, and the key is transmitted through the RSA public key system. The client is decrypted using the CPU card ekey. However, in the system writing process, the RSA encryption algorithm in .NET adds some random numbers to be added before the data is ready to be encrypted, so the RSA encryption algorithm in the .NET encrypted 117 bytes (more The 117-byte needs to be split into a plurality of segments, encrypted and then connected, and after encryption, obtain a length of 128 bytes of encrypted data. However, this will bring a lot of trouble for the public key system that needs to be confirmed by the identity of both parties. In my system, I need to implement online encryption of the user session key by following the steps:

Encryption process: 1. Add a random number to the session key, add 128 bits, 2, use the CA private key to decrypt, the result is 128-bit data, 3, encrypt the data using the user public key to obtain 128-bit data, transmitted over the network .

Decryption process: 1. Use the user private key to decrypt the 128-bit data transferred on the online transfer; 2. Use the CA public key encryption; 3, remove the random number used to confuse, extract the session key

However, the RSA encryption in .NET can only operate 117 bytes of data, resulting in 128-bit data to process two parts, so encrypted data constantly expands. In order to solve this problem, the RSA encryption, the decryption process is consistent with the process on the EKEY, I have to write my own RSA encryption algorithm.

After finding a lot of information, I decided to use the ready-made Biginteger class. You can get more information about http://www.codeproject.com/csharp/biginteger.asp. With Biginteger, I added two ways RSAENCRYPT and RSADECrypt to implement RSA encryption decryption. This will never be limited by 117 bytes.

The two sections are given, and the program first is to use .NET comes with the RSA encryption algorithm to achieve encrypted decryption, once the TextLength property exceeds 117, the system will not be encrypted; the second is the transformed system, and 128-bit data can be performed. Encryption, no restrictions on 117. The program is omitted by the Biginteger class. If you need it, you can download it from http://www.codeger.asp, don't forget to comment the main method, otherwise there is a compilation error when compiling, saying two An entry point (of course, you can also specify an entry point in the project properties).

Program 1:

Using

System; using

System.security.cryptography; using

System.Text; Class

Oldrsa ...

{Static void Main () ... {int TextLength = 117; byte [] encryptedData; byte [] decryptedData; string Key1 = " 4n6EJsx4qNFpp6h wcPdJz8sSMMRJEVJaBQEGsOOBHKNePo / v3M94Nf89 zL5lLH7 / LuRgcUfnizVIETH / z9 H / yDuM0F3fjImN3UtK1TK0ioFf0cVC9lnErbEoEjmkeQIVUJUC4c BmqtTN6UrhFCY3R3zGp3feeGqORLjeKVc = AQAB

7w2qsVRBn168Ehc4V / fiPML 7WUkORRIJ9I8i21Fs5GlvYrja2CzBzPLKrAHumLOCLgd / qKj0iApF17471nfKw == 8oztAlInRK1VDuVLHnPPcNQsehbP9IF5p kwRu07sFGwAHnyeWuRG0EpebvbGOE / 1KzpKqb / WU8vSN4OeauohQ == DIh 5oUwW5av7ZLiFVqdtenTS8b9uzBhCBVxry2vddaxBdr SWbse / gvMrG / 9fmwK6zbhbopNJ8TCHKmQoZHuQ == 6g96q / GxeUG3Qk dBP8HIL9vSEX5Wd8UEigicV9 / aS / 7IwqLJgbama1xI8tXrBO6MDbIL2PGKF4UqEG5QEqZrQ == nyx28u1fREiIgXgx2S5 PXbB8wq0xVxnE2G2Mt0vq9xQDHbaXEFpfznjNaga8AhVluNahqG5uRGRY3OgQONO4g == PVKj1R1nTc3lHU XGitvq9QE0TR9V6RCY7SFOV9XBCM / YPF20Q8SOD3Y0AD87U9ccssdwfjyagukai0wugjfgfalf8 / 4pfwqzrgblsn96klmklmy7c6oihlriw myxmvaggsp3 / r4se6wgk5 ISchjKIKyv / PyWoobDRe6orDzIE = "; try ... {RSACryptoServiceProvider RSA = new RSACryptoServiceProvider (); RSA.FromXmlString (Key1); byte [] dataToEncrypt = GenerateBytes (TextLength); Console.WriteLine (" Original buff: " Convert.ToBase64String (dataToEncrypt) " "); encryptedData = RSA.Encrypt (dataToEncrypt, false); Console.WriteLine (" Encrypted buff: " Convert.ToBase64String (encryptedData) "

"); DecryptedData = RSA.Decrypt (encryptedData, false); Console.WriteLine (" Decrypted buff: " Convert.ToBase64String (decryptedData) " ");} catch ... {Console.WriteLine (" Encryption failed. " );}} // ************************************************************ ************************************************************************************* *********************************************************** *********** Public static Byte [] generatebytes (int Bytelength) ... {byte [] buff = new byte [bytelength]; rngcryptoserviceProvider RNG = new RNGCRYPTOSERVICEPROVIDER (); // This array has been used Password enhanced random bytes fill RNG.GetBytes (buff); Return BUFF;}} 2:

Using

System; using

System.security.cryptography; using

System.Text; Class

Newrsa ...

{Public static void Main () ... {int TextLength = 128; byte [] encryptedData; byte [] decryptedData; string Key1 = " 4n6EJsx4qNFpp6h wcPdJz8sSMMRJEVJaBQEGsOOBHKNePo / v3M94Nf89 zL5lLH7 / LuRgcUfnizVIETH / z9 H / yDuM0F3fjImN3UtK1TK0ioFf0cVC9lnErbEoEjmkeQIVUJUC4c BmqtTN6UrhFCY3R3zGp3feeGqORLjeKVc = AQAB

7w2qsVRBn168Ehc4V / fiPML 7WUkORRIJ9I8i21Fs5GlvYrja2CzBzPLKrAHumLOCLgd / qKj0iApF17471nfKw == 8oztAlInRK1VDuVLHnPPcNQsehbP9IF5p kwRu07sFGwAHnyeWuRG0EpebvbGOE / 1KzpKqb / WU8vSN4OeauohQ == DIh 5oUwW5av7ZLiFVqdtenTS8b9uzBhCBVxry2vddaxBdr SWbse / gvMrG / 9fmwK6zbhbopNJ8TCHKmQoZHuQ == 6g96q / GxeUG3Qk dBP8HIL9vSEX5Wd8UEigicV9 / aS / 7IwqLJgbama1xI8tXrBO6MDbIL2PGKF4UqEG5QEqZrQ == nyx28u1fREiIgXgx2S5 PXbB8wq0xVxnE2G2Mt0vq9xQDHbaXEFpfznjNaga8AhVluNahqG5uRGRY3OgQONO4g == PVKj1R1nTc3lHU XGitvq9QE0TR9V6RCY7SFOV9XBCM / YPF20Q8SOD3Y0AD87U9ccssdwfjyagukai0wugjfgfalf8 / 4pfwqzrgblsn96klmklmy7c6oihlriw myxmvagssp3 / r4 sE6wGk5ISchjKIKyv / PyWoobDRe6orDzIE = "; try ... {RSACryptoServiceProvider RSA = new RSACryptoServiceProvider (); RSA.FromXmlString (Key1); RSAParameters RSAKeyInfo = RSA.ExportParameters (true); byte [] dataToEncrypt = GenerateBytes ("Original Buff:" Convert.TOBASE64STRING (DataToEncrypt) ""); EncryptedData = RSAENCrypt (DatatoEncrypt, RsakeyInfo.exponent, RsakeyInfo.modulus);

Console.WriteLine ( "Encrypted buff:" Convert.ToBase64String (encryptedData) ""); decryptedData = RSADecrypt (encryptedData, RSAKeyInfo.D, RSAKeyInfo.Modulus); Console.WriteLine ( "Decrypted buff:" Convert.ToBase64String ( DecryptedData) "");} catch ... {console.writeline ("Encryption failed.");}} // ****************************** ********************************************************* / / Rsa encrypt // ************************************************************* ******************************************** Static public Byte [] rTE [] DataToencrypt, Byte [] Exponent, Byte [] MODULUS) .. . {BigInteger original = new BigInteger (dataToEncrypt); BigInteger e = new BigInteger (Exponent); BigInteger n = new BigInteger (Modulus); BigInteger encrypted = original.modPow (e, n); return HexStringToByte (encrypted.ToHexString ()) } // ************************************************************* *********************** // RSA Decrypt // *************************** ********** ************************************************************** Static Public Byte [] RSADecrypt ( byte [] encryptedData, byte [] D, byte [] Modulus) ... {BigInteger encrypted = new BigInteger (encryptedData); BigInteger d = new BigInteger (D); BigInteger n = new BigInteger (Modulus); BigInteger decrypted = encrypted .MODPOW (D, N); Return HEXSTRINGTOBYTE (Decrypted.toHexString ());

转载请注明原文地址:https://www.9cbs.com/read-95362.html

New Post(0)