使用TripleDES进行加密和解密的方法

 

ContractedBlock.gif ExpandedBlockStart.gif
  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.Text;
  4None.gifusing System.IO;
  5None.gifusing System.Security.Cryptography;
  6None.gif
  7None.gifnamespace Sun.Yi.Csharp.Security
  8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 10InBlock.gif    /// 使用TripleDES处理加密和解密的方法
 11ExpandedSubBlockEnd.gif    /// </summary>

 12InBlock.gif    class TripleDESProcess
 13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 14ContractedSubBlock.gifExpandedSubBlockStart.gif        内部私有变量#region 内部私有变量
 15InBlock.gif
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 17InBlock.gif        /// 指定的密钥(Key)
 18ExpandedSubBlockEnd.gif        /// </summary>

 19InBlock.gif        private byte[] bytDesKey;
 20InBlock.gif
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 22InBlock.gif        /// 初始化向量(IV)
 23ExpandedSubBlockEnd.gif        /// </summary>

 24InBlock.gif        private byte[] bytDesIV;
 25InBlock.gif
 26ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 27InBlock.gif        /// 空白字节的填充字符
 28ExpandedSubBlockEnd.gif        /// </summary>

 29InBlock.gif        private char chrFillChar;
 30InBlock.gif
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 32InBlock.gif        /// 发生错误后的错误信息
 33ExpandedSubBlockEnd.gif        /// </summary>

 34InBlock.gif        private string strErrorMessage;
 35InBlock.gif
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 37InBlock.gif        /// 已经被加密的文字长度(需要解密的文字内容,长度必须小于该值)
 38ExpandedSubBlockEnd.gif        /// </summary>

 39InBlock.gif        private const int EncryptLength = 56;
 40ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 41InBlock.gif        /// 没有被加密的文字长度(需要加密的文字内容,长度必须小于该值)
 42ExpandedSubBlockEnd.gif        /// </summary>

 43InBlock.gif        private const int DecryptLength = 50;
 44ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 45InBlock.gif        /// 密钥和初始化向量的长度(密钥和初始化向量的长度必须等于该值)
 46ExpandedSubBlockEnd.gif        /// </summary>

 47InBlock.gif        private const int KeyLength = 16;
 48InBlock.gif
 49ExpandedSubBlockEnd.gif        #endregion

 50InBlock.gif
 51ContractedSubBlock.gifExpandedSubBlockStart.gif        公共属性#region 公共属性
 52InBlock.gif
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 54InBlock.gif        /// 指定的密钥(Key)
 55ExpandedSubBlockEnd.gif        /// </summary>

 56InBlock.gif        public string DesKey
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58InBlock.gif            get
 59ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 60InBlock.gif                return Encoding.ASCII.GetString(this.bytDesKey);
 61ExpandedSubBlockEnd.gif            }

 62InBlock.gif            set
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 64InBlock.gif                this.bytDesKey = Encoding.ASCII.GetBytes(value);
 65ExpandedSubBlockEnd.gif            }

 66ExpandedSubBlockEnd.gif        }

 67InBlock.gif
 68ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 69InBlock.gif        /// 初始化向量(IV)
 70ExpandedSubBlockEnd.gif        /// </summary>

 71InBlock.gif        public string DesIV
 72ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 73InBlock.gif            get
 74ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 75InBlock.gif                return Encoding.ASCII.GetString(this.bytDesIV);
 76ExpandedSubBlockEnd.gif            }

 77InBlock.gif            set
 78ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 79InBlock.gif                this.bytDesIV = Encoding.ASCII.GetBytes(value);
 80ExpandedSubBlockEnd.gif            }

 81ExpandedSubBlockEnd.gif        }

 82InBlock.gif
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 84InBlock.gif        /// 空白字节的填充字符
 85ExpandedSubBlockEnd.gif        /// </summary>

 86InBlock.gif        public char FillChar
 87ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 88InBlock.gif            get
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 90InBlock.gif                return this.chrFillChar;
 91ExpandedSubBlockEnd.gif            }

 92InBlock.gif            set
 93ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 94InBlock.gif                this.chrFillChar = value;
 95ExpandedSubBlockEnd.gif            }

 96ExpandedSubBlockEnd.gif        }

 97InBlock.gif
 98ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 99InBlock.gif        /// 发生错误后的错误信息
100ExpandedSubBlockEnd.gif        /// </summary>

101InBlock.gif        public string ErrorMessage
102ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
103InBlock.gif            get
104ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
105InBlock.gif                return this.strErrorMessage;
106ExpandedSubBlockEnd.gif            }

