C # strings using keys to add and decrypt _c# tutorial

Source: Internet
Author: User
Tags decrypt

The first to share is a C # string that uses a key to encrypt and decrypt the code, which reads as follows

public class Desencrypt {///<summary>///algorithm offset///</summary> const string M_iv = "12345
 
    678 "; <summary>///Function Description: Generate 8-bit key based on key input///Author: Love to template network 2gei.cn///Date created: 2015-07-20 17:25:26///</s  ummary>///<param name= "strkey" >strkey</param>///<returns>8 bit key </returns> private static string Getkey (String strkey) {if (string).
      IsNullOrEmpty (strkey)) {strkey = "infocoll"; } if (strkey.
      Length% 8 = 0) {return strkey;
      else {return Getkey (strkey + "0"); }///<summary>///feature Description: Encrypted string///Author: Love to template network 2gei.cn///Date created: 2015-07-20 17:18:31/// Task number:///</summary>///<param name= "strsourcestring" > Original string </param>///<param name= "strk EY "> Key </param>///<returns> encrypted string </returns> public static string Encrypt (String STRsOurcestring, String strkey) {strkey = Getkey (strkey);
 
      byte[] Btkey = Encoding.UTF8.GetBytes (strkey);
 
      byte[] Btiv = Encoding.UTF8.GetBytes (M_IV);
 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider (); using (MemoryStream ms = new MemoryStream ()) {try {byte[] Indata = Encoding.UTF8.GetByte
          S (strsourcestring); using (CryptoStream cs = new CryptoStream (MS, Des.) CreateEncryptor (Btkey, Btiv), CryptoStreamMode.Write)) {cs.
 
            Write (indata, 0, indata.length); Cs.
          FlushFinalBlock (); Return convert.tobase64string (Ms.
        ToArray ());
        catch {return strsourcestring;
    ///<summary>///Feature Description: Decryption string///Author: Love to template network 2gei.cn///Date created: 2015-07-20 17:18:49  Task number:///</summary>///<param name= "strencryptedstring" > Original string </param>///<param Name= "StRkey "> Key </param>///<returns> decrypted string </returns> public static string Decrypt (String Strencryp
      Tedstring, String strkey) {strkey = Getkey (strkey);
 
      byte[] Btkey = Encoding.UTF8.GetBytes (strkey);
 
      byte[] Btiv = Encoding.UTF8.GetBytes (M_IV);
 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider (); using (MemoryStream ms = new MemoryStream ()) {try {byte[] Indata = convert.frombase64str
          ING (strencryptedstring); using (CryptoStream cs = new CryptoStream (MS, Des.) CreateDecryptor (Btkey, Btiv), CryptoStreamMode.Write)) {cs.
 
            Write (indata, 0, indata.length); Cs.
          FlushFinalBlock (); Return Encoding.UTF8.GetString (Ms.
        ToArray ());
        catch {return strencryptedstring;

 }
      }
    }
  }

C # string encryption and decryption

Using System.Security.Cryptography;
Using System.IO;

    Default key vector private static byte[] keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}; <summary>///des cryptographic strings///</summary>///<param name= "encryptstring" > Strings to be encrypted </param&
    Gt <param name= "Encryptkey" > Encryption key, required for 8-bit </param>///<returns> encryption to successfully return the encrypted string, failed to return the source string </returns > public static string Encryptdes (String encryptstring, String encryptkey) {try {byte[] R
        Gbkey = Encoding.UTF8.GetBytes (encryptkey.substring (0, 8));//Convert to byte byte[] Rgbiv = Keys;
        byte[] Inputbytearray = Encoding.UTF8.GetBytes (encryptstring); DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ()//materialized Data Encryption Standard MemoryStream Mstream = new MemoryStream ();//materialized memory stream//stream CryptoStream cstream = new CryptoStream (Mstream, Dcsp.createencryptor (Rgbkey,
        RGBIV), cryptostreammode.write); Cstream.write (inputByteArray, 0, inputbytearray.length);
        Cstream.flushfinalblock ();
      Return convert.tobase64string (Mstream.toarray ());
      catch {return encryptstring; }///<summary>///des decryption string///</summary>///<param name= "decryptstring" > Words to be decrypted String </param>///<param name= "Decryptkey" > Decryption key, required for 8-bit, and the same encryption key </param>///<returns> decryption successfully returned the decrypted
      String, failed back source string </returns> public static string Decryptdes (String decryptstring, String decryptkey) {try
        {byte[] Rgbkey = Encoding.UTF8.GetBytes (Decryptkey);
        byte[] Rgbiv = Keys;
        byte[] Inputbytearray = convert.frombase64string (decryptstring);
        DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider ();
        MemoryStream mstream = new MemoryStream (); CryptoStream cstream = new CryptoStream (Mstream, DCSP.
        CreateDecryptor (Rgbkey, Rgbiv), cryptostreammode.write); Cstream.write (inputbytEarray, 0, inputbytearray.length);
        Cstream.flushfinalblock ();
      Return Encoding.UTF8.GetString (Mstream.toarray ());
      catch {return decryptstring; } String encryptstr = Encryptdesstring.encryptdes ("Aaaaaaaaaa", "ssssssss");

 Returns the encrypted string decryptstr = Encryptdesstring.decryptdes (Encryptstr, "ssssssss");//Decrypt string

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.