欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-24 14:29:04
复制代码 代码如下: using system; using system.io; using system.data; using system.text; using...
复制代码 代码如下:

using system;
using system.io;
using system.data;
using system.text;
using system.diagnostics;
using system.security;
using system.security.cryptography;
/**//*
* .net框架由于拥有clr提供的丰富库支持,只需很少的代码即可实现先前使用c等旧式语言很难实现的加密算法。本类实现一些常用机密算法,供参考。其中md5算法返回int的tostring字串。返回数字字母型结果的算法参见之前blog文章
*/
namespace 档案数字化加工
{
/**//// <summary>
/// 类名:hashencrypt
/// 作用:对传入的字符串进行hash运算,返回通过hash算法加密过的字串。
/// 属性:[无]
/// 构造函数额参数:
/// isreturnnum:是否返回为加密后字符的byte代码
/// iscasesensitive:是否区分大小写。
/// 方法:此类提供md5,sha1,sha256,sha512等四种算法,加密字串的长度依次增大。
/// </summary>
public class hashencrypt
{
//private string strin;
private bool isreturnnum;
private bool iscasesensitive;
/**//// <summary>
/// 类初始化,此类提供md5,sha1,sha256,sha512等四种算法,加密字串的长度依次增大。
/// </summary>
/// <param name="iscasesensitive">是否区分大小写</param>
/// <param name="isreturnnum">是否返回为加密后字符的byte代码</param>
public hashencrypt(bool iscasesensitive, bool isreturnnum)
{
this.isreturnnum = isreturnnum;
this.iscasesensitive = iscasesensitive;
}
private string getstrin(string strin)
{
//string strin = strin;
if (strin.length == 0)
{
strin = "~null~";
}
if (iscasesensitive == false)
{
strin = strin.toupper();
}
return strin;
}
public string md5encrypt(string strin)
{
//string strin = getstrin(strin);
byte[] tmpbyte;
md5 md5 = new md5cryptoserviceprovider();
tmpbyte = md5.computehash(getkeybytearray(getstrin(strin)));
md5.clear();
return getstringvalue(tmpbyte);
}
public string sha1encrypt(string strin)
{
//string strin = getstrin(strin);
byte[] tmpbyte;
sha1 sha1 = new sha1cryptoserviceprovider();
tmpbyte = sha1.computehash(getkeybytearray(strin));
sha1.clear();
return getstringvalue(tmpbyte);
}
public string sha256encrypt(string strin)
{
//string strin = getstrin(strin);
byte[] tmpbyte;
sha256 sha256 = new sha256managed();
tmpbyte = sha256.computehash(getkeybytearray(strin));
sha256.clear();
return getstringvalue(tmpbyte);
}
public string sha512encrypt(string strin)
{
//string strin = getstrin(strin);
byte[] tmpbyte;
sha512 sha512 = new sha512managed();
tmpbyte = sha512.computehash(getkeybytearray(strin));
sha512.clear();
return getstringvalue(tmpbyte);
}
/**//// <summary>
/// 使用des加密(added by niehl 2005-4-6)
/// </summary>
/// <param name="originalvalue">待加密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <param name="iv">初始化向量(最大长度8)</param>
/// <returns>加密后的字符串</returns>
public string desencrypt(string originalvalue, string key, string iv)
{
//将key和iv处理成8个字符
key += "12345678";
iv += "12345678";
key = key.substring(0, 8);
iv = iv.substring(0, 8);
symmetricalgorithm sa;
icryptotransform ct;
memorystream ms;
cryptostream cs;
byte[] byt;
sa = new descryptoserviceprovider();
sa.key = encoding.utf8.getbytes(key);
sa.iv = encoding.utf8.getbytes(iv);
ct = sa.createencryptor();
byt = encoding.utf8.getbytes(originalvalue);
ms = new memorystream();
cs = new cryptostream(ms, ct, cryptostreammode.write);
cs.write(byt, 0, byt.length);
cs.flushfinalblock();
cs.close();
return convert.tobase64string(ms.toarray());
}
public string desencrypt(string originalvalue, string key)
{
return desencrypt(originalvalue, key, key);
}
/**//// <summary>
/// 使用des解密(added by niehl 2005-4-6)
/// </summary>
/// <param name="encryptedvalue">待解密的字符串</param>
/// <param name="key">密钥(最大长度8)</param>
/// <param name="iv">m初始化向量(最大长度8)</param>
/// <returns>解密后的字符串</returns>
public string desdecrypt(string encryptedvalue, string key, string iv)
{
//将key和iv处理成8个字符
key += "12345678";
iv += "12345678";
key = key.substring(0, 8);
iv = iv.substring(0, 8);
symmetricalgorithm sa;
icryptotransform ct;
memorystream ms;
cryptostream cs;
byte[] byt;
sa = new descryptoserviceprovider();
sa.key = encoding.utf8.getbytes(key);
sa.iv = encoding.utf8.getbytes(iv);
ct = sa.createdecryptor();
byt = convert.frombase64string(encryptedvalue);
ms = new memorystream();
cs = new cryptostream(ms, ct, cryptostreammode.write);
cs.write(byt, 0, byt.length);
cs.flushfinalblock();
cs.close();
return encoding.utf8.getstring(ms.toarray());
}
public string desdecrypt(string encryptedvalue, string key)
{
return desdecrypt(encryptedvalue, key, key);
}
private string getstringvalue(byte[] byte)
{
string tmpstring = "";
if (this.isreturnnum == false)
{
asciiencoding asc = new asciiencoding();
tmpstring = asc.getstring(byte);
}
else
{
int icounter;
for (icounter = 0; icounter < byte.length; icounter++)
{
tmpstring = tmpstring + byte[icounter].tostring();
}
}
return tmpstring;
}
private byte[] getkeybytearray(string strkey)
{
asciiencoding asc = new asciiencoding();
int tmpstrlen = strkey.length;
byte[] tmpbyte = new byte[tmpstrlen - 1];
tmpbyte = asc.getbytes(strkey);
return tmpbyte;
}
}
}