.NET加密技术应用

None.gif
None.gif
using  System;
None.gif
using  System.Text;
None.gif
using  System.Security;
None.gif
using  System.Security.Cryptography;
None.gif
using  System.IO;
None.gif
namespace  EncryptClasses
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// 此处定义的是DES加密,为了便于今后的管理和维护
InBlock.gif 
/// 请不要随便改动密码,或者改变了密码后请一定要
InBlock.gif 
/// 牢记先前的密码,否则将会照成不可预料的损失
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class DESEncrypt
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif  
"member fields"#region "member fields"
InBlock.gif  
private string iv="12345678";
InBlock.gif  
private string key="12345678";
InBlock.gif  
private Encoding encoding=new UnicodeEncoding();
InBlock.gif  
private DES des;
ExpandedSubBlockEnd.gif  
#endregion

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 构造函数
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public DESEncrypt()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   des
=new DESCryptoServiceProvider();
ExpandedSubBlockEnd.gif  }

ContractedSubBlock.gifExpandedSubBlockStart.gif  
"propertys"#region "propertys"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 设置加密密钥
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public string EncryptKey
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
getdot.gif{return this.key;}
InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
this.key=value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 要加密字符的编码模式
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public Encoding EncodingMode
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
getdot.gif{return this.encoding;}
ExpandedSubBlockStart.gifContractedSubBlock.gif   
setdot.gif{this.encoding=value;}
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

ContractedSubBlock.gifExpandedSubBlockStart.gif  
"methods"#region "methods"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 加密字符串并返回加密后的结果
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="str"></param>
ExpandedSubBlockEnd.gif  
/// <returns></returns>

InBlock.gif  public string EncryptString(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
InBlock.gif   
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);//得到加密密钥
InBlock.gif
   byte[] toEncrypt=this.EncodingMode.GetBytes(str);//得到要加密的内容
InBlock.gif
   byte[] encrypted;
InBlock.gif   ICryptoTransform encryptor
=des.CreateEncryptor(keyb,ivb);
InBlock.gif   MemoryStream msEncrypt
=new MemoryStream();
InBlock.gif   CryptoStream csEncrypt
=new CryptoStream(msEncrypt,encryptor,CryptoStreamMode.Write);
InBlock.gif   csEncrypt.Write(toEncrypt,
0,toEncrypt.Length);
InBlock.gif   csEncrypt.FlushFinalBlock();
InBlock.gif   encrypted
=msEncrypt.ToArray();
InBlock.gif   csEncrypt.Close();
InBlock.gif   msEncrypt.Close();
InBlock.gif   
return this.EncodingMode.GetString(encrypted);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 加密指定的文件,如果成功返回True,否则false
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="filePath">要加密的文件路径</param>
ExpandedSubBlockEnd.gif  
/// <param name="outPath">加密后的文件输出路径</param>

InBlock.gif  public void EncryptFile(string filePath,string outPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
bool isExist=File.Exists(filePath);
InBlock.gif   
if(isExist)//如果存在
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
InBlock.gif    
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
InBlock.gif    
//得到要加密文件的字节流
InBlock.gif
    FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
InBlock.gif    StreamReader reader
=new StreamReader(fin,this.EncodingMode);
InBlock.gif    
string dataStr=reader.ReadToEnd();
InBlock.gif    
byte[] toEncrypt=this.EncodingMode.GetBytes(dataStr);
InBlock.gif    fin.Close();
InBlock.gif
InBlock.gif    FileStream fout
=new FileStream(outPath,FileMode.Create,FileAccess.Write);
InBlock.gif    ICryptoTransform encryptor
=des.CreateEncryptor(keyb,ivb);
InBlock.gif    CryptoStream csEncrypt
=new CryptoStream(fout,encryptor,CryptoStreamMode.Write);
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
//加密得到的文件字节流
InBlock.gif
     csEncrypt.Write(toEncrypt,0,toEncrypt.Length);
InBlock.gif     csEncrypt.FlushFinalBlock();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
throw new ApplicationException(err.Message);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
try
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      fout.Close();
InBlock.gif      csEncrypt.Close();
ExpandedSubBlockEnd.gif     }

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

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw new FileNotFoundException("没有找到指定的文件");
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 文件加密函数的重载版本,如果不指定输出路径,
InBlock.gif  
/// 那么原来的文件将被加密后的文件覆盖
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="filePath"></param>

