博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
silverlight中AES加密RijndaelManaged的使用
阅读量:5844 次
发布时间:2019-06-18

本文共 2099 字,大约阅读时间需要 6 分钟。

因silverlight不支持DES(X509)加密,服务端与WPF本来是用X509加解密的,

因为要兼容SL,所以改用AES来完成,但是不幸的是在silverlight中虽然支持AES却不支持RijndaelManaged类,

只能使用AesManaged类来实现了,具体实现代码如下:

/// <summary>
        /// 使用AES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密字符串</param>
        /// <param name="encryptKey">加密密匙</param>
        /// <param name="salt">盐</param>
        /// <returns>加密结果,加密失败则返回源串</returns>
        public static string EncryptAES(string encryptString, string encryptKey, string salt)
        {
            AesManaged aes = null;
            MemoryStream ms = null;
            CryptoStream cs = null;
            try
            {
                Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(encryptKey, Encoding.UTF8.GetBytes(salt));
                aes = new AesManaged();
                aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
                aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write);
                byte[] data = Encoding.UTF8.GetBytes(encryptString);
                cs.Write(data, 0, data.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch
            {
                return encryptString;
            }
            finally
            {
                if (cs != null)
                    cs.Close();
                if (ms != null)
                    ms.Close();
                if (aes != null)
                    aes.Clear();
            }
        }
        /// <summary>
        /// 使用AES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密字符串</param>
        /// <param name="decryptKey">解密密匙</param>
        /// <param name="salt">盐</param>
        /// <returns>解密结果,解谜失败则返回源串</returns>
        public static string DecryptAES(string decryptString, string decryptKey, string salt)
        {
            AesManaged aes = null;
            MemoryStream ms = null;
            CryptoStream cs = null;
            try
            {
                Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(decryptKey, Encoding.UTF8.GetBytes(salt));
                aes = new AesManaged();
                aes.Key = rfc2898.GetBytes(aes.KeySize / 8);
                aes.IV = rfc2898.GetBytes(aes.BlockSize / 8);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write);
                byte[] data = Convert.FromBase64String(decryptString);
                cs.Write(data, 0, data.Length);
                cs.FlushFinalBlock();
                return Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
            }
            catch
            {
                return decryptString;
            }
            finally
            {
                if (cs != null)
                    cs.Close();
                if (ms != null)
                    ms.Close();
                if (aes != null)
                    aes.Clear();
            }
        }

转载地址:http://dwqcx.baihongyu.com/

你可能感兴趣的文章
mpls--通过全局路由表和静态路由的因特网接入
查看>>
WPF-21:WPF实现仿安卓的图案密码键盘(改进)
查看>>
Reids实战(7)数据类型五sorted sets
查看>>
PHP - 如何解析HTML文件
查看>>
持续集成之代码质量管理-Sonar [三]
查看>>
电脑管理员密码忘记了,如何解决
查看>>
MySQL5.6.26升级到MySQL5.7.9实战方案
查看>>
非域环境下安装并配置Project Server 2007(二)
查看>>
Hibernate主键生成种类
查看>>
【小窍门tip】oracle sequence 修改增量值
查看>>
MariaDB 10之并行复制--延迟测试结果
查看>>
为-微软-重写-TechNet Library-中-Microsoft Lync Server 2010
查看>>
统一沟通-技巧-10-Lync-公网证书-Go Daddy-Buy
查看>>
全媒体平台可以适度超前
查看>>
ExtJS 4.1有什么值得期待?
查看>>
ora-12545连接错误解决一例
查看>>
初窥QuickTest脚本录制
查看>>
甘肃银行借助HDIM打造综合数据保护平台
查看>>
linux命令格式,获取帮助及其目录结构简要理解
查看>>
22条你不知道的微营销干货
查看>>