加密解密类

None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Security.Cryptography;
None.gif
using  System.Text;
None.gif
namespace  MyTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// 加密和解密
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class EncryptDecrypt
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public EncryptDecrypt()dot.gif{}
InBlock.gif  
//默认密钥向量
ExpandedSubBlockStart.gifContractedSubBlock.gif
  public byte[] Keys = dot.gif{0x120x340x560x780x900xAB0xCD0xEF};
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 利用MD5对字符串进行加密
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="encryptString">待加密的字符串</param>
ExpandedSubBlockEnd.gif  
/// <returns>返回加密后的字符串</returns>

InBlock.gif  public string EncryptMD5(string encryptString)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   MD5CryptoServiceProvider md5Hasher 
= new MD5CryptoServiceProvider();
InBlock.gif   UTF8Encoding Encode 
= new UTF8Encoding();
InBlock.gif   
byte[] HashedBytes = md5Hasher.ComputeHash(Encode.GetBytes(encryptString));
InBlock.gif   
return Encode.GetString(HashedBytes);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// DES加密字符串
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="encryptString">待加密的字符串</param>
InBlock.gif  
/// <param name="encryptKey">加密密钥,要求为8位</param>
ExpandedSubBlockEnd.gif  
/// <returns>加密成功返回加密后的字符串,失败返回源串</returns>

InBlock.gif  public string EncryptDES(string encryptString,string encryptKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
InBlock.gif    
byte[] rgbIV = Keys;
InBlock.gif    
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
InBlock.gif    DESCryptoServiceProvider dCSP 
= new DESCryptoServiceProvider();
InBlock.gif    MemoryStream mStream 
= new MemoryStream();
InBlock.gif    CryptoStream cStream 
= new CryptoStream(mStream,dCSP.CreateEncryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
InBlock.gif    cStream.Write(inputByteArray,
0,inputByteArray.Length);
InBlock.gif    cStream.FlushFinalBlock();
InBlock.gif    
return Convert.ToBase64String(mStream.ToArray());
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return encryptString;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// DES解密字符串
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="decryptString">待解密的字符串</param>
InBlock.gif  
/// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
ExpandedSubBlockEnd.gif  
/// <returns>解密成功返回解密后的字符串,失败返源串</returns>

InBlock.gif  public string DecryptDES(string decryptString,string decryptKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
InBlock.gif    
byte[] rgbIV = Keys;
InBlock.gif    
byte[] inputByteArray = Convert.FromBase64String(decryptString);
InBlock.gif    DESCryptoServiceProvider DCSP 
= new DESCryptoServiceProvider();
InBlock.gif    MemoryStream mStream 
= new MemoryStream();
InBlock.gif    CryptoStream cStream 
= new CryptoStream(mStream,DCSP.CreateDecryptor(rgbKey,rgbIV),CryptoStreamMode.Write);
InBlock.gif    cStream.Write(inputByteArray,
0,inputByteArray.Length);
InBlock.gif    cStream.FlushFinalBlock();
InBlock.gif    
return Encoding.UTF8.GetString(mStream.ToArray());
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return decryptString;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// DES加密文件
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="inFilePath">待加密文件</param>
InBlock.gif  
/// <param name="outFilePath">加密后的文件</param>
InBlock.gif  
/// <param name="encryptKey">加密密钥</param>
ExpandedSubBlockEnd.gif  
/// <returns></returns>

InBlock.gif  public bool EncryptDES(string inFilePath,string outFilePath,string encryptKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{  
InBlock.gif   
byte[] rgbIV= Keys;
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0,8));
InBlock.gif    
//读入的流
InBlock.gif
    FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
InBlock.gif    
//待写的流
InBlock.gif
    FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
InBlock.gif    outFs.SetLength(
0);
InBlock.gif    
//创建一个变量来帮助读写
InBlock.gif
    byte[] byteIn  = new byte[100]; //临时存放读入的流
InBlock.gif
    long readLen  = 0;              //读入流的长度
InBlock.gif
    long totalLen = inFs.Length;    //总共读入流的长度
InBlock.gif
    int  everyLen;                  //每次读入流动长度
InBlock.gif    
//读入InFs,加密后写入OutFs
InBlock.gif
    DES des = new DESCryptoServiceProvider();          
InBlock.gif    CryptoStream encStream 
= new CryptoStream(outFs, des.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 
InBlock.gif    
while(readLen < totalLen)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     everyLen 
= inFs.Read(byteIn, 0100);
InBlock.gif     encStream.Write(byteIn, 
0, everyLen);
InBlock.gif     readLen 
= readLen + everyLen; 
ExpandedSubBlockEnd.gif    }

InBlock.gif    encStream.Close();  
InBlock.gif    outFs.Close();
InBlock.gif    inFs.Close();
InBlock.gif    
return true;//加密成功
ExpandedSubBlockEnd.gif
   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return false;//加密失败 
ExpandedSubBlockEnd.gif
   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// DES解密文件
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="inFilePath">待解密文件</param>
InBlock.gif  
/// <param name="outFilePath">待加密文件</param>
InBlock.gif  
/// <param name="decryptKey">解密密钥</param>
ExpandedSubBlockEnd.gif  
/// <returns></returns>

InBlock.gif  public bool DecryptDES(string inFilePath,string outFilePath,string decryptKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
byte[] rgbIV= Keys;
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey.Substring(0,8));
InBlock.gif    
//读入的流
InBlock.gif
    FileStream inFs = new FileStream(inFilePath, FileMode.Open, FileAccess.Read);
InBlock.gif    
//待写的流
InBlock.gif
    FileStream outFs = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
InBlock.gif    outFs.SetLength(
0);
InBlock.gif    
//创建一个变量来帮助读写
InBlock.gif
    byte[] byteIn  = new byte[100]; //临时存放读入的流
InBlock.gif
    long readLen  = 0;              //读入流的长度
InBlock.gif
    long totalLen = inFs.Length;    //总共读入流的长度
InBlock.gif
    int  everyLen;                  //每次读入流动长度
InBlock.gif    
//读入InFs,解密后写入OutFs
InBlock.gif
    DES des = new DESCryptoServiceProvider();          
InBlock.gif    CryptoStream encStream 
= new CryptoStream(outFs, des.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write); 
InBlock.gif    
while(readLen < totalLen)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     everyLen 
= inFs.Read(byteIn, 0100);
InBlock.gif     encStream.Write(byteIn, 
0, everyLen);
InBlock.gif     readLen 
= readLen + everyLen; 
ExpandedSubBlockEnd.gif    }

InBlock.gif    encStream.Close();  
InBlock.gif    outFs.Close();
InBlock.gif    inFs.Close();
InBlock.gif    
return true;//解密成功
ExpandedSubBlockEnd.gif
   }

InBlock.gif   
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
return false;//解密失败 
ExpandedSubBlockEnd.gif
   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/syringa-flz/archive/2006/10/28/542547.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值