首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET Framework >

文件的加解密疑惑解决方案

2012-03-23 
文件的加解密疑惑网上得如下说明:成员函数EncryptData()负责对指定的文件进行加密,主要步骤如下:根据从用

文件的加解密疑惑
网上得如下说明:
成员函数EncryptData()负责对指定的文件进行加密,主要步骤如下:
根据从用户界面获取的_sourceFile和_saveFile,创建输入输出流
FileStream fin = new FileStream(_sourceFile, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(_saveFile, FileMode.OpenOrCreate, FileAccess.Write);
创建DESCryptoServiceProvider类的对象
DES des = new DESCryptoServiceProvider();
调用CreateEncryptor方法创建加密器对象,并将输入流套接成为加密流
CryptoStream encryptStream = new CryptoStream(fout, des.CreateEncryptor(_DESKey, _DESIV),CryptoStreamMode.Write);
按每200字节块进行加密
byte[] readBuffer = new byte[200];
len = fin.Read(readBuffer, 0, readBuffer.Length) encryptStream.Write(readBuffer, 0, len);
清除所有敏感数据
des.Clear();

如上:大侠请指点
  1、将输入流套接成为加密流
  CryptoStream encryptStream = new CryptoStream(fout, des.CreateEncryptor(_DESKey, _DESIV),CryptoStreamMode.Write);
其中,fout就是一个创建的文件,何以作为输入流加密?
  2、len = fin.Read(readBuffer, 0, readBuffer.Length) encryptStream.Write(readBuffer, 0, len);此输出吗,可是无法理解是在哪里将流输入到指定文件,并形成文件的?

  恳求高人指点!!!


[解决办法]
#region 加密
const string KEY_64 = "VavicApp";//注意了,是8个字符,64位

const string IV_64 = "VavicApp";
static public string Encode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
int i = cryptoProvider.KeySize;
MemoryStream ms = new MemoryStream();
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write);

StreamWriter sw = new StreamWriter(cst);
sw.Write(data);
sw.Flush();
cst.FlushFinalBlock();
sw.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);

}
#endregion

#region 解密
static public string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);

byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}

DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
#endregion

[解决办法]
加密数据与原来肯定不一样,文件大小肯定改变。
如a加密后数据大小长度肯定不同
,页面的viewstate就是用base64加密的
多出的部分数据是什么数据,原来数据是否一致
当Convert.FromBase64String方法的参数s的长度小于 4 或不是 4 的偶数倍时,将会抛出FormatException。

 public string Encrypto(string Source)
{
byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
MemoryStream ms = new MemoryStream();
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
cs.Write(bytIn, 0, bytIn.Length);
cs.FlushFinalBlock();
ms.Close();
byte[] bytOut = ms.ToArray();
return Convert.ToBase64String(bytOut);


}
/// 解密方法
public string Decrypto(string Source)
{
byte[] bytIn = Convert.FromBase64String(Source);
MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
mobjCryptoService.Key = GetLegalKey();
mobjCryptoService.IV = GetLegalIV();
ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}

热点排行