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