107ExpandedSubBlockEnd.gif        }

108InBlock.gif
109ExpandedSubBlockEnd.gif        #endregion

110InBlock.gif
111ContractedSubBlock.gifExpandedSubBlockStart.gif        构造函数#region 构造函数
112InBlock.gif
113ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
114InBlock.gif        /// TripleDESProcess的构造函数
115InBlock.gif        /// </summary>
116InBlock.gif        /// <param name="desKey">指定的密钥(Key)</param>
117ExpandedSubBlockEnd.gif        /// <param name="desIV">初始化向量(IV)</param>

118InBlock.gif        public TripleDESProcess(string desKey, string desIV)
119InBlock.gif            : this(desKey, desIV, '\r')
120ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
121ExpandedSubBlockEnd.gif        }

122InBlock.gif
123ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
124InBlock.gif        /// TripleDESProcess的构造函数
125InBlock.gif        /// </summary>
126InBlock.gif        /// <param name="desKey">指定的密钥(Key)</param>
127InBlock.gif        /// <param name="desIV">初始化向量(IV)</param>
128ExpandedSubBlockEnd.gif        /// <param name="fillChar">空白字节的填充字符</param>

129InBlock.gif        public TripleDESProcess(string desKey, string desIV, char fillChar)
130ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
131InBlock.gif            this.bytDesKey = Encoding.ASCII.GetBytes(desKey);
132InBlock.gif            this.bytDesIV = Encoding.ASCII.GetBytes(desIV);
133InBlock.gif            this.chrFillChar = fillChar;
134InBlock.gif            this.strErrorMessage = "";
135ExpandedSubBlockEnd.gif        }

136InBlock.gif
137ExpandedSubBlockEnd.gif        #endregion

138InBlock.gif
139ContractedSubBlock.gifExpandedSubBlockStart.gif        公共方法#region 公共方法
140InBlock.gif
141ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
142InBlock.gif        /// 使用TripleDES加密
143InBlock.gif        /// </summary>
144InBlock.gif        /// <param name="content">需要加密的文字内容</param>
145ExpandedSubBlockEnd.gif        /// <returns>加密后的文字内容,当出现错误时返回null</returns>

146InBlock.gif        public string Encrypt(string content)
147ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
148InBlock.gif            try
149ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
150InBlock.gif                if (IsEmpty(content) || IsKeyError(bytDesKey) || IsKeyError(bytDesIV))
151ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
152InBlock.gif                    return null;
153ExpandedSubBlockEnd.gif                }

154InBlock.gif                byte[] bInContent = Encoding.ASCII.GetBytes(content.PadRight(50this.chrFillChar));
155InBlock.gif                if (IsOverflow(bInContent, DecryptLength))
156ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
157InBlock.gif                    return null;
158ExpandedSubBlockEnd.gif                }

159InBlock.gif                byte[] bOutContent = new byte[EncryptLength];
160InBlock.gif                MemoryStream memoryStream = new MemoryStream(bOutContent);
161InBlock.gif                TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
162InBlock.gif                using (CryptoStream cryptoStream =
163InBlock.gif                    new CryptoStream(memoryStream, provider.CreateEncryptor(bytDesKey, bytDesIV), CryptoStreamMode.Write))
164ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
165InBlock.gif                    cryptoStream.Write(bInContent, 0, bInContent.Length);
166InBlock.gif                    cryptoStream.Close();
167ExpandedSubBlockEnd.gif                }

168InBlock.gif                return Convert.ToBase64String(bOutContent);
169ExpandedSubBlockEnd.gif            }

170InBlock.gif            catch (Exception ex)
171ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
172InBlock.gif                strErrorMessage = ex.Message;
173InBlock.gif                return null;
174ExpandedSubBlockEnd.gif            }

175ExpandedSubBlockEnd.gif        }

176InBlock.gif
177ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
178InBlock.gif        /// 使用TripleDES解密
179InBlock.gif        /// </summary>
180InBlock.gif        /// <param name="content">需要解密的文字内容</param>
181ExpandedSubBlockEnd.gif        /// <returns>解密后的文字内容,当出现错误时返回null</returns>

182InBlock.gif        public string Decrypt(string content)
183ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
184InBlock.gif            try
185ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
186InBlock.gif                if (IsEmpty(content) || IsKeyError(bytDesKey) || IsKeyError(bytDesIV))
187ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
188InBlock.gif                    return null;
189ExpandedSubBlockEnd.gif                }

