Creating a software application requires asymmetric encryption.AlgorithmEncryption of registration information. What I did not expect was that RSA could only use the public key to encrypt and decrypt the private key. This is exactly the opposite of my needs. So I found the encryption algorithm developed by Niu Ren on the Internet, after use, you can find that there is a problem with Chinese support.
Address: http://www.cnblogs.com/hhh/archive/2011/06/03/2070692.html
The original algorithm splits the input string by 128 characters in each segment. However, if there is a Chinese character in the split position, garbled characters may occur after decryption, which is a headache.
After studying the algorithm carefully, I found that the method for processing data in the string method was not reasonable, so I overwrote the algorithm and processed it in binary mode, successfully solving the garbled problem, the following are the improved algorithms.
Public Static Byte [] Encrypt ( Byte [] Data, biginteger D, biginteger N ){ Int Size = 128 ; Byte [] Buffer = New Byte [Size]; Using (Memorystream source = New Memorystream (data )) Using (Memorystream output = New Memorystream ()){ Int Bytes; While (Bytes = source. Read (buffer,0 , Size)> 0 ) {Biginteger bi1 = New Biginteger (buffer, bytes); biginteger bi2 = Bi1.modpow (d, n); Data = Bi2.getbytes (); system. Diagnostics. Debug. Assert (data. Length < 256 ); Output. writebyte (( Byte ) Data. Length); output. Write (data, 0 , Data. Length );} Return Output. toarray ();}} Public Static Byte [] Decrypt ( Byte [] Data, biginteger E, biginteger N ){ Using (Memorystream input = New Memorystream (data )) Using (Memorystream output =New Memorystream ()){ Int Length; While (Length = input. readbyte ()> = 0 ){ Byte [] Buffer = New Byte [Length]; If (Input. Read (buffer, 0 , Length )! =Length) Throw New Invaliddataexception (); biginteger bi1 = New Biginteger (buffer); biginteger bi2 = Bi1.modpow (E, n); Buffer = Bi2.getbytes (); output. Write (buffer, 0 , Buffer. Length );} Return Output. toarray ();}} Public Static String Encrypt ( String Source, String D, String N ){ Byte [] N = Convert. frombase64string (N ); Byte [] D = Convert. frombase64string (d); biginteger Bin =New Biginteger (n); biginteger bid = New Biginteger (d ); Byte [] DATA = System. Text. encoding. utf8.getbytes (source); Data = Encrypt (data, bid, BIN ); Return Convert. tobase64string (data );} Public Static String Decrypt ( String Source, String E, String N ){ Byte [] N = Convert. frombase64string (N ); Byte [] E = Convert. frombase64string (E); biginteger Bin = New Biginteger (n); biginteger BIE = New Biginteger (E ); Byte [] DATA =Convert. frombase64string (source); Data = Decrypt (data, Bie, BIN ); Return System. Text. encoding. utf8.getstring (data );}
Finally, I would like to thank the author for his efforts and saved me many things.
ReferenceArticle:
[1]RSA private key encryption and Public Key decryption algorithm
[2] C # Improve public key encryption and decryption using RSA private key to solve the problem of garbled code after decryption in specific circumstances
[3]Biginteger big Integer Operation class