C# DES 加密/解密类库
2010-04-30 16:30
489 查看
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Security; using System.Security.Cryptography; namespace HyeyClass.Common { #region DES Class /// <summary> /// ClassName: DES 加密类 /// DES加密、解密类库,字符串加密结果使用BASE64编码返回,支持文件的加密和解密 /// Time:2010-04-30 /// Auther:Hyey.wl /// DES 的摘要说明。 /// </summary> public sealed class DES { /// <summary> /// myiv is iv /// </summary> string myiv = "Hyey20100430"; /// <summary> /// mykey is key /// </summary> string mykey = "HyeyWl30"; /// <summary> /// DES加密偏移量 /// 必须是>=8位长的字符串 /// </summary> public string IV { get{ return myiv;} set{ myiv = value;} } /// <summary> /// DES加密的私钥 /// 必须是8位长的字符串 /// </summary> public string Key { get{ return mykey;} set{ mykey = value;} } public DES() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 对字符串进行DES加密 /// Encrypts the specified sourcestring. /// </summary> /// <param name="sourcestring">The sourcestring.待加密的字符串</param> /// <returns>加密后的BASE64编码的字符串</returns> public string Encrypt(string sourceString) { byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Encoding.Default.GetBytes(sourceString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Convert.ToBase64String(ms.ToArray()); } catch { throw; } } } /// <summary> /// Decrypts the specified encrypted string. /// 对DES加密后的字符串进行解密 /// </summary> /// <param name="encryptedString">The encrypted string.待解密的字符串</param> /// <returns>解密后的字符串</returns> public string Decrypt(string encryptedString) { byte[] btKey = Encoding.Default.GetBytes(mykey); byte[] btIV = Encoding.Default.GetBytes(myiv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (MemoryStream ms = new MemoryStream()) { byte[] inData = Convert.FromBase64String(encryptedString); try { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); } return Encoding.Default.GetString(ms.ToArray()); } catch { throw; } } } /// <summary> /// Encrypts the file. /// 对文件内容进行DES加密 /// </summary> /// <param name="sourceFile">The source file.待加密的文件绝对路径</param> /// <param name="destFile">The dest file.加密后的文件保存的绝对路径</param> public void EncryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// Encrypts the file. /// 对文件内容进行DES加密,加密后覆盖掉原来的文件 /// </summary> /// <param name="sourceFile">The source file.待加密的文件的绝对路径</param> public void EncryptFile(string sourceFile) { EncryptFile(sourceFile, sourceFile); } /// <summary> /// Decrypts the file. /// 对文件内容进行DES解密 /// </summary> /// <param name="sourceFile">The source file.待解密的文件绝对路径</param> /// <param name="destFile">The dest file.解密后的文件保存的绝对路径</param> public void DecryptFile(string sourceFile, string destFile) { if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile); byte[] btKey = Encoding.Default.GetBytes(key); byte[] btIV = Encoding.Default.GetBytes(iv); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] btFile = File.ReadAllBytes(sourceFile); using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write)) { try { using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(btFile, 0, btFile.Length); cs.FlushFinalBlock(); } } catch { throw; } finally { fs.Close(); } } } /// <summary> /// Decrypts the file. /// 对文件内容进行DES解密,加密后覆盖掉原来的文件. /// </summary> /// <param name="sourceFile">The source file.待解密的文件的绝对路径.</param> public void DecryptFile(string sourceFile) { DecryptFile(sourceFile, sourceFile); } } #endregion }
相关文章推荐
- [读书笔记]C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- [转]C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- 黄聪:C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- C# DES 加密/解密类库,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- C# 加密解密(DES,3DES,MD5,Base64) 类
- C#中使用DES和AES加密解密
- C#字符串的加密与解密(DES和TripleDES算法的加密解密过程)
- C# 加密解密(DES,3DES,MD5,Base64) 类
- C# DES 加密/解密,支持文件和中文/UNICODE字符,返回BASE64编码字符串
- c# DES 加密解密
- c# DESEncrypt 加密、解密算法
- C#DES对较大文件和文件字节数组对象加密解密
- C#中使用DES和AES加密解密
- Java加密和C#解密=>DES方法
- C#加密解密(DES,AES,Base64,md5,SHA256,RSA,RC4)
- C#中使用DES和AES加密解密
- 用 Java 解密 C# 加密的数据(DES)(转)
- Asp.Net C#中除了MD5加密之外还经常用到了AES加密、解密与DES加密、解密,本次先介绍DES的加密及解密。注意的是DES的密匙是8位的。
- C#解密(DES)Java的加密结果
- C# 加密解密(DES,3DES,MD5,Base64) 类