This article is a mirror article of machine translation, please click here to jump to the original article.

View: 23464|Reply: 3

[Source] c# AES encryption decryption algorithm

[Copy link]
Posted on 12/25/2015 2:30:37 PM | | |
  1. /// <summary>
  2.         /// AES加密
  3.         /// </summary>
  4.         /// <param name="encryptStr">明文</param>
  5.         /// <param name="key">密钥</param>
  6.         /// <returns></returns>
  7.         public static string Encrypt(string encryptStr,string key)
  8.         {
  9.             byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
  10.             byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);
  11.             RijndaelManaged rDel = new RijndaelManaged();
  12.             rDel.Key = keyArray;
  13.             rDel.Mode = CipherMode.ECB;
  14.             rDel.Padding = PaddingMode.PKCS7;
  15.             ICryptoTransform cTransform = rDel.CreateEncryptor();
  16.             byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  17.             return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  18.         }
  19.         /// <summary>
  20.         /// AES解密
  21.         /// </summary>
  22.         /// <param name="decryptStr">密文</param>
  23.         /// <param name="key">密钥</param>
  24.         /// <returns></returns>
  25.         public static string Decrypt(string decryptStr,string key)
  26.         {
  27.             byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key);
  28.             byte[] toEncryptArray = Convert.FromBase64String(decryptStr);
  29.             RijndaelManaged rDel = new RijndaelManaged();
  30.             rDel.Key = keyArray;
  31.             rDel.Mode = CipherMode.ECB;
  32.             rDel.Padding = PaddingMode.PKCS7;
  33.             ICryptoTransform cTransform = rDel.CreateDecryptor();
  34.             byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  35.             return UTF8Encoding.UTF8.GetString(resultArray);
  36.         }
Copy code






Previous:c# SmtpClient sends the email source code
Next:hi
Posted on 12/25/2015 3:36:13 PM |
Help me think about whether there is an SM3 algorithm, hurry up and find it
 Landlord| Posted on 12/25/2015 5:05:47 PM |
xiaoweier posted on 2015-12-25 15:36
Help me think about whether there is an SM3 algorithm, hurry up and find it

I don't understand C algorithms
 Landlord| Posted on 12/25/2015 5:17:13 PM |
key must be 16,32 bits long
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com