因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(); } }