关于 Rijndael 加密

Posted 夜黔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于 Rijndael 加密相关的知识,希望对你有一定的参考价值。

MSDN https://msdn.microsoft.com/zh-cn/library/system.security.cryptography.rijndaelmanaged(v=vs.110).aspx 类的相关属性和方法。

在此我将MSDN里给的用法做放在这里以便以后直接使用

  1 using System;
  2 using System.IO;
  3 using System.Security.Cryptography;
  4 
  5 namespace RijndaelManaged_Example
  6 {
  7     class RijndaelExample
  8     {
  9         public static void Main()
 10         {
 11             Start("Here is some data to encrypt!");
 12 
 13             Console.ReadKey();
 14         }
 15         private static void Start(string original)
 16         {
 17             try
 18             {
 19                 using (RijndaelManaged myRijndael = new RijndaelManaged())
 20                 {
 21                     myRijndael.GenerateKey();//随机生成 key
 22                     myRijndael.GenerateIV();//随机生成 iv
 23                     // 把字符串加密,返回一个 byte[]
 24                     byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);
 25                     // 把之前得到的字节数组解码
 26                     string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);
 27 
 28                     //Display the original data and the decrypted data.
 29                     Console.WriteLine("Original:   {0}", original);
 30                     Console.WriteLine("Round Trip: {0}", roundtrip);
 31                 }
 32             }
 33             catch (Exception e)
 34             {
 35                 Console.WriteLine("Error: {0}", e.Message);
 36             }
 37         }
 38 
 39         static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
 40         {
 41             // Check arguments.
 42             if (plainText == null || plainText.Length <= 0)
 43                 throw new ArgumentNullException("plainText");
 44             if (Key == null || Key.Length <= 0)
 45                 throw new ArgumentNullException("Key");
 46             if (IV == null || IV.Length <= 0)
 47                 throw new ArgumentNullException("IV");
 48             byte[] encrypted;
 49             // Create an RijndaelManaged object
 50             // with the specified(规定的) key and IV.
 51             using (RijndaelManaged rijAlg = new RijndaelManaged())
 52             {
 53                 rijAlg.Key = Key;
 54                 rijAlg.IV = IV;
 55 
 56                 // Create a decrytor to perform the stream transform.
 57                 ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
 58 
 59                 // Create the streams used for encryption.
 60                 using (MemoryStream msEncrypt = new MemoryStream())
 61                 {
 62                     using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
 63                     {
 64                         using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
 65                         {
 66 
 67                             //Write all data to the stream.
 68                             swEncrypt.Write(plainText);
 69                         }
 70                         encrypted = msEncrypt.ToArray();
 71                     }
 72                 }
 73             }
 74             return encrypted;
 75         }
 76 
 77         static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
 78         {
 79             // Check arguments.
 80             if (cipherText == null || cipherText.Length <= 0)
 81                 throw new ArgumentNullException("cipherText");
 82             if (Key == null || Key.Length <= 0)
 83                 throw new ArgumentNullException("Key");
 84             if (IV == null || IV.Length <= 0)
 85                 throw new ArgumentNullException("IV");
 86 
 87             // Declare the string used to hold
 88             // the decrypted text.
 89             string plaintext = null;
 90 
 91             // Create an RijndaelManaged object
 92             // with the specified key and IV.
 93             using (RijndaelManaged rijAlg = new RijndaelManaged())
 94             {
 95                 rijAlg.Key = Key;
 96                 rijAlg.IV = IV;
 97 
 98                 // Create a decrytor to perform the stream transform.
 99                 ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
100 
101                 // Create the streams used for decryption.
102                 using (MemoryStream msDecrypt = new MemoryStream(cipherText))
103                 {
104                     using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
105                     {
106                         using (StreamReader srDecrypt = new StreamReader(csDecrypt))
107                         {
108 
109                             // Read the decrypted bytes from the decrypting stream
110                             // and place them in a string.
111                             plaintext = srDecrypt.ReadToEnd();
112                         }
113                     }
114                 }
115 
116             }
117 
118             return plaintext;
119 
120         }
121     }
122 }

 

以上是关于关于 Rijndael 加密的主要内容,如果未能解决你的问题,请参考以下文章

Rijndael-256 PHP 中的加密解密

Delphi DEC 库 (Rijndael) 加密

如何使用Python进行Rijndael方式的加密解密

是否有像 PHP 一样进行 Rijndael 256 位加密的 JavaScript 库?

如何使用Rijndael加密与.Net Core类库

解密在 PHP 中使用 MCRYPT_RIJNDAEL_256 加密的 Python 字符串