Examples of how to use C # cryptography Classes

Source: Internet
Author: User
Tags decrypt
examples of how to use C # cryptography Classes

Using system;using system.io;using system.text;using system.security.cryptography;using System.Web;namespace  encryption.app_code{//<summary>//Plus password class///</summary> public class Encryption {//        <summary>//encryption///</summary>//<param name= "InputString" ></param> <returns></returns> public static string Desencrypt (String inputstring) {retur        N Desencrypt (inputstring, Key); }//<summary>//Decrypt//</summary>//<param name= "InputString" ></par            am>//<returns></returns> public static string Desdecrypt (string inputstring) {        Return Desdecrypt (inputstring, Key);            }///<summary>//key//</summary> private static string key {            get {return "Hongye10";    }    }///<summary>///encryption string///NOTE: The key must be 8 bits///</summary>//<param N Ame= "StrText" > String </param>//<param name= "Encryptkey" > Key </param>///<param name= "E        Ncryptkey "> returns the encrypted string </param> public static desencrypt string (string inputstring, String encryptkey)            {byte[] Bykey = null;            Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};                try {bykey = System.Text.Encoding.UTF8.GetBytes (encryptkey.substring (0, 8));                DESCryptoServiceProvider des = new DESCryptoServiceProvider ();                byte[] Inputbytearray = Encoding.UTF8.GetBytes (inputstring);                MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, Des.                CreateEncryptor (Bykey, IV), cryptostreammode.write); Cs.                Write (Inputbytearray, 0, inputbytearray.length); Cs. FlushfInalblock (); Return convert.tobase64string (Ms.            ToArray ()); } catch (System.Exception error) {//return error.                Message;            return null; }}///<summary>//Decrypt string///</summary>//<param name= "this.inputst Ring > Secret string </param>//<param name= "Decryptkey" > Key </param>///<param name= "decry            Ptkey "> returns decrypted string </param> public static Desdecrypt string (string inputstring, String decryptkey) {            byte[] Bykey = null;            Byte[] IV = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};            byte[] Inputbytearray = new Byte[inputstring.length];                try {bykey = System.Text.Encoding.UTF8.GetBytes (decryptkey.substring (0, 8));                DESCryptoServiceProvider des = new DESCryptoServiceProvider (); Inputbytearray = convert.frombase64sTring (inputstring);                MemoryStream ms = new MemoryStream (); CryptoStream cs = new CryptoStream (MS, Des.                CreateDecryptor (Bykey, IV), cryptostreammode.write); Cs.                Write (Inputbytearray, 0, inputbytearray.length); Cs.                FlushFinalBlock ();                System.Text.Encoding Encoding = new System.Text.UTF8Encoding (); return encoding. GetString (Ms.            ToArray ()); } catch (System.Exception error) {//return error.                Message;            return null; }        }    }}


The above is the content of the C # Cryptography Class Usage example, please pay attention to topic.alibabacloud.com (www.php.cn) for more related content!

  • 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.