SHA1、MD5 、DES的加密解密

MSDN 加网上参考结合版,因为项目需求用到DES加密
using  System;
using  System.IO;
using  System.Data;
using  System.Text;
using  System.Diagnostics;
using  System.Security;
using  System.Security.Cryptography;

///   <summary>
///  statics 的摘要说明
///   </summary>
public   class  statics
{
    
public  statics()
    {
        
//
        
//  TODO: 在此处添加构造函数逻辑
        
//
    }
    
public   static   byte [] Gytes( string  txt)   // 转换字节
    {
        UTF8Encoding utf 
=   new  UTF8Encoding();
        
byte [] bytes  =  utf.GetBytes(txt);
        
return  bytes;
    }
    
public   static   string  Gpass( byte [] bt) // 转换字符串
    {
        StringBuilder sb 
=   new  StringBuilder();
        
foreach  ( int  p  in  bt)
        {
            sb.Append(Convert.ToString(p, 
16 ).PadLeft( 2 ' 0 ' ));
        }
        
return  sb.ToString();
    }
    
public   static   string  Gutf8( byte [] bt)
    {
        UTF8Encoding utf 
=   new  UTF8Encoding();
        
return  utf.GetString(bt).ToString();
    }
    
public   static   byte [] Gbase( string  bs)
    {
        
return  Convert.FromBase64String(bs);
    }
    
public   static   string  Sha1EN( string  txt)  // sha1散列
    {
        
try
        {
            SHA1 s 
=   new  SHA1CryptoServiceProvider();
            
byte [] b2  =  s.ComputeHash(Gytes(txt));
            s.Clear();
            
return  Gpass(b2);
        }
        
catch  {  return   string .Empty; }
    }
    
public   static  String MD5EN( string  txt)
    {
        
try
        {
            MD5 m 
=   new  MD5CryptoServiceProvider();
            
byte [] b2  =  m.ComputeHash(Gytes(txt));
            m.Clear();
            
return  Gpass(b2);
        }
        
catch  {  return   string .Empty; }
    }
   
    
///  使用DES加密
    
///   <param name="value"> 待加密的字符串 </param>
    
///   <param name="key"> 密钥(最大长度8) </param>
    
///   <param name="IV"> 初始化向量(最大长度8) </param>
    
///   <returns> 加密后的字符串 </returns>
     public   static   string  Des3EN( string  value,  string  key,  string  iv)
    {
        
try
        {
            key 
=  key.PadRight( 8 ' 0 ' ).Substring( 0 8 );
            iv 
=  iv.PadRight( 8 ' 0 ' ).Substring( 0 8 );
            MemoryStream ms 
=   new  MemoryStream();
            DES des 
=   new  DESCryptoServiceProvider();
            CryptoStream cs 
=   new  CryptoStream(ms, des.CreateEncryptor(Gytes(key), Gytes(iv)), CryptoStreamMode.Write);
            des.Clear();
            cs.Write(Gytes(value), 
0 , Gytes(value).Length);
            cs.FlushFinalBlock();
            cs.Clear();
            
return  Convert.ToBase64String(ms.ToArray());
        }
        
catch  {  return   string .Empty; }
    }
    
public   static   string  Des3EN( string  value,  string  key)
    {
        
return  Des3EN(value, key, key);
    }
    
public   static   string  Des3DE( string  value,  string  key,  string  iv)
    {
        
try
        {
            key 
=  key.PadRight( 8 ' 0 ' ).Substring( 0 8 );
            iv 
=  iv.PadRight( 8 ' 0 ' ).Substring( 0 8 );
            MemoryStream ms 
=   new  MemoryStream();
            DES des 
=   new  DESCryptoServiceProvider();
            CryptoStream cs 
=   new  CryptoStream(ms, des.CreateDecryptor(Gytes(key), Gytes(iv)), CryptoStreamMode.Write);
            des.Clear();
            cs.Write(Gbase(value), 
0 , Gbase(value).Length);
            cs.FlushFinalBlock();
            cs.Clear();
            
return  Gutf8(ms.ToArray()).ToString();
        }
        
catch  {  return   string .Empty; }
    }
    
public   static   string  Des3DE( string  value,  string  key)
    {
        
return  Des3DE(value, key, key);
    }
}

转载于:https://www.cnblogs.com/xcive/archive/2007/08/03/841758.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值