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();
                }
            }
  • 相关阅读:
    Navicat for MySQL破解版安装
    LACP学习笔记
    MUX VLAN
    Beyond Compare用于文件比较还是蛮好的选择,特别是我们程序袁用于比较两个项目的时候,最初使用的是Beyond Compare3一直用着挺好的,几年前更新了版本4,用着用着就提示试用期30天已过期,于是我尝试如下步骤:
    思科交换机如何进行备份与还原?
    vSphere ESXi 6.7 注册码(有效)
    VMware ESXi 6.7密码正确不能登录
    Esxi 6.5 6.7的root密码经过一段时间就不可用的解决方法
    Windows Server 2012 R2 安装密钥
    ubuntu 16 添加多个IP
  • 原文地址:https://www.cnblogs.com/akingyao/p/3108319.html
Copyright © 2011-2022 走看看