96辅助游戏论坛 - 辅助工具|www.96fuzhu.com

 找回密码
 立即注册
查看: 2577|回复: 2

C#字符串加密解密

[复制链接]
发表于 2018-9-10 15:23 | 显示全部楼层 |阅读模式
1.png


  1.         //加密字符串
  2.         //失败返回0
  3.         public static string EncryptString(string plainText, string passPhrase)  
  4.         {
  5.             try
  6.             {
  7.                 byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
  8.                 byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
  9.                 PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
  10.                 byte[] keyBytes = password.GetBytes(keysize / 8);
  11.                 RijndaelManaged symmetricKey = new RijndaelManaged();
  12.                 symmetricKey.Mode = CipherMode.CBC;
  13.                 ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
  14.                 MemoryStream memoryStream = new MemoryStream();
  15.                 CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
  16.                 cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
  17.                 cryptoStream.FlushFinalBlock();
  18.                 byte[] cipherTextBytes = memoryStream.ToArray();
  19.                 memoryStream.Close();
  20.                 cryptoStream.Close();
  21.                 return Convert.ToBase64String(cipherTextBytes);
  22.             }
  23.             catch
  24.             {
  25.                 return "0";
  26.             }
  27.         }
  28.         //解密字符串
  29.         //失败返回0
  30.         public static string DecryptString(string cipherText, string passPhrase)  
  31.         {
  32.             try
  33.             {
  34.                 byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
  35.                 byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
  36.                 PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
  37.                 byte[] keyBytes = password.GetBytes(keysize / 8);
  38.                 RijndaelManaged symmetricKey = new RijndaelManaged();
  39.                 symmetricKey.Mode = CipherMode.CBC;
  40.                 ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
  41.                 MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
  42.                 CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
  43.                 byte[] plainTextBytes = new byte[cipherTextBytes.Length];
  44.                 int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
  45.                 memoryStream.Close();
  46.                 cryptoStream.Close();
  47.                 return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
  48.             }
  49.             catch
  50.             {
  51.                 return "0";
  52.             }
  53.         }

  54.         private const string initVector = "96lt1937md5.....";
  55.         private const int keysize = 256;
复制代码


C#字符串加密解密.zip

20.72 KB, 下载次数: 46

回复

使用道具 举报

发表于 2020-4-8 22:57 | 显示全部楼层
在学中,,,感谢大佬!
回复 支持 反对

使用道具 举报

发表于 2020-4-8 23:11 | 显示全部楼层
这是什么加密文?是Base64还是什么?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|96辅助游戏论坛

GMT+8, 2024-4-20 02:03 , Processed in 0.061938 second(s), 23 queries .

Powered by Discuz! X3.4

© 2016-2023 Comsenz Inc.

快速回复 返回顶部 返回列表