C#编程之AES加密(一)

这里我们主要参考一下官方的例程:

  1 using System;
  2 using System.IO;
  3 using System.Security.Cryptography;
  4 
  5 namespace Aes_Example
  6 {
  7     class AesExample
  8     {
  9         public static void Main()
 10         {
 11             string original = "Here is some data to encrypt!";
 12 
 13             // Create a new instance of the Aes
 14             // class.  This generates a new key and initialization 
 15             // vector (IV).
 16             using (Aes myAes = Aes.Create())
 17             {
 18 
 19                 // Encrypt the string to an array of bytes.
 20                 byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
 21 
 22                 // Decrypt the bytes to a string.
 23                 string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
 24 
 25                 //Display the original data and the decrypted data.
 26                 Console.WriteLine("Original:   {0}", original);
 27                 Console.WriteLine("Round Trip: {0}", roundtrip);
 28             }
 29             Console.ReadKey();
 30         }
 31         static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
 32         {
 33             // Check arguments.
 34             if (plainText == null || plainText.Length <= 0)
 35                 throw new ArgumentNullException("plainText");
 36             if (Key == null || Key.Length <= 0)
 37                 throw new ArgumentNullException("Key");
 38             if (IV == null || IV.Length <= 0)
 39                 throw new ArgumentNullException("IV");
 40             byte[] encrypted;
 41 
 42             // Create an Aes object
 43             // with the specified key and IV.
 44             using (Aes aesAlg = Aes.Create())
 45             {
 46                 aesAlg.Key = Key;
 47                 aesAlg.IV = IV;
 48 
 49                 // Create an encryptor to perform the stream transform.
 50                 ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
 51 
 52                 // Create the streams used for encryption.
 53                 using (MemoryStream msEncrypt = new MemoryStream())
 54                 {
 55                     using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
 56                     {
 57                         using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
 58                         {
 59                             //Write all data to the stream.
 60                             swEncrypt.Write(plainText);
 61                         }
 62                         encrypted = msEncrypt.ToArray();
 63                     }
 64                 }
 65             }
 66 
 67 
 68             // Return the encrypted bytes from the memory stream.
 69             return encrypted;
 70 
 71         }
 72 
 73         static string DecryptStringFromBytes_Aes(byte[] cipherText, byte[] Key, byte[] IV)
 74         {
 75             // Check arguments.
 76             if (cipherText == null || cipherText.Length <= 0)
 77                 throw new ArgumentNullException("cipherText");
 78             if (Key == null || Key.Length <= 0)
 79                 throw new ArgumentNullException("Key");
 80             if (IV == null || IV.Length <= 0)
 81                 throw new ArgumentNullException("IV");
 82 
 83             // Declare the string used to hold
 84             // the decrypted text.
 85             string plaintext = null;
 86 
 87             // Create an Aes object
 88             // with the specified key and IV.
 89             using (Aes aesAlg = Aes.Create())
 90             {
 91                 aesAlg.Key = Key;
 92                 aesAlg.IV = IV;
 93 
 94                 // Create a decryptor to perform the stream transform.
 95                 ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
 96 
 97                 // Create the streams used for decryption.
 98                 using (MemoryStream msDecrypt = new MemoryStream(cipherText))
 99                 {
100                     using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
101                     {
102                         using (StreamReader srDecrypt = new StreamReader(csDecrypt))
103                         {
104 
105                             // Read the decrypted bytes from the decrypting stream
106                             // and place them in a string.
107                             plaintext = srDecrypt.ReadToEnd();
108                         }
109                     }
110                 }
111 
112             }
113 
114             return plaintext;
115 
116         }
117     }
118 }
View Code

当然,具体的解说,可以参考一下官方说明,以下是例程的链接:

https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netframework-4.8

这例程,运行后,我们添加一个暂停,就可以查看运行结果,在main结束前,添加: Console.ReadKey(); ,大家也可以添加其他的参数显示出来。具体这里就一一列举了。

 

后续,我们将针对AES加密做更进一步的说明。

 

End.

谢谢.

posted on 2019-11-29 15:16  Milo_lu  阅读(457)  评论(0编辑  收藏  举报

导航