ASP.NET加密技术的应用(加密类代码参考)

来源:互联网 发布:人人家政软件怎么样 编辑:程序博客网 时间:2024/05/18 01:24

 /**********************Created by Chen**************************

*如果你觉得本人的文章好,要引用请尊重著作人的劳动果实,说明 

*出处以及原创作者,Thank you!!!   email:aishen944-sohu.com

******************************************************************
*/


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

 public class DESEncrypt
 
{
  
"member fields"
  
/// <summary>
  
/// 构造函数
  
/// </summary>

  public DESEncrypt()
  
{
   des
=new DESCryptoServiceProvider();
  }

  
"propertys"
  
"methods"
 }

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

 public class MD5Encrypt
 
{
  
private MD5 md5;
  
public MD5Encrypt()
  
{
   md5
=new MD5CryptoServiceProvider();
  }

  
/// <summary>
  
/// 从字符串中获取散列值
  
/// </summary>
  
/// <param name="str">要计算散列值的字符串</param>
  
/// <returns></returns>

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

  
/// <summary>
  
/// 根据文件来计算散列值
  
/// </summary>
  
/// <param name="filePath">要计算散列值的文件路径</param>
  
/// <returns></returns>

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

   
else//文件不存在
   {
    
throw new FileNotFoundException("指定的文件没有找到");
   }

  }

 }

 
/// <summary>
 
/// 用于数字签名的hash类
 
/// </summary>

 public class MACTripleDESEncrypt
 
{
  
private MACTripleDES mact;
  
private string __key="ksn168ch";
  
private byte[] __data=null;
  
public MACTripleDESEncrypt()
  
{
   mact
=new MACTripleDES();
  }

  
/// <summary>
  
/// 获取或设置用于数字签名的密钥
  
/// </summary>

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

    }

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

  }

  
/// <summary>
  
/// 获取或设置用于数字签名的用户数据
  
/// </summary>

  public byte[] Data
  
{
   
get{return this.__data;}
   
set{this.__data=value;}
  }

  
/// <summary>
  
/// 得到签名后的hash值
  
/// </summary>
  
/// <returns></returns>

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

 }

}