加密与解密

 3    //用户自己设置
  4    private static string key_string = "1234567890123456";  //加密密钥
  5    private static string iv_string = "12345678";   //初始化向量
  6    //系统根据字符串自动生成
  7    private static byte[] key;
  8    private static byte[] iv;
  9   
 10    //初始化iv与key
 11    private static void InitKeyAndIV()
 12    {
 13        /**//*比较复杂的方式生成key与iv
 14        //3DES使用124位--192位加密,密钥的长度是16--24个字节
 15        key = new byte[16];  //124位密钥
 16        iv = new byte[8];    //64位
 17   
 18        SHA1 hasher = (SHA1)HashAlgorithm.Create("sha1");
 19        //向字符编码为字节数组
 20        byte[] key_arr = ASCIIEncoding.ASCII.GetBytes(key_string);
 21        byte[] iv_arr = ASCIIEncoding.ASCII.GetBytes(iv_string);
 22       
 23        //哈希相应的字符数组
 24        //SHA1哈希后得到20*8位
 25        //MD5哈希后得到16*8位
 26        byte[] hashed_key_arr = hasher.ComputeHash(key_arr);
 27        byte[] hashed_iv_arr = hasher.ComputeHash(iv_arr);
 28       
 29        //取前16个字符为密钥
 30        //取前8个字符为初始化向量
 31        for (int i = 0; i < 16; i++)
 32        {
 33            key[i] = hashed_key_arr[i];
 34        }
 35        for (int i = 0; i < 8; i++)
 36        {
 37            iv[i] = hashed_iv_arr[i];
 38        }
 39        */
 40        /**//*简单的方式生成key与iv*/
 41        key = System.Text.Encoding.ASCII.GetBytes(key_string);
 42        iv = System.Text.Encoding.ASCII.GetBytes(iv_string);
 43        return;
 44    }
 45   
 46    //加密算法
 47    private static string Encrypt(string data)
 48    {
 49        //先初始化 KEY 和 IV
 50        if (key == null || iv == null)
 51            InitKeyAndIV();
 52       
 53        //将要加密的数据编码为字节数组
 54        byte[] _data = System.Text.Encoding.UTF8.GetBytes(data);    //如果使用Unicode编码,支持中文加密
 55       
 56        //创建流,保存加密后的数据
 57        MemoryStream ms = new MemoryStream();
 58       
 59        //创建TripleDESCryptoServiceProvider类
 60        TripleDESCryptoServiceProvider _3des = new TripleDESCryptoServiceProvider();
 61       
 62        //创建加密流
 63        CryptoStream cs = new CryptoStream(ms, _3des.CreateEncryptor(key, iv), CryptoStreamMode.Write);
 64       
 65        //调用Write方法将加密后的结果写到 流 中
 66        cs.Write(_data, 0, _data.Length);
 67        //将数据从流写入内存(这句不能少,否则在从内存导出到字符串的时候会出现字符丢失)
 68        cs.FlushFinalBlock();
 69       
 70        //ms.ToString(); ==>将流写入字节数组中
 71        string result = Convert.ToBase64String(ms.ToArray());
 72        //string result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
 73        /**//*
 74        StringBuilder result = new StringBuilder();
 75        foreach (byte b in ms.ToArray())
 76        {
 77            result.AppendFormat("{0:X2}", b);
 78        }
 79        */
 80       
 81        ms.Close();
 82        cs.Close();
 83       
 84        return result;//.ToString();
 85       
 86    }
 87   
 88    //解密算法
 89    private static string Decrypt(string data)
 90    {
 91        if (key == null || iv == null)
 92        {
 93            InitKeyAndIV();
 94        }
 95       
 96        byte[] encrypted_data = Convert.FromBase64String(data);
 97
 98        MemoryStream ms = new MemoryStream();
 99       
100        TripleDESCryptoServiceProvider _3des = new TripleDESCryptoServiceProvider();
101       
102        CryptoStream cs = new CryptoStream(ms, _3des.CreateDecryptor(key, iv), CryptoStreamMode.Write);
103
104        cs.Write(encrypted_data, 0, encrypted_data.Length);
105        cs.FlushFinalBlock();
106        string result = System.Text.Encoding.UTF8.GetString(ms.ToArray());
107       
108        cs.Close();
109        ms.Close();
110       
111        return result;
112    } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值