国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

DES(Data Encryption Standard)加密解密整理

2024-07-21 02:24:34
字體:
來源:轉載
供稿:網友
這個類是我在網上參考了幾個文檔總結出來的,測試過可以直接用,后面有一段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

}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 渝中区| 龙井市| 罗源县| 富阳市| 尉犁县| 新丰县| 高州市| 昌都县| 扎鲁特旗| 类乌齐县| 合肥市| 临桂县| 沽源县| 海门市| 茌平县| 乌兰浩特市| 高邑县| 礼泉县| 京山县| 沛县| 阳春市| 尚志市| 长葛市| 瑞安市| 浦县| 城口县| 屏东县| 青浦区| 建德市| 拜城县| 临安市| 福海县| 当雄县| 松桃| 资源县| 石柱| 固原市| 高邑县| 元阳县| 固原市| 涪陵区|