• 热门专题

WinPhone7手机中的AES加密,解密算法

作者:厚德载物  发布日期:2012-07-05 19:56:46
Tag标签:WinPhone7  AES加密  解密算法  
  • 因工作需要,需要在winphone7手机上实现AES的加密,解密算法。经过自己的查找,终于找到了一个可以实现的代码,来和大家共享,代码如下:
    using System;
    
    using System.Security.Cryptography;
    
    using System.Text;
    
    using System.IO;
    
    
    
    namespace SheenClient.classes.eventhandler
    
    {
    
        /// <summary>
    
    
    
        /// AES解密算法类
    
    
    
        /// </summary>
    
    
    
        public class AESEncryption
    
        {
    
            /// <summary>
    
    
    
            /// 默认密钥向量
    
    
    
            /// </summary>
    
    
    
            private static string salt = "PasswordSalt";
    
    
    
            /// <summary>
    
    
    
            /// 进行AES加密算法
    
    
    
            /// </summary>
    
    
    
            /// <param name="dataToEncrypt">要进行加密的字符串</param>
    
    
    
            /// <param name="key">进行加密的键</param>
    
    
    
            /// <returns>加密后的字符串</returns>
    
    
    
            public static string AesEncrypt(string dataToEncrypt, string key)
    
            {
    
                AesManaged aes = null;
    
                MemoryStream memoryStream = null;
    
                CryptoStream cryptoStream = null;
    
    
    
                try
    
                {
    
                    //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator 
    
    
    
                    Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(key, Encoding.UTF8.GetBytes(salt));
    
    
    
                    //Create AES algorithm with 256 bit key and 128-bit block size 
    
    
    
                    aes = new AesManaged();
    
                    aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
    
                    aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);
    
    
    
                    //Create Memory and Crypto Streams 
    
    
    
                    memoryStream = new MemoryStream();
    
                    cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
    
    
    
                    //Encrypt Data 
    
    
    
                    byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
    
                    cryptoStream.Write(data, 0, data.Length);
    
                    cryptoStream.FlushFinalBlock();
    
    
    
                    //Return Base 64 String 
    
    
    
                    return Convert.ToBase64String(memoryStream.ToArray());
    
                }
    
                finally
    
                {
    
                    if (cryptoStream != null)
    
                        cryptoStream.Close();
    
    
    
                    if (memoryStream != null)
    
                        memoryStream.Close();
    
    
    
                    if (aes != null)
    
                        aes.Clear();
    
                }
    
            }
    
    
    
            /// <summary>
    
    
    
            /// 进行AES解密
    
    
    
            /// </summary>
    
    
    
            /// <param name="dataToDecrypt">要进行解密的字符串</param>
    
    
    
            /// <param name="key">解密使用的键</param>
    
    
    
            /// <returns>解密后的明文字符串</returns>
    
    
    
            public static string AesDecrypt(string dataToDecrypt, string key)
    
            {
    
                AesManaged aes = null;
    
                MemoryStream memoryStream = null;
    
                CryptoStream cryptoStream = null;
    
    
    
                try
    
                {
    
                    //Generate a Key based on a Password, Salt and HMACSHA1 pseudo-random number generator 
    
    
    
                    Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(key, Encoding.UTF8.GetBytes(salt));
    
    
    
                    //Create AES algorithm with 256 bit key and 128-bit block size 
    
    
    
                    aes = new AesManaged();
    
                    aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
    
                    aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);
    
    
    
                    //Create Memory and Crypto Streams 
    
    
    
                    memoryStream = new MemoryStream();
    
                    cryptoStream = new CryptoStream(memoryStream, aes.CreateDecryptor(), CryptoStreamMode.Write);
    
    
    
                    //Decrypt Data 
    
    
    
                    byte[] data = Convert.FromBase64String(dataToDecrypt);
    
                    cryptoStream.Write(data, 0, data.Length);
    
                    cryptoStream.FlushFinalBlock();
    
    
    
                    //Return Decrypted String 
    
    
    
                    byte[] decryptBytes = memoryStream.ToArray();
    
                    return Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
    
                }
    
                finally
    
                {
    
                    if (cryptoStream != null)
    
                        cryptoStream.Close();
    
    
    
                    if (memoryStream != null)
    
                        memoryStream.Close();
    
    
    
                    if (aes != null)
    
                        aes.Clear();
    
                }
    
            } 
    
        }
    
    }
    
    

    直接调用,就可以进行字符串的加密,解密了。

延伸阅读:

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规