1. Adding references
Using System.security.cryptography;using System.IO;
2. Add default key vector
Default key vector private static byte[] Keys = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
3. Adding a cryptographic decryption class
<summary>/// des encrypted string/// </summary>/// <param name= "EncryptString" > String to encrypt </param>/// <param name= "Encryptkey" > Encryption key, required for 8-bit </param>/// < Returns> encryption successfully returns the encrypted string, failing to return the source string </returns>public static string encryptdes (string encryptstring, string encryptkey) { try { byte[] rgbkey = encoding.utf8.getbytes (encryptkey.substring (0, 8));// Convert to bytes byte[] rgbIV = Keys; byte[] Inputbytearray = encoding.utf8.getbytes (encryptstring); Descryptoserviceprovider dcsp = new descryptoserviceprovider ();//Instantiate Data Encryption Standard memorystream mstream = new memorystream ();//Instantiate Memory stream // Stream that links a data stream to a cryptographic transformation cryptostream cstrEam = new cryptostream (Mstream, dcsp.createencryptor (RGBKEY,&NBSP;RGBIV), CryptoStreamMode.Write); cstream.write (inputbytearray, 0, Inputbytearray.length); cstream.flushfinalblock (); return Convert.tobase64string (Mstream.toarray ()); } catch { return encryptString; }}/// <summary>/// des Decrypt String/// </summary>/// <param name= " Decryptstring "> String to Decrypt </param>/// <param name=" Decryptkey "> Decryption key, required to be 8-bit, same as encryption key </ Param>/// <returns> decryption successfully returns the decrypted string, failed to return to the 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,&NBSP;DCSP. CreateDecryptor (RGBKEY,&NBSP;RGBIV), cryptostreammode.write); cstream.write ( Inputbytearray, 0, inputbytearray.length); cstream.flushfinalblock (); return encoding.utf8.getstring (Mstream.toarray ()); } catch { return decryptstring; }}
4. Call method (at the end of the article, there are related classes, directly after the compilation call)
Returns the encrypted string encryptstrstring encryptstr = Encryptdesstring.encryptdes ("This is a Test", "password"); MessageBox.Show (ENCRYPTSTR);//Decrypt string encryptstrstring decryptstr = Encryptdesstring.decryptdes (EncryptStr, "password "); MessageBox.Show (ENCRYPTSTR);
After encryption:
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/8B/F7/wKiom1hc_Z3TU_UwAAARwl1qpf8461.jpg "style=" float: none; "title=" qq20161223183332.jpg "alt=" Wkiom1hc_z3tu_uwaaarwl1qpf8461.jpg "/>
After decryption:
650) this.width=650; "src=" http://s2.51cto.com/wyfs02/M02/8B/F3/wKioL1hc_Z2CI1JfAAAN6pUpkLc271.jpg "style=" float: none; "title=" qq20161223183343.jpg "alt=" Wkiol1hc_z2ci1jfaaan6pupklc271.jpg "/>
This article is from the "World is the same" blog, please be sure to keep this source http://970076933.blog.51cto.com/9767314/1885616
C#des Encrypting and decrypting strings