. Net (c #) encryption and decryption of Aes and Des,

Source: Internet
Author: User

. Net (c #) encryption and decryption of Aes and Des,
. Net (c #) encryption and decryption tool class:

/// <Summary> ///. net encryption and decryption help class // </summary> public class NetCryptoHelper {# region des implementation /// <summary> // Des default key vector /// </summary> public static byte [] DesIv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; /// <summary> // The Des encryption key must be 8 bits // </summary> public const string encryption ey = "eyey8w "; /// <summary> /// obtain the Des8 bit key // </summary> /// <param name = "key"> Des key string </param> // /<returns> Des8 bit key </returns> static byte [] GetDesKey (string key) {if (string. isNullOrEmpty (key) {throw new ArgumentNullException ("key", "Des key cannot be blank");} if (key. length> 8) {key = key. substring (0, 8);} if (key. length <8) {// less than 8 fill key = key. padRight (8, '0');} return Encoding. UTF8.GetBytes (key );} /// <summary> // Des encryption // </summary> /// <param name = "source"> source string </param> /// <param name = "key"> des key, the length must be 8 bits </param> /// <param name = "iv"> key vector </param> /// <returns> the encrypted string </returns> public static string EncryptDes (string source, string key, byte [] iv) {using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider () {byte [] rgbKeys = getpolicey (key), rgbIvs = iv, inputByteArray = Encoding. UTF8.GetBytes (source); using (MemoryStream memoryStream = new MemoryStream () {using (CryptoStream cryptoStream = new CryptoStream (memoryStream, desProvider. createEncryptor (rgbKeys, rgbIvs), CryptoStreamMode. write) {cryptoStream. write (inputByteArray, 0, inputByteArray. length); cryptoStream. flushFinalBlock (); // 1. first return Convert. toBase64String (memoryStream. toArray (); // 2. // StringBuilder result = new StringBuilder (); // foreach (byte B in memoryStream. toArray () // {// result. appendFormat ("{0: X2}", B); //} // return result. toString ();}}}} /// <summary> // Des decryption // </summary> /// <param name = "source"> source string </param> /// <param name = "key"> des key, the length must be 8 bits </param> /// <param name = "iv"> key vector </param> /// <returns> the decrypted string </returns> public static string DecryptDes (string source, string key, byte [] iv) {using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider () {byte [] rgbKeys = getpolicey (key), rgbIvs = iv, inputByteArray = Convert. fromBase64String (source); using (MemoryStream memoryStream = new MemoryStream () {using (CryptoStream cryptoStream = new CryptoStream (memoryStream, desProvider. createDecryptor (rgbKeys, rgbIvs), CryptoStreamMode. write) {cryptoStream. write (inputByteArray, 0, inputByteArray. length); cryptoStream. flushFinalBlock (); return Encoding. UTF8.GetString (memoryStream. toArray ());}}}} # endregion # region aes implementation // <summary> // Aes encryption key must be 32 bits // </summary> public static string AesKey = "asekey32w "; /// <summary> /// obtain the Aes32-bit key // </summary> /// <param name = "key"> Aes key string </param> // /<returns> Aes32-bit key </returns> static byte [] GetAesKey (string key) {if (string. isNullOrEmpty (key) {throw new ArgumentNullException ("key", "Aes key cannot be blank");} if (key. length <32) {// less than 32 complete key = key. padRight (32, '0');} if (key. length> 32) {key = key. substring (0, 32);} return Encoding. UTF8.GetBytes (key );} /// <summary> // Aes encryption // </summary> /// <param name = "source"> source string </param> /// <param name = "key"> aes key, the length must be 32 characters </param> /// <returns> encrypted string </returns> public static string EncryptAes (string source, string key) {using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider () {aesProvider. key = GetAesKey (key); aesProvider. mode = CipherMode. ECB; aesProvider. padding = PaddingMode. PKCS7; using (ICryptoTransform cryptoTransform = aesProvider. createEncryptor () {byte [] inputBuffers = Encoding. UTF8.GetBytes (source); byte [] results = cryptoTransform. transformFinalBlock (inputBuffers, 0, inputBuffers. length); aesProvider. clear (); aesProvider. dispose (); return Convert. toBase64String (results, 0, results. length );}}} /// <summary> // Aes decryption // </summary> /// <param name = "source"> source string </param> /// <param name = "key"> aes key, the length must be 32 characters </param> /// <returns> decrypted string </returns> public static string DecryptAes (string source, string key) {using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider () {aesProvider. key = GetAesKey (key); aesProvider. mode = CipherMode. ECB; aesProvider. padding = PaddingMode. PKCS7; using (ICryptoTransform cryptoTransform = aesProvider. createDecryptor () {byte [] inputBuffers = Convert. fromBase64String (source); byte [] results = cryptoTransform. transformFinalBlock (inputBuffers, 0, inputBuffers. length); aesProvider. clear (); return Encoding. UTF8.GetString (results) ;}}# endregion}
Debug and run:
Class Program {static void Main (string [] args) {string plainText = "", encryptString = NetCryptoHelper. encryptDes (plainText, NetCryptoHelper. cipher ey, NetCryptoHelper. desIv); Console. writeLine ("des encrypted string: {0}", plainText); Console. writeLine ("des encrypted string: {0}", encryptString); Console. writeLine ("des decrypted string: {0}", NetCryptoHelper. decryptDes (encryptString, NetCryptoHelper. cipher ey, NetCryptoHelper. desIv); Console. writeLine ("----------- split line -----------"); Console. writeLine ("aes pre-encryption string: {0}", plainText); encryptString = NetCryptoHelper. encryptAes (plainText, NetCryptoHelper. aesKey); Console. writeLine ("aes encrypted string: {0}", encryptString); Console. writeLine ("aes decrypted string: {0}", NetCryptoHelper. decryptAes (encryptString, NetCryptoHelper. aesKey); Console. readKey ();}}

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.