c# 实现MD5,SHA1,SHA256,SHA512等常用加密算法

None.gif using  System;
None.gif
using  System.IO;
None.gif
using  System.Data;
None.gif
using  System.Text;
None.gif
using  System.Diagnostics;
None.gif
using  System.Security;
None.gif
using  System.Security.Cryptography;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* 
InBlock.gif * .Net框架由于拥有CLR提供的丰富库支持,只需很少的代码即可实现先前使用C等旧式语言很难实现的加密算法。本类实现一些常用机密算法,供参考。其中MD5算法返回Int的ToString字串。返回数字字母型结果的算法参见之前Blog文章
ExpandedBlockEnd.gif 
*/

None.gif
namespace  档案数字化加工
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 类名:HashEncrypt
InBlock.gif    
/// 作用:对传入的字符串进行Hash运算,返回通过Hash算法加密过的字串。
InBlock.gif    
/// 属性:[无]
InBlock.gif    
/// 构造函数额参数:
InBlock.gif    
/// IsReturnNum:是否返回为加密后字符的Byte代码
InBlock.gif    
/// IsCaseSensitive:是否区分大小写。
InBlock.gif    
/// 方法:此类提供MD5,SHA1,SHA256,SHA512等四种算法,加密字串的长度依次增大。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class HashEncrypt
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//private string strIN;
InBlock.gif
        private bool isReturnNum;
InBlock.gif        
private bool isCaseSensitive;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 类初始化,此类提供MD5,SHA1,SHA256,SHA512等四种算法,加密字串的长度依次增大。
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="IsCaseSensitive">是否区分大小写</param>
ExpandedSubBlockEnd.gif        
/// <param name="IsReturnNum">是否返回为加密后字符的Byte代码</param>

