📄 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 + -