将IV添加到CryptoStream的开头我正在现有文件管理程序中实现本机加密。我能找到的大部分示例代码(如Microsoft)演示了如何直接写入文件,但我需要做的是提供一个流以供在程序的其他地方使用:CryptoStreamGetEncryptStream(stringfilename){varrjndl=newRijndaelManaged();rjndl.KeySize=256;rjndl.BlockSize=256;rjndl.Mode=CipherMode.CBC;rjndl.Padding=PaddingMode.PKCS7;//打开未加密源文件流的读取流:varfileStream=newFileStream(filename,FileMode.Open);/*获取密钥和iv*/vartransform=rjndl.CreateEncryptor(key,iv);//*读取*模式下的CryptoStream:varcryptoStream=newCryptoStream(fileStream,transform,CryptoStreamMode.Read);/*我可以在这里做什么来在流的开头插入未加密的IV,以便cryptoStream返回的前X个字节。在返回加密文件的字节之前读取IV?*/返回加密流;//返回CryptoStream以在别处使用,控制何时实际开始读取流和写入文件是否超出了我的代码范围?好的......现在你的问题很明显,它是“非常“简单......不幸的是,.NET不包含合并两个流的类,但我们可以轻松创建它。MergedStream是只读的,只进的多流合并。你使用类似的东西:varmergedStream=newMergedStream(newStream{newMemoryStream(iv),cryptoStream,}现在...当有人试图从MergedStream中读取时,首先会消耗包含IV的MemoryStream,然后会消耗cryptoStream。公共类MergedStream:Stream{privateStream[]streams;私人int位置=0;私人intcurrentStream=0;publicMergedStream(Stream[]streams){this.streams=streams;}publicoverrideboolCanRead=>true;publicoverrideboolCanSeek=>false;publicoverrideboolCanWrite=>false;publicoverridelongLength=>thrownewNotImplementedException();publicoverridelongPosition{get=>position;设置=>抛出新的NotSupportedException();}publicoverridevoidFlush(){}publicoverrideintRead(byte[]buffer,intoffset,intcount){if(streams==null){thrownewObjectDisposedException(nameof(MergedStream));}if(currentStream>=streams.Length){返回0;}读;while(true){read=streams[currentStream].Read(buffer,offset,count);位置+=阅读;如果(读取!=0){中断;}当前流++;如果(currentStream==streams.Length){break;}}返回读取;}publicoverridelongSeek(longoffset,SeekOriginori杜松子酒){抛出新的NotSupportedException();}publicoverridevoidSetLength(longvalue){thrownewNotSupportedException();}publicoverridevoidWrite(byte[]buffer,intoffset,intcount){thrownewNotSupportedException();protectedoverridevoidDispose(booldisposing){try{if(disposing&&streams!=null){for(inti=0;iusingCryptoStreamtocommunicatebetweentwolocalplacesisnotagooddesignyoushouldusegenericInputStreamorPipe(用于进程间通信)。然后,您可以将IV的MemoryStream和CryptoStream组合起来,并返回组合。请参阅xanatos的回答,了解如何执行此操作(如果需要,您可能仍需要填写Seekfunction)。CryptoStream只能处理密文。因为你需要改变接收器上的代码,如果你想解密,你也可以重构为InputStream。如果你需要保持当前设计,有一个hack。首先使用ECB模式“解密”IV和无填充。由于单个块密码调用总是成功,因此结果将是一个数据块-当使用CipherStream加密时-再次成为IV。步骤:在数组中生成16个随机字节,这将是真正的IV;使用ECB解密16字节IV,没有填充和CipherStream的密钥;使用密钥和全零、16字节IV初始化CipherStream;使用CipherStream加密“解密”IV;输入明文的其余部分。您需要先创建一个接收解密后的IV(作为MemoryStream)然后接收明文的InputStream(作为FileStream)来实现这一点。同样,请参阅xanatos的回答以了解如何执行此操作。或者看看这个组合器和这个HugeStream就好了'StackOverflow。然后使用组合流作为CipherInputStream源。但不用说,应该在最方便的时候记录并删除此类黑客行为。备注:感谢那些花时间回答的人。最后我意识到我必须知道缓冲区代码中的IV长度,没有办法绕过它,所以选择保持简单:加密方法(伪代码):/*获取密钥和IV*/outFileStream.Write(IV);//将IV写入输出流vartransform=rijndaelManaged.CreateEncryptor(key,iv);//读取模式下的CryptoStream:varcryptoStream=newCryptoStream(inFileStream,transform,CryptoStreamMode.Read);做{cryptoStream.Read(chunk,0,blockSize);//获取并加密块outFileStream.Write(chunk);//写入chunk}while(chunk.Length>0)/*清理*/解密方法(伪代码):以上是C#学习教程:将IV添加到CryptoStream开头的所有分享内容,如果有用你和你需要了解更多的C#学习教程,希望你多多关注---/*Getkey*/variv=inFileStream.Read(ivLength);//从输入流中获取IVvartransform=rijndaelManaged.CreateDecryptor(key,iv);//写入模式的CryptoStream:varcryptoStream=newCryptoStream(outFileStream,transform,CryptoStreamMode.Write);做{inFileStream.Read(块,0,blockSize);//获取块cryptoStream。写(块);//解密并写入块}while(chunk.Length>0)/*清理*/本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