InBlock.gif        public HashEncrypt(bool IsCaseSensitive, bool IsReturnNum)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.isReturnNum = IsReturnNum;
InBlock.gif            
this.isCaseSensitive = IsCaseSensitive;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string getstrIN(string strIN)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//string strIN = strIN;
InBlock.gif
            if (strIN.Length == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strIN 
= "~NULL~";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
if (isCaseSensitive == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                strIN 
= strIN.ToUpper();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return strIN;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string MD5Encrypt(string strIN)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//string strIN = getstrIN(strIN);
InBlock.gif
            byte[] tmpByte;
InBlock.gif            MD5 md5 
= new MD5CryptoServiceProvider();
InBlock.gif            tmpByte 
= md5.ComputeHash(GetKeyByteArray(getstrIN(strIN)));
InBlock.gif            md5.Clear();
InBlock.gif
InBlock.gif            
return GetStringValue(tmpByte);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string SHA1Encrypt(string strIN)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//string strIN = getstrIN(strIN);
InBlock.gif
            byte[] tmpByte;
InBlock.gif            SHA1 sha1 
= new SHA1CryptoServiceProvider();
InBlock.gif
InBlock.gif            tmpByte 
= sha1.ComputeHash(GetKeyByteArray(strIN));
InBlock.gif            sha1.Clear();
InBlock.gif
InBlock.gif            
return GetStringValue(tmpByte);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string SHA256Encrypt(string strIN)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//string strIN = getstrIN(strIN);
InBlock.gif
            byte[] tmpByte;
InBlock.gif            SHA256 sha256 
= new SHA256Managed();
InBlock.gif
InBlock.gif            tmpByte 
= sha256.ComputeHash(GetKeyByteArray(strIN));
InBlock.gif            sha256.Clear();
InBlock.gif
InBlock.gif            
return GetStringValue(tmpByte);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string SHA512Encrypt(string strIN)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//string strIN = getstrIN(strIN);
InBlock.gif
            byte[] tmpByte;
InBlock.gif            SHA512 sha512 
= new SHA512Managed();
InBlock.gif
InBlock.gif            tmpByte 
= sha512.ComputeHash(GetKeyByteArray(strIN));
InBlock.gif            sha512.Clear();
InBlock.gif
InBlock.gif            
return GetStringValue(tmpByte);
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 使用DES加密(Added by niehl 2005-4-6)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="originalValue">待加密的字符串</param>
InBlock.gif        
/// <param name="key">密钥(最大长度8)</param>
InBlock.gif        
/// <param name="IV">初始化向量(最大长度8)</param>
ExpandedSubBlockEnd.gif        
/// <returns>加密后的字符串</returns>

InBlock.gif        public string DESEncrypt(string originalValue, string key, string IV)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//将key和IV处理成8个字符
InBlock.gif
            key += "12345678";
InBlock.gif            IV 
+= "12345678";
InBlock.gif            key 
= key.Substring(08);
InBlock.gif            IV 
= IV.Substring(08);
InBlock.gif
InBlock.gif            SymmetricAlgorithm sa;
InBlock.gif            ICryptoTransform ct;
InBlock.gif            MemoryStream ms;
InBlock.gif            CryptoStream cs;
InBlock.gif            
byte[] byt;
InBlock.gif
InBlock.gif            sa 
= new DESCryptoServiceProvider();
InBlock.gif            sa.Key 
= Encoding.UTF8.GetBytes(key);
InBlock.gif            sa.IV 
= Encoding.UTF8.GetBytes(IV);
InBlock.gif            ct 
= sa.CreateEncryptor();
InBlock.gif
InBlock.gif            byt 
= Encoding.UTF8.GetBytes(originalValue);
InBlock.gif
InBlock.gif            ms 
= new MemoryStream();
InBlock.gif            cs 
= new CryptoStream(ms, ct, CryptoStreamMode.Write);
InBlock.gif            cs.Write(byt, 
0, byt.Length);
InBlock.gif            cs.FlushFinalBlock();
InBlock.gif
InBlock.gif            cs.Close();
InBlock.gif
InBlock.gif            
return Convert.ToBase64String(ms.ToArray());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string DESEncrypt(string originalValue, string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return DESEncrypt(originalValue, key, key);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 使用DES解密(Added by niehl 2005-4-6)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="encryptedValue">待解密的字符串</param>
InBlock.gif        
/// <param name="key">密钥(最大长度8)</param>
InBlock.gif        
/// <param name="IV">m初始化向量(最大长度8)</param>
ExpandedSubBlockEnd.gif        
/// <returns>解密后的字符串</returns>

InBlock.gif        public string DESDecrypt(string encryptedValue, string key, string IV)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//将key和IV处理成8个字符
InBlock.gif
            key += "12345678";
InBlock.gif            IV 
+= "12345678";
InBlock.gif            key 
= key.Substring(08);
InBlock.gif            IV 
= IV.Substring(08);
InBlock.gif
InBlock.gif            SymmetricAlgorithm sa;
InBlock.gif            ICryptoTransform ct;
InBlock.gif            MemoryStream ms;
InBlock.gif            CryptoStream cs;
InBlock.gif            
byte[] byt;
InBlock.gif
InBlock.gif            sa 
= new DESCryptoServiceProvider();
InBlock.gif            sa.Key 
= Encoding.UTF8.GetBytes(key);
InBlock.gif            sa.IV 
= Encoding.UTF8.GetBytes(IV);
InBlock.gif            ct 
= sa.CreateDecryptor();
InBlock.gif
InBlock.gif            byt 
= Convert.FromBase64String(encryptedValue);
InBlock.gif
InBlock.gif            ms 
= new MemoryStream();
InBlock.gif            cs 
= new CryptoStream(ms, ct, CryptoStreamMode.Write);
InBlock.gif            cs.Write(byt, 
0, byt.Length);
InBlock.gif            cs.FlushFinalBlock();
InBlock.gif
InBlock.gif            cs.Close();
InBlock.gif
InBlock.gif            
return Encoding.UTF8.GetString(ms.ToArray());
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string DESDecrypt(string encryptedValue, string key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return DESDecrypt(encryptedValue, key, key);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string GetStringValue(byte[] Byte)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string tmpString = "";
InBlock.gif            
if (this.isReturnNum == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ASCIIEncoding Asc 
= new ASCIIEncoding();
InBlock.gif                tmpString 
= Asc.GetString(Byte);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int iCounter;
InBlock.gif                
for (iCounter = 0; iCounter < Byte.Length; iCounter++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    tmpString 
= tmpString + Byte[iCounter].ToString();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return tmpString;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private byte[] GetKeyByteArray(string strKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            ASCIIEncoding Asc 
= new ASCIIEncoding();
InBlock.gif
InBlock.gif            
int tmpStrLen = strKey.Length;
InBlock.gif            
byte[] tmpByte = new byte[tmpStrLen - 1];
InBlock.gif
InBlock.gif            tmpByte 
= Asc.GetBytes(strKey);
InBlock.gif
InBlock.gif            
return tmpByte;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值