InBlock.gif  public void EncryptFile(string filePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
this.EncryptFile(filePath,filePath);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 解密给定的字符串
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="str">要解密的字符</param>
ExpandedSubBlockEnd.gif  
/// <returns></returns>

InBlock.gif  public string DecryptString(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
InBlock.gif   
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
InBlock.gif   
byte[] toDecrypt=this.EncodingMode.GetBytes(str);
InBlock.gif   
byte[] deCrypted=new byte[toDecrypt.Length];
InBlock.gif   ICryptoTransform deCryptor
=des.CreateDecryptor(keyb,ivb);
InBlock.gif   MemoryStream msDecrypt
=new MemoryStream(toDecrypt);
InBlock.gif   CryptoStream csDecrypt
=new CryptoStream(msDecrypt,deCryptor,CryptoStreamMode.Read);
InBlock.gif   
try
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    csDecrypt.Read(deCrypted,
0,deCrypted.Length);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
catch(Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw new ApplicationException(err.Message);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     msDecrypt.Close();
InBlock.gif     csDecrypt.Close();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
catchdot.gif{;}
ExpandedSubBlockEnd.gif   }

InBlock.gif   
return this.EncodingMode.GetString(deCrypted);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 解密指定的文件
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="filePath">要解密的文件路径</param>
ExpandedSubBlockEnd.gif  
/// <param name="outPath">解密后的文件输出路径</param>

InBlock.gif  public void DecryptFile(string filePath,string outPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
bool isExist=File.Exists(filePath);
InBlock.gif   
if(isExist)//如果存在
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    
byte[] ivb=Encoding.ASCII.GetBytes(this.iv);
InBlock.gif    
byte[] keyb=Encoding.ASCII.GetBytes(this.EncryptKey);
InBlock.gif    FileInfo file
=new FileInfo(filePath);
InBlock.gif    
byte[] deCrypted=new byte[file.Length];
InBlock.gif    
//得到要解密文件的字节流
InBlock.gif
    FileStream fin=new FileStream(filePath,FileMode.Open,FileAccess.Read);
InBlock.gif    
//解密文件
InBlock.gif
    try
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     ICryptoTransform decryptor
=des.CreateDecryptor(keyb,ivb);
InBlock.gif     CryptoStream csDecrypt
=new CryptoStream(fin,decryptor,CryptoStreamMode.Read);
InBlock.gif     csDecrypt.Read(deCrypted,
0,deCrypted.Length);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
catch(Exception err)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
throw new ApplicationException(err.Message);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
try
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      fin.Close();
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockStart.gifContractedSubBlock.gif     
catchdot.gif{;}
ExpandedSubBlockEnd.gif    }

InBlock.gif    FileStream fout
=new FileStream(outPath,FileMode.Create,FileAccess.Write);
InBlock.gif    fout.Write(deCrypted,
0,deCrypted.Length);
InBlock.gif    fout.Close();
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
throw new FileNotFoundException("指定的解密文件没有找到");
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
InBlock.gif  
/// 则解密后的文件将覆盖先前的文件
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <param name="filePath"></param>

InBlock.gif  public void DecryptFile(string filePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
this.DecryptFile(filePath,filePath);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif  
#endregion

ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// MD5加密类,注意经MD5加密过的信息是不能转换回原始数据的
InBlock.gif 
/// ,请不要在用户敏感的信息中使用此加密技术,比如用户的密码,
InBlock.gif 
/// 请尽量使用对称加密
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class MD5Encrypt
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
private MD5 md5;
InBlock.gif  
public MD5Encrypt()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   md5
=new MD5CryptoServiceProvider();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 从字符串中获取散列值
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="str">要计算散列值的字符串</param>
ExpandedSubBlockEnd.gif  
/// <returns></returns>

InBlock.gif  public string GetMD5FromString(string str)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
byte[] toCompute=Encoding.Unicode.GetBytes(str);
InBlock.gif   
byte[] hashed=md5.ComputeHash(toCompute,0,toCompute.Length);
InBlock.gif   
return Encoding.ASCII.GetString(hashed);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 根据文件来计算散列值
InBlock.gif  
/// </summary>
InBlock.gif  
/// <param name="filePath">要计算散列值的文件路径</param>
ExpandedSubBlockEnd.gif  
/// <returns></returns>

InBlock.gif  public string GetMD5FromFile(string filePath)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
bool isExist=File.Exists(filePath);
InBlock.gif   
if(isExist)//如果文件存在
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    FileStream stream
=new FileStream(filePath,FileMode.Open,FileAccess.Read);
InBlock.gif    StreamReader reader
=new StreamReader(stream,Encoding.Unicode);
InBlock.gif    
string str=reader.ReadToEnd();
InBlock.gif    
byte[] toHash=Encoding.Unicode.GetBytes(str);
InBlock.gif    
byte[] hashed=md5.ComputeHash(toHash,0,toHash.Length);
InBlock.gif    stream.Close();
InBlock.gif    
return Encoding.ASCII.GetString(hashed);
ExpandedSubBlockEnd.gif   }

InBlock.gif   
else//文件不存在
ExpandedSubBlockStart.gifContractedSubBlock.gif
   dot.gif{
InBlock.gif    
throw new FileNotFoundException("指定的文件没有找到");
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif 
/**//// <summary>
InBlock.gif 
/// 用于数字签名的hash类
ExpandedSubBlockEnd.gif 
/// </summary>

InBlock.gif public class MACTripleDESEncrypt
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
private MACTripleDES mact;
InBlock.gif  
private string __key="ksn168ch";
InBlock.gif  
private byte[] __data=null;
InBlock.gif  
public MACTripleDESEncrypt()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   mact
=new MACTripleDES();
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 获取或设置用于数字签名的密钥
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public string Key
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
getdot.gif{return this.__key;}
InBlock.gif   
set
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
int keyLength=value.Length;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
int[] keyAllowLengths=new int[]dot.gif{8,16,24};
InBlock.gif    
bool isRight=false;
InBlock.gif    
foreach(int i in keyAllowLengths)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif     
if(keyLength==keyAllowLengths[i])
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif      isRight
=true;
InBlock.gif      
break;
ExpandedSubBlockEnd.gif     }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
if(!isRight)
InBlock.gif     
throw new ApplicationException("用于数字签名的密钥长度必须是8,16,24值之一");
InBlock.gif    
else
InBlock.gif     
this.__key=value;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 获取或设置用于数字签名的用户数据
ExpandedSubBlockEnd.gif  
/// </summary>

InBlock.gif  public byte[] Data
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
getdot.gif{return this.__data;}
ExpandedSubBlockStart.gifContractedSubBlock.gif   
setdot.gif{this.__data=value;}
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
/**//// <summary>
InBlock.gif  
/// 得到签名后的hash值
InBlock.gif  
/// </summary>
ExpandedSubBlockEnd.gif  
/// <returns></returns>

InBlock.gif  public string GetHashValue()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if(this.Data==null)
InBlock.gif    
throw new NotSetSpecialPropertyException("没有设置要进行数字签名的用户"+
InBlock.gif                                         
"数据(property:Data)");
InBlock.gif   
byte[] key=Encoding.ASCII.GetBytes(this.Key);
InBlock.gif   
this.mact.Key=key;
InBlock.gif   
byte[] hash_b=this.mact.ComputeHash(this.mact.ComputeHash(this.Data));
InBlock.gif   
return Encoding.ASCII.GetString(hash_b);
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif

转载于:https://www.cnblogs.com/jhobo/archive/2006/09/15/505125.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值