這個類是我在網上參考了幾個文檔總結出來的,測試過可以直接用,后面有一段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
}
}