这个类是我在网上参考了几个文档总结出来的,测试过可以直接用,后面有一段md5的,应该独立成一个类的,我懒,所以测试的时候就写到一个文件里了,感觉还是满实用的,如果有什么机密文件,就用这个东西处理一下,将来要看的时候再反过来处理一下,只是你不要忘记了密码就对了,如果你跟我一样懒,你直接把下面的代码拷贝下来直接用吧。
using system;
using system.io;
using system.text;
using system.security.cryptography;
using system.web;
namespace test.com
{
/// <summary>
/// desencryptor 的摘要说明。
/// </summary>
public class desencryptor
{
#region 私有成员
/// <summary>
/// 输入字符串
/// </summary>
private string inputstring=null;
/// <summary>
/// 输出字符串
/// </summary>
private string outstring=null;
/// <summary>
/// 输入文件路径
/// </summary>
private string inputfilepath=null;
/// <summary>
/// 输出文件路径
/// </summary>
private string outfilepath=null;
/// <summary>
/// 加密密钥
/// </summary>
private string encryptkey=null;
/// <summary>
/// 解密密钥
/// </summary>
private string decryptkey=null;
/// <summary>
/// 提示信息
/// </summary>
private string notemessage=null;
#endregion
#region 公共属性
/// <summary>
/// 输入字符串
/// </summary>
public string inputstring
{
get{return inputstring;}
set{inputstring=value;}
}
/// <summary>
/// 输出字符串
/// </summary>
public string outstring
{
get{return outstring;}
set{outstring=value;}
}
/// <summary>
/// 输入文件路径
/// </summary>
public string inputfilepath
{
get{return inputfilepath;}
set{inputfilepath=value;}
}
/// <summary>
/// 输出文件路径
/// </summary>
public string outfilepath
{
get{return outfilepath;}
set{outfilepath=value;}
}
/// <summary>
/// 加密密钥
/// </summary>
public string encryptkey
{
get{return encryptkey;}
set{encryptkey=value;}
}
/// <summary>
/// 解密密钥
/// </summary>
public string decryptkey
{
get{return decryptkey;}
set{decryptkey=value;}
}
/// <summary>
/// 错误信息
/// </summary>
public string notemessage
{
get{return notemessage;}
set{notemessage=value;}
}
#endregion
#region 构造函数
public desencryptor()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#endregion
#region des加密字符串
/// <summary>
/// 加密字符串
/// 注意:密钥必须为8位
/// </summary>
/// <param name="strtext">字符串</param>
/// <param name="encryptkey">密钥</param>
public void desencrypt()
{
byte[] bykey=null;
byte[] iv= {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
try
{
bykey = system.text.encoding.utf8.getbytes(this.encryptkey.substring(0,8));
descryptoserviceprovider des = new descryptoserviceprovider();
byte[] inputbytearray = encoding.utf8.getbytes(this.inputstring);
memorystream ms = new memorystream();
cryptostream cs = new cryptostream(ms, des.createencryptor(bykey, iv), cryptostreammode.write) ;
cs.write(inputbytearray, 0, inputbytearray.length);
cs.flushfinalblock();
this.outstring=convert.tobase64string(ms.toarray());
}
catch(system.exception error)
{
this.notemessage=error.message;
}
}
#endregion
#region des解密字符串
/// <summary>
/// 解密字符串
/// </summary>
/// <param name="this.inputstring">加了密的字符串</param>
/// <param name="decryptkey">密钥</param>
public void desdecrypt()
{
byte[] bykey = null;
byte[] iv= {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
byte[] inputbytearray = new byte[this.inputstring.length];
try
{
bykey = system.text.encoding.utf8.getbytes(decryptkey.substring(0,8));
descryptoserviceprovider des = new descryptoserviceprovider();
inputbytearray = convert.frombase64string(this.inputstring);
memorystream ms = new memorystream();
cryptostream cs = new cryptostream(ms, des.createdecryptor(bykey, iv), cryptostreammode.write);
cs.write(inputbytearray, 0, inputbytearray.length);
cs.flushfinalblock();
system.text.encoding encoding = new system.text.utf8encoding();
this.outstring=encoding.getstring(ms.toarray());
}
catch(system.exception error)
{
this.notemessage=error.message;
}
}
#endregion
#region des加密文件
/// <summary>
/// des加密文件
/// </summary>
/// <param name="this.inputfilepath">源文件路径</param>
/// <param name="this.outfilepath">输出文件路径</param>
/// <param name="encryptkey">密钥</param>
public void filedesencrypt()
{
byte[] bykey=null;
byte[] iv= {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
try
{
bykey = system.text.encoding.utf8.getbytes(this.encryptkey.substring(0,8));
filestream fin = new filestream(this.inputfilepath, filemode.open, fileaccess.read);
filestream fout = new filestream(this.outfilepath, filemode.openorcreate, fileaccess.write);
fout.setlength(0);
//create variables to help with read and write.
byte[] bin = new byte[100]; //this is intermediate storage for the encryption.
long rdlen = 0; //this is the total number of bytes written.
long totlen = fin.length; //this is the total length of the input file.
int len; //this is the number of bytes to be written at a time.
des des = new descryptoserviceprovider();
cryptostream encstream = new cryptostream(fout, des.createencryptor(bykey, iv), cryptostreammode.write);
//read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.read(bin, 0, 100);
encstream.write(bin, 0, len);
rdlen = rdlen + len;
}
encstream.close();
fout.close();
fin.close();
}
catch(system.exception error)
{
this.notemessage=error.message.tostring();
}
}
#endregion
#region des解密文件
/// <summary>
/// 解密文件
/// </summary>
/// <param name="this.inputfilepath">加密了的文件路径</param>
/// <param name="this.outfilepath">输出文件路径</param>
/// <param name="decryptkey">密钥</param>
public void filedesdecrypt()
{
byte[] bykey = null;
byte[] iv= {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
try
{
bykey = system.text.encoding.utf8.getbytes(decryptkey.substring(0,8));
filestream fin = new filestream(this.inputfilepath, filemode.open, fileaccess.read);
filestream fout = new filestream(this.outfilepath, filemode.openorcreate, fileaccess.write);
fout.setlength(0);
//create variables to help with read and write.
byte[] bin = new byte[100]; //this is intermediate storage for the encryption.
long rdlen = 0; //this is the total number of bytes written.
long totlen = fin.length; //this is the total length of the input file.
int len; //this is the number of bytes to be written at a time.
des des = new descryptoserviceprovider();
cryptostream encstream = new cryptostream(fout, des.createdecryptor(bykey, iv), cryptostreammode.write);
//read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.read(bin, 0, 100);
encstream.write(bin, 0, len);
rdlen = rdlen + len;
}
encstream.close();
fout.close();
fin.close();
}
catch(system.exception error)
{
this.notemessage=error.message.tostring();
}
}
#endregion
#region md5
/// <summary>
/// md5 encrypt
/// </summary>
/// <param name="strtext">text</param>
/// <returns>md5 encrypt string</returns>
public void md5encrypt()
{
md5 md5 = new md5cryptoserviceprovider();
byte[] result = md5.computehash(system.text.encoding.default.getbytes(this.inputstring));
this.outstring=system.text.encoding.default.getstring(result);
}
#endregion
}
}