This article describes the use of DES and AES to implement encryption and decryption functions in C#. Share it for your reference, as follows:
using System; using ; using ; using ; namespace MyCryptography { /// <summary> /// DES encryption and decryption /// </summary> public class DES { /// <summary> /// Get the key /// </summary> private static string Key { get { return @"P@+#wG+Z"; } } /// <summary> /// Get vector /// </summary> private static string IV { get { return @"L%n67}G\Mk@k%:~Y"; } } /// <summary> /// DES encryption /// </summary> /// <param name="plainStr">Plaintext string</param> /// <returns>ciphertext</returns> public static string DESEncrypt(string plainStr) { byte[] bKey = Encoding.(Key); byte[] bIV = Encoding.(IV); byte[] byteArray = Encoding.(plainStr); string encrypt = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, (bKey, bIV), )) { (byteArray, 0, ); (); encrypt = Convert.ToBase64String(()); } } } catch { } (); return encrypt; } /// <summary> /// DES decryption /// </summary> /// <param name="encryptStr">Secret text string</param> /// <returns>Plain text</returns> public static string DESDecrypt(string encryptStr) { byte[] bKey = Encoding.(Key); byte[] bIV = Encoding.(IV); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, (bKey, bIV), )) { (byteArray, 0, ); (); decrypt = Encoding.(()); } } } catch { } (); return decrypt; } } /// <summary> /// AES encryption and decryption /// </summary> public class AES { /// <summary> /// Get the key /// </summary> private static string Key { get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; } } /// <summary> /// Get vector /// </summary> private static string IV { get { return @"L+\~f4,Ir)b$=pkf"; } } /// <summary> /// AES encryption /// </summary> /// <param name="plainStr">Plaintext string</param> /// <returns>ciphertext</returns> public static string AESEncrypt(string plainStr) { byte[] bKey = Encoding.(Key); byte[] bIV = Encoding.(IV); byte[] byteArray = Encoding.(plainStr); string encrypt = null; Rijndael aes = (); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, (bKey, bIV), )) { (byteArray, 0, ); (); encrypt = Convert.ToBase64String(()); } } } catch { } (); return encrypt; } /// <summary> /// AES encryption /// </summary> /// <param name="plainStr">Plaintext string</param> /// <param name="returnNull">Whether to return null when encryption fails, false return </param> /// <returns>ciphertext</returns> public static string AESEncrypt(string plainStr, bool returnNull) { string encrypt = AESEncrypt(plainStr); return returnNull ? encrypt : (encrypt == null ? : encrypt); } /// <summary> /// AES decryption /// </summary> /// <param name="encryptStr">Secret text string</param> /// <returns>Plain text</returns> public static string AESDecrypt(string encryptStr) { byte[] bKey = Encoding.(Key); byte[] bIV = Encoding.(IV); byte[] byteArray = Convert.FromBase64String(encryptStr); string decrypt = null; Rijndael aes = (); try { using (MemoryStream mStream = new MemoryStream()) { using (CryptoStream cStream = new CryptoStream(mStream, (bKey, bIV), )) { (byteArray, 0, ); (); decrypt = Encoding.(()); } } } catch { } (); return decrypt; } /// <summary> /// AES decryption /// </summary> /// <param name="encryptStr">Secret text string</param> /// <param name="returnNull">Whether to return null when decryption fails, false return </param> /// <returns>Plain text</returns> public static string AESDecrypt(string encryptStr, bool returnNull) { string decrypt = AESDecrypt(encryptStr); return returnNull ? decrypt : (decrypt == null ? : decrypt); } } }
PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:
Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools./password/txt_encode
MD5 online encryption tool:
http://tools./password/CreateMD5Password
Online hash/hash algorithm encryption tool:
http://tools./password/hash_encrypt
Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools./password/hash_md5_sha
Online sha1/sha224/sha256/sha384/sha512 encryption tool:
http://tools./password/sha_encode
For more information about C#, you can also view the special topic of this site: "Summary of C# Encryption and Decryption Algorithm and Skills》、《Summary of C# form operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.