SHARE
TWEET

Untitled

a guest Mar 21st, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// <summary>
  2.         /// aes-256-cbc 解码
  3.         /// </summary>
  4.         /// <param name="cipherData"></param>
  5.         /// <param name="keyString"></param>
  6.         /// <param name="ivString"></param>
  7.         /// <returns></returns>
  8.         public static string Decrypt(string cipherData, string keyString, string ivString)
  9.         {
  10.             byte[] key = Encoding.UTF8.GetBytes(keyString);
  11.             byte[] iv = Encoding.UTF8.GetBytes(ivString);
  12.  
  13.             try
  14.             {
  15.                 using (var rijndaelManaged =
  16.                        new RijndaelManaged { Key = key, IV = iv, Mode = CipherMode.CBC })
  17.                 using (var memoryStream =
  18.                        new MemoryStream(Convert.FromBase64String(cipherData)))
  19.                 using (var cryptoStream =
  20.                        new CryptoStream(memoryStream,
  21.                            rijndaelManaged.CreateDecryptor(key, iv),
  22.                            CryptoStreamMode.Read))
  23.                 {
  24.                     return new StreamReader(cryptoStream).ReadToEnd();
  25.                 }
  26.             }
  27.             catch (CryptographicException e)
  28.             {
  29.                 Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  30.                 return null;
  31.             }
  32.             // You may want to catch more exceptions here...
  33.         }
  34.  
  35.         /// <summary>
  36.         /// aes-256-cbc 加密
  37.         /// </summary>
  38.         /// <param name="message"></param>
  39.         /// <param name="KeyString"></param>
  40.         /// <param name="IVString"></param>
  41.         /// <returns></returns>
  42.         public static string EncryptString(string message, string KeyString, string IVString)
  43.         {
  44.             byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
  45.             byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);
  46.  
  47.             string encrypted = null;
  48.             RijndaelManaged rj = new RijndaelManaged();
  49.             rj.Key = Key;
  50.             rj.IV = IV;
  51.             rj.Mode = CipherMode.CBC;
  52.  
  53.             try
  54.             {
  55.                 MemoryStream ms = new MemoryStream();
  56.  
  57.                 using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
  58.                 {
  59.                     using (StreamWriter sw = new StreamWriter(cs))
  60.                     {
  61.                         sw.Write(message);
  62.                         sw.Close();
  63.                     }
  64.                     cs.Close();
  65.                 }
  66.                 byte[] encoded = ms.ToArray();
  67.                 encrypted = Convert.ToBase64String(encoded);
  68.  
  69.                 ms.Close();
  70.             }
  71.             catch (CryptographicException e)
  72.             {
  73.                 Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
  74.                 return null;
  75.             }
  76.             catch (UnauthorizedAccessException e)
  77.             {
  78.                 Console.WriteLine("A file error occurred: {0}", e.Message);
  79.                 return null;
  80.             }
  81.             catch (Exception e)
  82.             {
  83.                 Console.WriteLine("An error occurred: {0}", e.Message);
  84.             }
  85.             finally
  86.             {
  87.                 rj.Clear();
  88.             }
  89.             return encrypted;
  90.         }
  91.         //call
  92.            string reta = DeEncode.EncryptString("中文cnn", "12345678901234561234567890123456", "1234567890123456");
  93.             string retb = DeEncode.Decrypt(reta, "12345678901234561234567890123456", "1234567890123456");
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top