190InBlock.gif                byte[] bInContent = Convert.FromBase64String(content);
191InBlock.gif                if (IsOverflow(bInContent, EncryptLength))
192ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
193InBlock.gif                    return null;
194ExpandedSubBlockEnd.gif                }

195InBlock.gif                byte[] bOutContent = new byte[DecryptLength];
196InBlock.gif                MemoryStream memoryStream = new MemoryStream(bInContent);
197InBlock.gif                TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();
198InBlock.gif                using (CryptoStream cryptoStream
199InBlock.gif                    = new CryptoStream(memoryStream, provider.CreateDecryptor(bytDesKey, bytDesIV), CryptoStreamMode.Read))
200ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
201InBlock.gif                    cryptoStream.Read(bOutContent, 0, bOutContent.Length);
202InBlock.gif                    cryptoStream.Close();
203ExpandedSubBlockEnd.gif                }

204InBlock.gif                return Encoding.ASCII.GetString(bOutContent).TrimEnd(this.chrFillChar);
205ExpandedSubBlockEnd.gif            }

206InBlock.gif            catch (Exception ex)
207ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
208InBlock.gif                strErrorMessage = ex.Message;
209InBlock.gif                return null;
210ExpandedSubBlockEnd.gif            }

211ExpandedSubBlockEnd.gif        }

212InBlock.gif
213ExpandedSubBlockEnd.gif        #endregion

214InBlock.gif
215ContractedSubBlock.gifExpandedSubBlockStart.gif        私有方法#region 私有方法
216InBlock.gif
217ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
218InBlock.gif        /// 检查内容是否为空
219InBlock.gif        /// </summary>
220InBlock.gif        /// <param name="content">待检查的文字内容</param>
221ExpandedSubBlockEnd.gif        /// <returns>当内容为空时返回true,当内容非空时返回false</returns>

222InBlock.gif        private bool IsEmpty(string content)
223ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
224InBlock.gif            if (content == null || content == string.Empty)
225ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
226InBlock.gif                strErrorMessage = "需要加密的文字内容不能为空";
227InBlock.gif                return true;
228ExpandedSubBlockEnd.gif            }

229InBlock.gif            else
230ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
231InBlock.gif                return false;
232ExpandedSubBlockEnd.gif            }

233ExpandedSubBlockEnd.gif        }

234InBlock.gif
235ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
236InBlock.gif        /// 检查内容是否溢出
237InBlock.gif        /// </summary>
238InBlock.gif        /// <param name="content">待检查的文字内容</param>
239InBlock.gif        /// <param name="length">内容的最大允许长度</param>
240ExpandedSubBlockEnd.gif        /// <returns>当内容溢出时返回true,当内容正常时返回false</returns>

241InBlock.gif        private bool IsOverflow(byte[] content, int length)
242ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
243InBlock.gif            if (content.Length > length)
244ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
245InBlock.gif                strErrorMessage = "需要加密的文字内容最大长度为" + length.ToString() + "个字节";
246InBlock.gif                return true;
247ExpandedSubBlockEnd.gif            }

248InBlock.gif            else
249ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
250InBlock.gif                return false;
251ExpandedSubBlockEnd.gif            }

252ExpandedSubBlockEnd.gif        }

253InBlock.gif
254ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
255InBlock.gif        /// 检查加密用的【指定的密钥(Key)】和【初始化向量(IV)】是否都满足16位的长度
256InBlock.gif        /// </summary>
257InBlock.gif        /// <param name="key">需要检查的key</param>
258ExpandedSubBlockEnd.gif        /// <returns>当key的长度错误时返回true,当key的长度正确时返回false</returns>

259InBlock.gif        private bool IsKeyError(byte[] key)
260ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
261InBlock.gif            if (key.Length != KeyLength)
262ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
263InBlock.gif                strErrorMessage = "请检查【指定的密钥(Key)】和【初始化向量(IV)】的长度是否都为" + KeyLength + "";
264InBlock.gif                return true;
265ExpandedSubBlockEnd.gif            }

266InBlock.gif            else
267ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
268InBlock.gif                return false;
269ExpandedSubBlockEnd.gif            }

270ExpandedSubBlockEnd.gif        }

271InBlock.gif
272ExpandedSubBlockEnd.gif        #endregion

273ExpandedSubBlockEnd.gif    }

274ExpandedBlockEnd.gif}

275None.gif

转载于:https://www.cnblogs.com/achilles/archive/2006/10/26/540612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值