C # Simple cryptographic decryption process for strings C # SQL database login password encryption decryption encryption restore direct copy code to use

Source: Internet
Author: User
Tags decrypt

C # Simple cryptographic decryption process for strings C # SQL database login password encryption decryption encryption restore direct copy code to use

Using system;using system.collections.generic;using system.linq;using system.text;using System.Security.Cryptography; Using System.IO;    Namespace Pdaprint{class Classsecuritystring {public static string encryptkey = "www.hanma-scan.com"; Define key public static string Encrypt (String str) {DESCryptoServiceProvider DESCSP = new D   Escryptoserviceprovider (); Instantiate the Add/Decrypt class object byte[] key = Encoding.Unicode.GetBytes (Encryptkey); Defines a byte array to store the key byte[] data = Encoding.Unicode.GetBytes (str);//defines a byte array for storing the string to be encrypted Memo Rystream mstream = new MemoryStream (); Instantiate a memory Stream object//use a memory stream to instantiate an encrypted stream object CryptoStream cstream = new CryptoStream (Mstream, DESCSP.                   CreateEncryptor (key, key), cryptostreammode.write); Cstream.write (data, 0, data.  Length);              Writes data to the encrypted stream Cstream.flushfinalblock (); Release the encrypted stream return Convert.tobase64strinG (Mstream.toarray ());//returns the encrypted string}///<summary>//decryption string//</sum          mary>//<param name= "str" > string to Decrypt </param>//<returns> decrypted string </returns> public static string Decrypt (String str) {DESCryptoServiceProvider DESCSP = new Descrypt   Oserviceprovider (); Instantiate the Add/Decrypt class object byte[] key = Encoding.Unicode.GetBytes (Encryptkey); Defines a byte array to store the key byte[] data = convert.frombase64string (str);//defines a byte array to store the string Mem to decrypt Orystream mstream = new MemoryStream (); Instantiate a memory Stream object//Use a memory flow instance to dissolve a dense stream object CryptoStream cstream = new CryptoStream (Mstream, descsp.c                  Reatedecryptor (key, key), cryptostreammode.write); Cstream.write (data, 0, data.      Length);               Writes data to the decryption stream Cstream.flushfinalblock (); Release the decryption stream return Encoding.Unicode.GetString (MStream.toarray ()); Returns the decrypted string}}}


private void Button10_click (object sender, EventArgs e)
{
String str = "123"; Record the input string
String strnew =classsecuritystring.encrypt (str); Encrypt string
String strold = Classsecuritystring.decrypt (strnew);

MessageBox.Show ("" "Encrypt:" + strnew + @ "" + "" Decrypt: "+ Strold + @" "");
}

Han code Inventory machine original, reproduced please indicate the source.

Chinese code counting machine-Focus on the Fool-type warehouse barcode management, domestic warehouse bar code management overall solution leader brand.

(Official website: www.hanma-scan.com)


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # Simple cryptographic decryption process for strings C # SQL database login password encryption decryption encryption restore direct copy code to use

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.