Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2538775
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: 嵌入式

2012-07-05 11:11:42

    因工作需要,需要在winphone7手机上实现AES的加密,解密算法。经过自己的查找,终于找到了一个可以实现的代码,来和大家共享,代码如下:

点击(此处)折叠或打开

  1. using System;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using System.IO;

  5. namespace SheenClient.classes.eventhandler
  6. {
  7.     ///
  8.     /// AES解密算法类
  9.     ///
  10.     public class AESEncryption
  11.     {
  12.         ///
  13.         /// 默认密钥向量
  14.         ///
  15.         private static string salt = "PasswordSalt";

  16.         ///
  17.         /// 进行AES加密算法
  18.         ///
  19.         /// 要进行加密的字符串
  20.         /// 进行加密的键
  21.         /// 加密后的字符串
  22.         public static string AesEncrypt(string dataToEncrypt, string key)
  23.         {
  24.             AesManaged aes = null;
  25.             MemoryStream memoryStream = null;
  26.             CryptoStream cryptoStream = null;

  27.             try
  28.             {
  29.                 //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator
  30.                 Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(key, Encoding.UTF8.GetBytes(salt));

  31.                 //Create AES algorithm with 256 bit key and 128-bit block size
  32.                 aes = new AesManaged();
  33.                 aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
  34.                 aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);

  35.                 //Create Memory and Crypto Streams
  36.                 memoryStream = new MemoryStream();
  37.                 cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);

  38.                 //Encrypt Data
  39.                 byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
  40.                 cryptoStream.Write(data, 0, data.Length);
  41.                 cryptoStream.FlushFinalBlock();

  42.                 //Return Base 64 String
  43.                 return Convert.ToBase64String(memoryStream.ToArray());
  44.             }
  45.             finally
  46.             {
  47.                 if (cryptoStream != null)
  48.                     cryptoStream.Close();

  49.                 if (memoryStream != null)
  50.                     memoryStream.Close();

  51.                 if (aes != null)
  52.                     aes.Clear();
  53.             }
  54.         }

  55.         ///
  56.         /// 进行AES解密
  57.         ///
  58.         /// 要进行解密的字符串
  59.         /// 解密使用的键
  60.         /// 解密后的明文字符串
  61.         public static string AesDecrypt(string dataToDecrypt, string key)
  62.         {
  63.             AesManaged aes = null;
  64.             MemoryStream memoryStream = null;
  65.             CryptoStream cryptoStream = null;

  66.             try
  67.             {
  68.                 //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator
  69.                 Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(key, Encoding.UTF8.GetBytes(salt));

  70.                 //Create AES algorithm with 256 bit key and 128-bit block size
  71.                 aes = new AesManaged();
  72.                 aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
  73.                 aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);

  74.                 //Create Memory and Crypto Streams
  75.                 memoryStream = new MemoryStream();
  76.                 cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);

  77.                 //Decrypt Data
  78.                 byte[] data = Convert.FromBase64String(dataToDecrypt);
  79.                 cryptoStream.Write(data, 0, data.Length);
  80.                 cryptoStream.FlushFinalBlock();

  81.                 //Return Decrypted String
  82.                 byte[] decryptBytes = memoryStream.ToArray();
  83.                 return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
  84.             }
  85.             finally
  86.             {
  87.                 if (cryptoStream != null)
  88.                     cryptoStream.Close();

  89.                 if (memoryStream != null)
  90.                     memoryStream.Close();

  91.                 if (aes != null)
  92.                     aes.Clear();
  93.             }
  94.         }
  95.     }
  96. }
直接调用,就可以进行字符串的加密,解密了。
阅读(3327) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~