栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在C#中使用AES加密

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在C#中使用AES加密

如果您只想使用内置的加密提供程序RijndaelManaged,请查看以下帮助文章(它也有一个简单的代码示例):

http://msdn.microsoft.com/zh-CN/library/system.security.cryptography.rijndaelmanaged.aspx

以防万一您急需样品,这就是所有窃的荣耀:

using System;using System.IO;using System.Security.Cryptography;namespace RijndaelManaged_Example{    class RijndaelExample    {        public static void Main()        { try {     string original = "Here is some data to encrypt!";     // Create a new instance of the RijndaelManaged      // class.  This generates a new key and initialization       // vector (IV).      using (RijndaelManaged myRijndael = new RijndaelManaged())     {         myRijndael.GenerateKey();         myRijndael.GenerateIV();         // Encrypt the string to an array of bytes.          byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);         // Decrypt the bytes to a string.          string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);         //Display the original data and the decrypted data.         Console.WriteLine("Original:   {0}", original);         Console.WriteLine("Round Trip: {0}", roundtrip);     } } catch (Exception e) {     Console.WriteLine("Error: {0}", e.Message); }        }        static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)        { // Check arguments.  if (plainText == null || plainText.Length <= 0)     throw new ArgumentNullException("plainText"); if (Key == null || Key.Length <= 0)     throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0)     throw new ArgumentNullException("IV"); byte[] encrypted; // Create an RijndaelManaged object  // with the specified key and IV.  using (RijndaelManaged rijAlg = new RijndaelManaged()) {     rijAlg.Key = Key;     rijAlg.IV = IV;     // Create a decryptor to perform the stream transform.     ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);     // Create the streams used for encryption.      using (MemoryStream msEncrypt = new MemoryStream())     {         using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))         {  using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))  {      //Write all data to the stream.      swEncrypt.Write(plainText);  }  encrypted = msEncrypt.ToArray();         }     } } // Return the encrypted bytes from the memory stream.  return encrypted;        }        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)        { // Check arguments.  if (cipherText == null || cipherText.Length <= 0)     throw new ArgumentNullException("cipherText"); if (Key == null || Key.Length <= 0)     throw new ArgumentNullException("Key"); if (IV == null || IV.Length <= 0)     throw new ArgumentNullException("IV"); // Declare the string used to hold  // the decrypted text.  string plaintext = null; // Create an RijndaelManaged object  // with the specified key and IV.  using (RijndaelManaged rijAlg = new RijndaelManaged()) {     rijAlg.Key = Key;     rijAlg.IV = IV;     // Create a decrytor to perform the stream transform.     ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);     // Create the streams used for decryption.      using (MemoryStream msDecrypt = new MemoryStream(cipherText))     {         using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))         {  using (StreamReader srDecrypt = new StreamReader(csDecrypt))  {      // Read the decrypted bytes from the decrypting stream       // and place them in a string.      plaintext = srDecrypt.ReadToEnd();  }         }     } } return plaintext;        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/364827.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号