|

本帖最后由 1623863129 于 2017-12-11 22:34 编辑
我用的是https://www.cnblogs.com/lged/p/5713560.html这篇博客代码,主要是解密这个我想返回stream不知道怎么搞,下面是我改的,错了,请大神,改的地方加了序号
public static Stream DecryptFile(string inFile, string password)
{
// 创建打开文件流
using (FileStream fin = File.OpenRead(inFile))
{
Stream fout=null;//需要返回的流 (1)加了一个流
int size = (int)fin.Length;
byte[] bytes = new byte[BUFFER_SIZE];
int read = -1;
int value = 0;
int outValue = 0;
byte[] IV = new byte[16];
fin.Read(IV, 0, 16);
byte[] salt = new byte[16];
fin.Read(salt, 0, 16);
SymmetricAlgorithm sma = DESFileClass.CreateRijndael(password, salt);
sma.IV = IV;
value = 32;
long lSize = -1;
// 创建散列对象, 校验文件
HashAlgorithm hasher = SHA256.Create();
using (CryptoStream cin = new CryptoStream(fin, sma.CreateDecryptor(), CryptoStreamMode.Read),
chash = new CryptoStream(Stream.Null, hasher, CryptoStreamMode.Write))
{
// 读取文件长度
BinaryReader br = new BinaryReader(cin);
lSize = br.ReadInt64();
ulong tag = br.ReadUInt64();
if (FC_TAG != tag)
throw new CryptoHelpException("文件被破坏");
long numReads = lSize / BUFFER_SIZE;
long slack = (long)lSize % BUFFER_SIZE;
for (int i = 0; i < numReads; ++i)
{
read = cin.Read(bytes, 0, bytes.Length);
fout.Write(bytes, 0, read);//我换成fout.Read(bytes,0,read)和fout.Read(bytes,outValue,read)都不行 (2)改了无效果,不会啊
chash.Write(bytes, 0, read);
value += read;
outValue += read;
}
if (slack > 0)
{
read = cin.Read(bytes, 0, (int)slack);
fout.Write(bytes, 0, read);//我换成fout.Read(bytes,0,read)和fout.Read(bytes,outValue,read)都不行 (3)
chash.Write(bytes, 0, read);
value += read;
outValue += read;
}
chash.Flush();
chash.Close();
// fout.Flush();
// fout.Close();
return fout; (4)返回流,后面都不要了
// byte[] curHash = hasher.Hash;
// // 获取比较和旧的散列对象
// byte[] oldHash = new byte[hasher.HashSize / 8];
// read = cin.Read(oldHash, 0, oldHash.Length);
// if ((oldHash.Length != read) || (!CheckByteArrays(oldHash, curHash)))
// throw new CryptoHelpException("文件被破坏");
}
//if (outValue != lSize)
// throw new CryptoHelpException("文件大小不匹配");
}
}
|
|