加密类代码
/**//**//**//**********************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""member fields"#region "member fields"
private string iv="12345678";
private string key="12345678";
private encoding encoding=new unicodeencoding();
private des des;
#endregion
/**//**//**//// <summary>
/// 构造函数
/// </summary>
public desencrypt()
...{
des=new descryptoserviceprovider();
}
"propertys""propertys"#region "propertys"
/**//**//**//// <summary>
/// 设置加密密钥
/// </summary>
public string encryptkey
...{
get...{return this.key;}
set
...{
this.key=value;
}
}
/**//**//**//// <summary>
/// 要加密字符的编码模式
/// </summary>
public encoding encodingmode
...{
get...{return this.encoding;}
set...{this.encoding=value;}
}
#endregion
"methods""methods"#region "methods"
/**//**//**//// <summary>
/// 加密字符串并返回加密后的结果
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string encryptstring(string str)
...{
byte[] ivb=encoding.ascii.getbytes(this.iv);
byte[] keyb=encoding.ascii.getbytes(this.encryptkey);//得到加密密钥
byte[] toencrypt=this.encodingmode.getbytes(str);//得到要加密的内容
byte[] encrypted;
icryptotransform encryptor=des.createencryptor(keyb,ivb);
memorystream msencrypt=new memorystream();
cryptostream csencrypt=new cryptostream(msencrypt,encryptor,cryptostreammode.write);
csencrypt.write(toencrypt,0,toencrypt.length);
csencrypt.flushfinalblock();
encrypted=msencrypt.toarray();
csencrypt.close();
msencrypt.close();
return this.encodingmode.getstring(encrypted);
}
/**//**//**//// <summary>
/// 加密指定的文件,如果成功返回true,否则false
/// </summary>
/// <param name="filepath">要加密的文件路径</param>
/// <param name="outpath">加密后的文件输出路径</param>
public void encryptfile(string filepath,string outpath)
...{
bool isexist=file.exists(filepath);
if(isexist)//如果存在
...{
byte[] ivb=encoding.ascii.getbytes(this.iv);
byte[] keyb=encoding.ascii.getbytes(this.encryptkey);
//得到要加密文件的字节流
filestream fin=new filestream(filepath,filemode.open,fileaccess.read);
streamreader reader=new streamreader(fin,this.encodingmode);
string datastr=reader.readtoend();
byte[] toencrypt=this.encodingmode.getbytes(datastr);
fin.close();
filestream fout=new filestream(outpath,filemode.create,fileaccess.write);
icryptotransform encryptor=des.createencryptor(keyb,ivb);
cryptostream csencrypt=new cryptostream(fout,encryptor,cryptostreammode.write);
try
...{
//加密得到的文件字节流
csencrypt.write(toencrypt,0,toencrypt.length);
csencrypt.flushfinalblock();
}
catch(exception err)
...{
throw new applicationexception(err.message);
}
finally
...{
try
...{
fout.close();
csencrypt.close();
}
catch
...{
;
}
}
}
else
...{
throw new filenotfoundexception("没有找到指定的文件");
}
}
/**//**//**//// <summary>
/// 文件加密函数的重载版本,如果不指定输出路径,
/// 那么原来的文件将被加密后的文件覆盖
/// </summary>
/// <param name="filepath"></param>
public void encryptfile(string filepath)
...{
this.encryptfile(filepath,filepath);
}
/**//**//**//// <summary>
/// 解密给定的字符串
/// </summary>
/// <param name="str">要解密的字符</param>
/// <returns></returns>
public string decryptstring(string str)
...{
byte[] ivb=encoding.ascii.getbytes(this.iv);
byte[] keyb=encoding.ascii.getbytes(this.encryptkey);
byte[] todecrypt=this.encodingmode.getbytes(str);
byte[] decrypted=new byte[todecrypt.length];
icryptotransform decryptor=des.createdecryptor(keyb,ivb);
memorystream msdecrypt=new memorystream(todecrypt);
cryptostream csdecrypt=new cryptostream(msdecrypt,decryptor,cryptostreammode.read);
try
...{
csdecrypt.read(decrypted,0,decrypted.length);
}
catch(exception err)
...{
throw new applicationexception(err.message);
}
finally
...{
try
...{
msdecrypt.close();
csdecrypt.close();
}
catch...{;}
}
return this.encodingmode.getstring(decrypted);
}
/**//**//**//// <summary>
/// 解密指定的文件
/// </summary>
/// <param name="filepath">要解密的文件路径</param>
/// <param name="outpath">解密后的文件输出路径</param>
public void decryptfile(string filepath,string outpath)
...{
bool isexist=file.exists(filepath);
if(isexist)//如果存在
...{
byte[] ivb=encoding.ascii.getbytes(this.iv);
byte[] keyb=encoding.ascii.getbytes(this.encryptkey);
fileinfo file=new fileinfo(filepath);
byte[] decrypted=new byte[file.length];
//得到要解密文件的字节流
filestream fin=new filestream(filepath,filemode.open,fileaccess.read);
//解密文件
try
...{
icryptotransform decryptor=des.createdecryptor(keyb,ivb);
cryptostream csdecrypt=new cryptostream(fin,decryptor,cryptostreammode.read);
csdecrypt.read(decrypted,0,decrypted.length);
}
catch(exception err)
...{
throw new applicationexception(err.message);
}
finally
...{
try
...{
fin.close();
}
catch...{;}
}
filestream fout=new filestream(outpath,filemode.create,fileaccess.write);
fout.write(decrypted,0,decrypted.length);
fout.close();
}
else
...{
throw new filenotfoundexception("指定的解密文件没有找到");
}
}
/**//**//**//// <summary>
/// 解密文件的重载版本,如果没有给出解密后文件的输出路径,
/// 则解密后的文件将覆盖先前的文件
/// </summary>
/// <param name="filepath"></param>
public void decryptfile(string filepath)
...{
this.decryptfile(filepath,filepath);
}
#endregion
}
/**//**//**//// <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);
}
}
}
最大的网站源码资源下载站,
新闻热点
疑难解答
图片精选