开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 2932|回复: 1
收起左侧

[C#求助] 关于C#解密返回流的问题

[复制链接]
结帖率:69% (9/13)
发表于 2017-12-11 22:28:36 | 显示全部楼层 |阅读模式   辽宁省锦州市
本帖最后由 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("文件大小不匹配");         
  }        
}

头像被屏蔽
发表于 2018-1-9 19:49:38 | 显示全部楼层   山东省青岛市
同小白 支持
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 诚聘英才| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 800073686,邮箱:800073686@b.qq.com
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表