Author Blog: http://blog.9cbs.net/curn ----- Learn each other
Using system.seircurity.cryptography; using system.text; using system.windows.form;
namespace Curllion {public class Crypt {private byte [] key; private byte [] iv; private System.Text.ASCIIEncoding asciiEncoding; private System.Text.UnicodeEncoding textConverter; private RC2CryptoServiceProvider rc2CSP; public Crypt () {InitializeComponent ();} private void InitializeComponent () {key = new byte [] {106,51,25,141,157,142,23,111,234,159,187,154,215,34,37,204}; iv = new byte [] {135,186,133,136,184,149,153,144}; asciiEncoding = new System.Text.ASCIIEncoding (); textConverter = new System .Text.unicodeEncoding (); RC2CSP = new rc2cryptoServiceProvider ();} ///
For (int i = 0; i <10261; i ) TMP [I] = 0; WriteFileStream.write (TMP, 0, 10261); WriteFileStream.flush (); writeFileStream.close ();} catch (system.io. IOEXCEPTION) {MessageBox.show ("File Operation Error!", "Error!", MessageBoxButtons.ok, MessageBoxicon.Error);}} ///
Byte [] index = new byte [10261]; // write the read content to the BYTE array TmpFileStream.read (index, 0,10261); tmpfilestream.close (); // Define basic encryption conversion operation System.security .Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor (this.key, this.iv); System.IO.MemoryStream msEncrypt = new MemoryStream (); // this stream encryption conversion, from CSEncrypt encryption, encrypted, in the result Msencrypt stream. System.Security.cryptography.cryptostream csencrypt = new cryptostream (msencrypt, encryptor, cryptostreammode.write); // The text to be encrypted into UTF-16 encoding, saved in the TMP array. Byte [] tmp = textconverter.getbytes (toEncryptText); // Enter the TMP CSENCRYPT will be encrypted via Encryptor. CSencrypt.write (TMP, 0, Tmp.Length); // Output to msenctypt csencrypt.flushfinalBlock (); // Transfer stream to Byte [] encrypted = msencrypt.toarray (); if (Encrypted.Length> 1024) {MessageBox .Show ("After encryption, the data length is greater than 1kb, no saving"); Return False;} // Get the size of the data after encryption, the result exists the result.
Index [DataIndex * 2 - 1] = Convert.TOBYTE (Convert.TOString (Encrypted.Length / 128)); Index [DataIndex * 2] = Convert.TOBYTE (Convert.TOString (Encrypted.Length% 128)); // Write the encrypted result into INDEX FOR (INT i = 0; I
TmpFileStream.read (Index, 0,10261); int StartIndex = 1024 * (DataIndex-1) 21; int count = index [dataindex * 2 - 1] * 128 index [dataindex * 2]; byte [] TMP = New byte [count]; Array.copy (DataIndex-1) 21, TMP, 0, Count; csdecrypt.write; csdecrypt.flushfinalBlock (); decrypted = msdecrypt. ToArray (); r = textconverter.getstring (Decrypted, 0, Decrypted.Length); TmpFileStream.close (); Return R;} ///
System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor (this.key, this.iv); System.IO.MemoryStream msEncrypt = new MemoryStream (); System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream (msEncrypt, Encryptor, CryptostReammode.write;
byte [] tmp = textConverter.GetBytes (toEncryptText); csEncrypt.Write (tmp, 0, tmp.Length); csEncrypt.FlushFinalBlock (); encrypted = msEncrypt.ToArray (); tmpFileStream.Write (encrypted, 0, encrypted.Length ); Tmpfilestream.flush (); r = true; tmpfilestream.close (); return r;} ///
byte [] tmp = new byte [tmpFileStream.Length]; tmpFileStream.Read (tmp, 0, tmp.Length); csDecrypt.Write (tmp, 0, tmp.Length); csDecrypt.FlushFinalBlock (); decrypted = msDecrypt.ToArray (); R = textconverter.getstring (Decrypted, 0, Decrypted.Length); TMPFILESTREAM.CLOSE (); Return R;} // ------------------------------------------------------------------------------------------------------------ ----------------------------------------- /// Byte [] index = new byte [10261]; // write the read content to the BYTE array TmpFileStream.read (index, 0,10261); tmpfilestream.close (); // Define basic encryption conversion operation System.security .Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor (Key, IV); System.IO.MemoryStream msEncrypt = new MemoryStream (); // this encrypted after conversion stream from CSEncrypt encryption, encrypted, in the result msEncrypt stream. System.Security.cryptography.cryptostream csencrypt = new cryptostream (msencrypt, encryptor, cryptostreammode.write); // The text to be encrypted into UTF-16 encoding, saved in the TMP array. Byte [] tmp = textconverter.getbytes (toEncryptText); // Enter the TMP CSENCRYPT will be encrypted via Encryptor. CSencrypt.write (TMP, 0, Tmp.Length); // Output to msenctypt csencrypt.flushfinalBlock (); // Transfer stream to Byte [] encrypted = msencrypt.toarray (); if (Encrypted.Length> 1024) {MessageBox .Show ("After encryption, the data length is greater than 1kb, no saving"); Return False;} // Get the size of the data after encryption, the result exists the result. Index [DataIndex * 2 - 1] = Convert.TOBYTE (Convert.TOString (Encrypted.Length / 128)); Index [DataIndex * 2] = Convert.TOBYTE (Convert.TOString (Encrypted.Length% 128)); // Write the encrypted result into INDEX FOR (INT i = 0; I System.Security.Cryptography.ICryptoTransform Decryptor = rc2CSP.CreateDecryptor (Key, IV); System.IO.MemoryStream msDecrypt = new MemoryStream (); System.Security.Cryptography.CryptoStream csDecrypt = new CryptoStream (msDecrypt, Decryptor, CryptoStreamMode.Write) BTE [] index = new byte [10261]; tmpfilestream.read (index, 0,10261); int startex = 1024 * (DataIndex-1) 21; int count = index [dataindex * 2 - 1] * 128 Index [DataIndex * 2]; Byte [] TMP = New Byte [count]; Array.copy (INDEX, 1024 * (DataIndex-1) 21, TMP, 0, Count; CSDecrypt.write (TMP, 0, Count); csdecrypt.flushfinalBlock (); decrypted = msdecrypt.toArray (); r = TextconvertR.getstring (Decrypted, 0, Decrypted.Length); TMPFileStream.close (); return r;} /// System.Security.Cryptography.ICryptoTransform Encryptor = rc2CSP.CreateEncryptor (Key, IV); System.IO.MemoryStream msEncrypt = new MemoryStream (); System.Security.Cryptography.CryptoStream csEncrypt = new CryptoStream (msEncrypt, Encryptor, CryptoStreamMode.Write) ; byte [] tmp = textConverter.GetBytes (toEncryptText); csEncrypt.Write (tmp, 0, tmp.Length); csEncrypt.FlushFinalBlock (); encrypted = msEncrypt.ToArray (); tmpFileStream.Write (encrypted, 0, encrypted.Length ); Tmpfilestream.flush (); r = true; tmpfilestream.close (); return r;} ///