?? des_ed.cs
字號:
using System;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace des
{
/// <summary>
/// des_ed 的摘要說明。
/// </summary>
public class des_ed
{
private byte[] TheKey= new byte[8];
// 在向量中放入一些隨機數據
private byte[] Vector ={0x82, 0x84, 0x16, 0xE6, 0x58, 0x15, 0xED, 0x4E};
public des_ed()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
public string sha1ec(string strKey)
{
byte[] arrByte=new byte[strKey.Length];
ASCIIEncoding AscEncod=new ASCIIEncoding();
int i = 0;
AscEncod.GetBytes(strKey, i, strKey.Length, arrByte, i);
// 獲得密碼的Hash值
SHA1CryptoServiceProvider hashSha=new SHA1CryptoServiceProvider();
//byte[] arrHash=hashSha.ComputeHash(arrByte);
byte[] arrHash=hashSha.ComputeHash(arrByte);
//MessageBox.Show(AscEncod.GetString(arrHash,0,arrByte.Length)+"::"+arrHash.Length.ToString());
return AscEncod.GetString(arrHash,0,arrByte.Length);
}
public void CreateKey(string strKey )
{
// 保存密鑰的字節數組
byte[] arrByte=new byte[8];
ASCIIEncoding AscEncod=new ASCIIEncoding();
int i = 0;
AscEncod.GetBytes(strKey, i, strKey.Length, arrByte, i);
// 獲得密碼的Hash值
SHA1CryptoServiceProvider hashSha=new SHA1CryptoServiceProvider();
byte[] arrHash=hashSha.ComputeHash(arrByte);
//將Hash值保存到密鑰
for(i=0;i<=7;i++)
{
TheKey[i] = arrHash[i];
}
}
public void Encrypt(string inName,string outName)
{
try
{
// 創建緩沖區
byte[] storage=new byte[4096];
// 已經寫入的字節數量
long totalBytesWritten= 8;
// 每次寫入的字節數量
int packageSize=0;
//聲明文件流
FileStream fin=new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout=new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
// 源文件的大小
long totalFileLength= fin.Length;
// 創建加密對象
DESCryptoServiceProvider des=new DESCryptoServiceProvider();
CryptoStream crStream=new CryptoStream(fout,des.CreateEncryptor(TheKey,Vector),CryptoStreamMode.Write);
//輸出加密后的文件
while(totalBytesWritten<totalFileLength+8)
{
packageSize = fin.Read(storage, 0, 4096);
crStream.Write(storage, 0, packageSize);
totalBytesWritten = Convert.ToInt32(totalBytesWritten
+packageSize);//des.BlockSize * des.BlockSize);
}
crStream.Close();
fin.Close();
fout.Close();
// MessageBox.Show("fl:"+totalFileLength.ToString()+" tw:"+totalBytesWritten.ToString()+"ps:"+packageSize.ToString());
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
public void Decrypt(string inName,string outName)
{
try
{
// 創建緩沖區
byte[] storage=new byte[4096];
// 已經寫入的字節數量
long totalBytesWritten= 0;
// 每次寫入的字節數量
int packageSize=0;
//聲明文件流
FileStream fin=new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout=new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
// 源文件的大小
long totalFileLength= fin.Length;
// 創建加密對象
DESCryptoServiceProvider des=new DESCryptoServiceProvider();
CryptoStream crStream=new CryptoStream(fout,des.CreateDecryptor(TheKey,Vector),CryptoStreamMode.Write);
//輸出加密后的文件
while(totalBytesWritten<totalFileLength-8)
{
packageSize = fin.Read(storage, 0, 4096);
crStream.Write(storage, 0, packageSize);
totalBytesWritten = Convert.ToInt32(totalBytesWritten
+packageSize);//des.BlockSize * des.BlockSize);
}
crStream.Close();
fin.Close();
fout.Close();
MessageBox.Show("fl:"+totalFileLength.ToString()+" tw:"+totalBytesWritten.ToString()+"ps:"+packageSize.ToString());
}
catch(Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -