搞定加密解密

开发中需要一个加密解密的双向过程
MD5等Hash类型的单向加密不能符合使用的要求
所以自己写了一个类,当然还是调用 System.Security.Cryptography的方法
DESCryptoServiceProvider

加密过程
通过GetKey()、GetIV()方法产生和密钥key、随机变量IV

解密过程
用key、IV作为参数 通过SetKey()、SetIV()给Sercurity类的Key和IV负值

key、IV都是byte[]
遇到的问题:string-〉byte[]的转化
解决: Convert.FromBase64String(string s) 返回byte[]



  1 None.gif using  System;
  2 None.gif using  System.IO;
  3 None.gif using  System.Text;
  4 None.gif using  System.Security.Cryptography;
  5 None.gif
  6 None.gif namespace  mySecurity
  7 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
  8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
  9InBlock.gif    /// MySecurity 的摘要说明。
 10ExpandedSubBlockEnd.gif    /// </summary>

 11InBlock.gif    public class Security
 12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 13InBlock.gif        private  SymmetricAlgorithm mCSP = new DESCryptoServiceProvider();
 14InBlock.gif    
 15InBlock.gif        //private  SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
 16InBlock.gif        
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 18InBlock.gif        /// 加密
 19InBlock.gif        /// </summary>
 20InBlock.gif        /// <param name="Value"></param>
 21ExpandedSubBlockEnd.gif        /// <returns></returns>

 22InBlock.gif        public string EncryptString(string Value)
 23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 24InBlock.gif            ICryptoTransform ct;
 25InBlock.gif            MemoryStream ms;
 26InBlock.gif            CryptoStream cs;
 27InBlock.gif            byte[] byt;
 28InBlock.gif
 29InBlock.gif            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
 30InBlock.gif
 31InBlock.gif            byt = Encoding.UTF8.GetBytes(Value);
 32InBlock.gif
 33InBlock.gif            ms = new MemoryStream();
 34InBlock.gif            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
 35InBlock.gif            cs.Write(byt, 0, byt.Length);
 36InBlock.gif            cs.FlushFinalBlock();
 37InBlock.gif    
 38InBlock.gif            cs.Close();
 39InBlock.gif
 40InBlock.gif            return Convert.ToBase64String(ms.ToArray());
 41ExpandedSubBlockEnd.gif        }

 42InBlock.gif
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 44InBlock.gif        /// 解密
 45InBlock.gif        /// </summary>
 46InBlock.gif        /// <param name="Value"></param>
 47ExpandedSubBlockEnd.gif        /// <returns></returns>

 48InBlock.gif        public string DecryptString(string Value)
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 50InBlock.gif            ICryptoTransform ct;
 51InBlock.gif            MemoryStream ms;
 52InBlock.gif            CryptoStream cs;
 53InBlock.gif            byte[] byt;
 54InBlock.gif
 55InBlock.gif            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
 56InBlock.gif
 57InBlock.gif            byt = Convert.FromBase64String(Value);
 58InBlock.gif
 59InBlock.gif            ms = new MemoryStream();
 60InBlock.gif            cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
 61InBlock.gif            cs.Write(byt, 0, byt.Length);
 62InBlock.gif            cs.FlushFinalBlock();
 63InBlock.gif
 64InBlock.gif            cs.Close();
 65InBlock.gif
 66InBlock.gif            return Encoding.UTF8.GetString(ms.ToArray());
 67ExpandedSubBlockEnd.gif        }

 68InBlock.gif        
 69InBlock.gif        
 70ExpandedSubBlockStart.gifContractedSubBlock.gif        public Security()dot.gif{}
 71InBlock.gif
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        设置、获取Key、IV#region 设置、获取Key、IV
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 74InBlock.gif        /// 获取产生的Key值,加密过程之前需调用该方法
 75InBlock.gif        /// </summary>
 76ExpandedSubBlockEnd.gif        /// <returns></returns>

 77InBlock.gif        public string GetKey()
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 79InBlock.gif            mCSP.GenerateKey();
 80InBlock.gif            return Convert.ToBase64String(mCSP.Key);
 81ExpandedSubBlockEnd.gif        }

 82InBlock.gif
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 84InBlock.gif        /// 获取产生的随机变量值,加密过程之前需调用该方法
 85InBlock.gif        /// </summary>
 86ExpandedSubBlockEnd.gif        /// <returns></returns>

 87InBlock.gif        public string GetIV()
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            mCSP.GenerateIV();
 90InBlock.gif            return    Convert.ToBase64String(mCSP.IV);
 91ExpandedSubBlockEnd.gif        }

 92InBlock.gif
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 94InBlock.gif        /// 设置Key的值,解密过程之前需要调用
 95InBlock.gif        /// </summary>
 96InBlock.gif        /// <param name="key"></param>
 97ExpandedSubBlockEnd.gif        /// <returns></returns>

 98InBlock.gif        public string SetKey(string key)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100InBlock.gif            mCSP.Key    =    Convert.FromBase64String(key);
101InBlock.gif            return Convert.ToBase64String(mCSP.Key);
102ExpandedSubBlockEnd.gif        }

103ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
104InBlock.gif        /// 设置随机变量IV的值,解密过程之前需要调用
105InBlock.gif        /// </summary>
106InBlock.gif        /// <param name="IV"></param>
107ExpandedSubBlockEnd.gif        /// <returns></returns>

108InBlock.gif        public string SetIV(string IV)
109ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
110InBlock.gif            mCSP.IV = Convert.FromBase64String(IV);
111InBlock.gif            return    Convert.ToBase64String(mCSP.IV);
112ExpandedSubBlockEnd.gif        }

113ExpandedSubBlockEnd.gif        #endregion

114InBlock.gif
115InBlock.gif
116ExpandedSubBlockEnd.gif    }

117ExpandedBlockEnd.gif}

118 None.gif

最后编译成dll,方便重用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值