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 = " 7w2qsVRBn168Ehc4V / fiPML 7WUkORRIJ9I8i21Fs5GlvYrja2CzBzPLKrAHumLOCLgd / qKj0iApF17471nfKw == P> "); 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 = " 7w2qsVRBn168Ehc4V / fiPML 7WUkORRIJ9I8i21Fs5GlvYrja2CzBzPLKrAHumLOCLgd / qKj0iApF17471nfKw == P> 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 ()); 8oztAlInRK1VDuVLHnPPcNQsehbP9IF5p kwRu07sFGwAHnyeWuRG0EpebvbGOE / 1KzpKqb / WU8vSN4OeauohQ == Q>
8oztAlInRK1VDuVLHnPPcNQsehbP9IF5p kwRu07sFGwAHnyeWuRG0EpebvbGOE / 1KzpKqb / WU8vSN4OeauohQ == Q>