用AES加密指定文件怎么写? 我想用AES加密指定的单一文件,应该怎么写代码啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public bool EncryptFile(string filePath,string savePath,string keyStr) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); if(keyStr=="") keyStr=key; FileStream fs=File.OpenRead(filePath); byte[] inputByteArray =new byte[fs.Length]; fs.Read(inputByteArray,0,(int)fs.Length); fs.Close(); byte[] keyByteArray=Encoding.Default.GetBytes(keyStr); SHA1 ha=new SHA1Managed(); byte[] hb=ha.ComputeHash(keyByteArray); sKey=new byte[8]; sIV=new byte[8]; for(int i=0;i<8;i++) sKey[i]=hb[i]; for(int i=8;i<16;i++) sIV[i-8]=hb[i]; des.Key=sKey; des.IV=sIV; MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); fs=File.OpenWrite(savePath); foreach(byte b in ms.ToArray()) { fs.WriteByte(b); } fs.Close(); cs.Close(); ms.Close(); return true; } private string key = "123456"; //默认密钥 private byte[] sIV; private byte[] sKey; C#多个线程能同时访问一个方法吗? SVG - Netron C# OleDb打开VFP的问题, 谢谢. 新手求救,负数如何显示? 高分提问:[xxx()]是什么意义,是如何定义的,有什么作用 新手学习C#,ASP.NET,请教有效的学习方法和进程. WINFORM中的窗体关闭按纽 .net 3.5 编程疑难,请解答! C#的语言怎么调用第三方的OCX控件?最好有例子。 C#连接sqlite 出错NotSupportedException 接收UDP丢包 数据库取值填充到Textbox中
public bool EncryptFile(string filePath,string savePath,string keyStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if(keyStr=="")
keyStr=key;
FileStream fs=File.OpenRead(filePath);
byte[] inputByteArray =new byte[fs.Length];
fs.Read(inputByteArray,0,(int)fs.Length);
fs.Close();
byte[] keyByteArray=Encoding.Default.GetBytes(keyStr);
SHA1 ha=new SHA1Managed();
byte[] hb=ha.ComputeHash(keyByteArray);
sKey=new byte[8];
sIV=new byte[8];
for(int i=0;i<8;i++)
sKey[i]=hb[i];
for(int i=8;i<16;i++)
sIV[i-8]=hb[i];
des.Key=sKey;
des.IV=sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms,des.CreateEncryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
fs=File.OpenWrite(savePath);
foreach(byte b in ms.ToArray())
{
fs.WriteByte(b);
}
fs.Close();
cs.Close();
ms.Close();
return true;
}
private string key = "123456"; //默认密钥
private byte[] sIV;
private byte[] sKey;