Silverlight中的字符串常用加解密

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace UI.Common
{
    public class Encrypt
    {
        /// <summary>
        /// 通过用户提供的密码加密字符串
        /// </summary>
        /// <param name="input">要加密的字符串</param>
        /// <param name="password">要用于加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string input, string password)
        {

            string data = input;
            byte[] utfdata = UTF8Encoding.UTF8.GetBytes(data);
            byte[] saltBytes = UTF8Encoding.UTF8.GetBytes(password);

            // 对称加密技术 高级加密标准(AES) 对称算法的管理类
            AesManaged aes = new AesManaged();

            // 使用 PBKDF2 标准 密码加密技术
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, saltBytes);

            // 设置参数
            /*
            AesManaged.BlockSize - 加密操作的块大小(单位:bit)
            AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit)
            AesManaged.KeySize - 对称算法的密钥大小(单位:bit)
            AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit)
            AesManaged.Key - 对称算法的密钥
            AesManaged.IV - 对称算法的密钥大小
            Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥
           */
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // 加密 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
            ICryptoTransform encryptTransf = aes.CreateEncryptor();

            // 输出流,可用于文件流 
            MemoryStream encryptStream = new MemoryStream();
            CryptoStream encryptor = new CryptoStream(encryptStream, encryptTransf, CryptoStreamMode.Write);
            // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
            encryptor.Write(utfdata, 0, utfdata.Length);
            encryptor.Flush();
            encryptor.Close();

            byte[] encryptBytes = encryptStream.ToArray();
            // 将加密后所得到的流转换成字节数组,再用Base64编码将其转换为字符串
            string encryptedString = Convert.ToBase64String(encryptBytes);

            return encryptedString;
        }


        /// <summary> 
        /// 通过用户提供的密码解密字符串
        /// </summary> 
        /// <param name="base64Input">待解密的字符串</param> 
        /// <param name="password">用于解密的密码</param> 
        /// <returns>解密后的字符串</returns> 
        public static string Decrypt(string base64Input, string password)
        {

            byte[] encryptBytes = Convert.FromBase64String(base64Input);
            byte[] saltBytes = Encoding.UTF8.GetBytes(password);

            // 对称加解密技术
            AesManaged aes = new AesManaged();

            // 使用 PBKDF2 标准 密码加密技术
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, saltBytes);

            // 设置参数
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // 开始解密 
            ICryptoTransform decryptTrans = aes.CreateDecryptor();

            // 解密后的输出流
            MemoryStream decryptStream = new MemoryStream();
            CryptoStream decryptor = new CryptoStream(decryptStream, decryptTrans, CryptoStreamMode.Write);
            // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
            decryptor.Write(encryptBytes, 0, encryptBytes.Length);
            decryptor.Flush();
            decryptor.Close();
            // 将解密后所得到的流转换为字符串
            byte[] decryptBytes = decryptStream.ToArray();
            string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
            return decryptedString;
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值