A method of symmetric encryption and decryption C # tool class

Source: Internet
Author: User

Using system;using system.collections.generic;using system.text;using system.io;using System.Security.Cryptography; Namespace CMD. edi{public class Encrypthandler {//<summary>///encryption string///</summary> PU            Blic static string Encrypt (string password, string cleartext) {string password2 = "Ahbool";            string cipher;            char[] key = new CHAR[8]; if (password. Length > 8) {password = password.            Remove (8); } password. CopyTo (0, key, 0, password.            Length);            Char[] IV = new CHAR[8]; if (Password2. Length > 8) {password2 = Password2.            Remove (8); } password2. CopyTo (0, IV, 0, Password2.            Length); if (cleartext = = null) {return string.            Empty;            } symmetricalgorithm serviceprovider = new DESCryptoServiceProvider (); ServiceprovideR.key = Encoding.ASCII.GetBytes (Key);            SERVICEPROVIDER.IV = Encoding.ASCII.GetBytes (IV);            MemoryStream MemoryStream = new MemoryStream (); CryptoStream cryptostream = new CryptoStream (MemoryStream, Serviceprovider.createencryptor (), CryptoStreamMode.Write            );            StreamWriter StreamWriter = new StreamWriter (CryptoStream);            StreamWriter.Write (cleartext);            Streamwriter.dispose ();            Cryptostream.dispose ();            byte[] SignData = Memorystream.toarray ();            Memorystream.dispose ();            Serviceprovider.clear ();            cipher = convert.tobase64string (signdata);        return cipher; }//<summary>//Decrypt string///</summary> public static string Decrypt (String PASSW            Ord, String ciphertext) {string password2 = "Ahbool"; string cipher = string.            Empty;              try {char[] key = new CHAR[8];  if (password. Length > 8) {password = password.                Remove (8); } password. CopyTo (0, key, 0, password.                Length);                Char[] IV = new CHAR[8]; if (Password2. Length > 8) {password2 = Password2.                Remove (8); } password2. CopyTo (0, IV, 0, Password2.                Length);                if (ciphertext = = null) {return cipher;                } symmetricalgorithm serviceprovider = new DESCryptoServiceProvider ();                Serviceprovider.key = Encoding.ASCII.GetBytes (Key);                SERVICEPROVIDER.IV = Encoding.ASCII.GetBytes (IV);                byte[] Contentarray = convert.frombase64string (ciphertext);                MemoryStream MemoryStream = new MemoryStream (Contentarray); CryptoStream cryptostream = new CryptoStream (MemoryStream, Serviceprovider.createdecryptor (), CryptoStreamMOde.                Read);                StreamReader StreamReader = new StreamReader (CryptoStream);                cipher = Streamreader.readtoend ();                Streamreader.dispose ();                Cryptostream.dispose ();                Memorystream.dispose ();            Serviceprovider.clear ();            } catch (Exception ex) {throw new SystemException ("Key error, packet decryption failed.");        } return cipher; }    }}

  

A method of symmetric encryption and decryption C # tool class

Related Article

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.