Cryptographic decryption helper Class (symmetric encryption)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.