1 //Symmetric encryption Helper class2 Public Static classCryptohelper3 {4 //For more information, refer to http://www.cnblogs.com/JimmyZhang/archive/2008/10/02/Cryptograph.html5 Private StaticICryptoTransform encryptor;//Crypto Object6 Private StaticICryptoTransform decryptor;//Decryption Object7 8 Private StaticSymmetricAlgorithm Provider = Symmetricalgorithm.create ("TripleDES");9 Ten Private Const intBufferSize =1024x768; One A - /// <summary> - ///Encryption Algorithm the /// </summary> - /// <param name= "key" >is 24 or 16-bit characters</param> - /// <param name= "Encrypttext" >strings that are encrypted</param> - /// <returns></returns> + Public Static stringEncrypt (stringKeystringencryptedtext) - { +PROVIDER.IV =New byte[] {0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF }; AProvider. Key =Encoding.UTF8.GetBytes (key); at -Encryptor =provider. CreateEncryptor (); - //create a clear flow - byte[] Clearbuffer =Encoding.UTF8.GetBytes (encryptedtext); -MemoryStream ClearStream =NewMemoryStream (clearbuffer); - in //create an empty ciphertext stream -MemoryStream Encryptedstream =NewMemoryStream (); to +CryptoStream CryptoStream = - NewCryptoStream (Encryptedstream, Encryptor, cryptostreammode.write); the * //write plaintext stream to buffer $ //writes data from buffer to CryptoStreamPanax Notoginseng intBytesread =0; - byte[] buffer =New byte[buffersize]; the Do + { ABytesread = clearstream.read (buffer,0, buffersize); theCryptostream.write (Buffer,0, bytesread); +} while(Bytesread >0); - $ Cryptostream.flushfinalblock (); $ - //gets the encrypted text -Buffer =Encryptedstream.toarray (); the stringstr =convert.tobase64string (buffer); - returnstr;Wuyi } the - //Decryption Algorithm Wu /// <summary> - /// </summary> About /// <param name= "key" >is 24 or 16-bit characters</param> $ /// <param name= "Decryptedtext" >the decrypted string</param> - /// <returns></returns> - Public Static stringDecrypt (stringKeystringdecryptedtext) - { APROVIDER.IV =New byte[] {0x12,0x34,0x56,0x78,0x90,0xAB,0xCD,0xEF }; +Provider. Key =Encoding.UTF8.GetBytes (key); the -Decryptor =provider. CreateDecryptor (); $ byte[] Encryptedbuffer =convert.frombase64string (decryptedtext); theStream Encryptedstream =NewMemoryStream (encryptedbuffer); the theMemoryStream ClearStream =NewMemoryStream (); theCryptoStream CryptoStream = - NewCryptoStream (Encryptedstream, Decryptor, cryptostreammode.read); in the intBytesread =0; the byte[] buffer =New byte[buffersize]; About the Do the { theBytesread = cryptostream.read (buffer,0, buffersize); +Clearstream.write (Buffer,0, bytesread); -} while(Bytesread >0); the BayiBuffer =Clearstream.getbuffer (); the stringstr = theEncoding.UTF8.GetString (Buffer,0, (int) clearstream.length); - - returnstr; the } the the}
Cryptographic decryption helper Class (symmetric encryption)