zoukankan      html  css  js  c++  java
  • silverlight 加密解密

    因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();
                }
            }
  • 相关阅读:
    iOS开发~UI布局(二)storyboard中autolayout和size class的使用详解
    iOS开发~UI布局(一)初探Size Class
    OC登陆界面登陆按钮动画
    Git学习 --> 个人常用命令add,commit以及push
    Git使用之设置SSH Key
    【iOS开发】多屏尺的自动适配 AutoLayout (纯代码方式)
    iOS网络检测Reachability 使用 Demo,可检测2、3、4G
    iOS提醒用户进入设置界面进行重新授权通知定位等功能
    iOS中 @synthesize 和 @dynamic 区别
    iOS 开发笔记
  • 原文地址:https://www.cnblogs.com/akingyao/p/3108319.html
Copyright © 2011-2022 走看看