- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在现有文件管理程序中实现本地加密。
我可以找到很多示例代码,例如 Microsoft's ,演示了如何直接写入文件,但我需要做的是提供一个在程序其他地方使用的流:
CryptoStream GetEncryptStream(string filename)
{
var rjndl = new RijndaelManaged();
rjndl.KeySize = 256;
rjndl.BlockSize = 256;
rjndl.Mode = CipherMode.CBC;
rjndl.Padding = PaddingMode.PKCS7;
// Open read stream of unencrypted source fileStream:
var fileStream = new FileStream(filename, FileMode.Open);
/* Get key and iv */
var transform = rjndl.CreateEncryptor(key, iv);
// CryptoStream in *read* mode:
var cryptoStream = new CryptoStream(fileStream, transform, CryptoStreamMode.Read);
/* What can I do here to insert the unencrypted IV at the start of the
stream so that the first X bytes returned by cryptoStream.Read are
the IV, before the bytes of the encrypted file are returned? */
return cryptoStream; // Return CryptoStream to be consumed elsewhere
}
我的问题在最后一行的评论中有所概述,但有一个:我如何将 IV 添加到 CryptoStream 的开头,以便它成为读取 CryptoStream 时返回的第一个 X 字节,考虑到何时控制真正开始读取流并写入文件超出了我的代码范围?
最佳答案
好的...既然您的问题很清楚,它“相当”简单...遗憾的是 .NET 不包含合并两个 Stream
的类,但我们可以轻松创建它. MergedStream
是一个只读、只进的多流合并器。
你像这样使用:
var mergedStream = new MergedStream(new Stream[]
{
new MemoryStream(iv),
cryptoStream,
});
现在...当有人试图从 MergedStream
读取数据时,首先会消耗包含 IV 的 MemoryStream
,然后是 cryptoStream
将被消耗。
public class MergedStream : Stream
{
private Stream[] streams;
private int position = 0;
private int currentStream = 0;
public MergedStream(Stream[] streams) => this.streams = streams;
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => streams.Sum(s => s.Length);
public override long Position
{
get => position;
set => throw new NotSupportedException();
}
public override void Flush()
{
}
public override int Read(byte[] buffer, int offset, int count)
{
if (streams == null)
{
throw new ObjectDisposedException(nameof(MergedStream));
}
if (currentStream >= streams.Length)
{
return 0;
}
int read;
while (true)
{
read = streams[currentStream].Read(buffer, offset, count);
position += read;
if (read != 0)
{
break;
}
currentStream++;
if (currentStream == streams.Length)
{
break;
}
}
return read;
}
public override long Seek(long offset, SeekOrigin origin)
=> throw new NotSupportedException();
public override void SetLength(long value)
=> throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count)
=> throw new NotSupportedException();
protected override void Dispose(bool disposing)
{
try
{
if (disposing && streams != null)
{
for (int i = 0; i < streams.Length; i++)
{
streams[i].Close();
}
}
}
finally
{
streams = null;
}
}
}
关于c# - 将 IV 添加到 CryptoStream 的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445749/
我正在为 Windows Phone 7 开发一个存储高度安全信息的项目。此数据受密码保护。如果我在 3 个其他 CryptoStreams 中加密一个 CryptoStream(总共 4 个密码流相
我有兴趣提供一个在 .Net 中使用的托管 dll,它提供经过身份验证的加密服务。该 DLL 可以在 WPF 程序或 ASP 应用程序中使用。我有几个与 Microsoft 的加密和流模型相关的问题。
我正在使用: cryptoStream.Write(buffer, 0, bytesRead); 并想要它的哈希值。如果我使用: hash.TransformBlock(buffer, 0, byte
在解密之前是否可以得到解密数据的最终长度?这是一个例子: RijndaelManaged RMCrypto = new RijndaelManaged(); RMCrypto.Padding = Pa
我正在尝试使用 RijndaelManaged 类加密纯文本,然后解密加密的字符串,最终得到相同的纯文本。 加密明文一切正常,如下所示, protected static string AES
我正在处理的 C# .NET Framework 4.5 代码应该允许我通过加密流将文本传输到另一个程序。我创建了两个简单的程序来演示我的问题。 EncryptionTestA 是服务器,意味着首先运
场景:我们有一个将要离线发布的 silverlight 5 OOB 应用程序 (DVD ROM)。此应用程序用作某些教育内容的媒体播放器。为了保护 DVD ROM 附带的视频文件,我决定用每个用户唯一
过去几天我一直在为一个问题苦思冥想:在进行公钥交换 + 登录后,我的端点切换到 Rijndael 对称加密模式来加密客户端之间的消息和服务器。 问题是,我创建了加密流并将其保持开放状态,认为这是最好的
如果我有一个 CryptoStream 我想传回给用户,天真的方法是 public Stream GetDecryptedFileStream(string inputFile, byte[] key
我试图将 CryptoStream 与 AWS .NET SDK 一起使用,但失败了,因为 CryptoStream 不支持搜索。我在某处阅读了内容长度已知的内容,我们应该能够将这些功能添加到 C
我对 .NET CryptoStream 类的一个怪癖感到困惑:它的 Dispose() 方法读取了密文的末尾以寻找填充它不应该,并因此抛出 CryprographicException。 下面的 C
我写了一个过程,其中文件被加密并上传到 Azure,然后下载过程必须被解密,这是失败的“填充无效且无法删除”错误,或“要解密的数据长度是无效的。”错误。 我在网上尝试了很多解决方案,包括 C# Dec
我正在研究使用 AesCryptoServiceProvider 解密二进制数据的过程。对于最后一步,检索解密的数据并将其作为字节数组返回,我目前正在使用以下实现: let rec streamByt
我正在尝试使用以下代码加密一些数据: public static byte[] EncryptString(byte[] input, string password) { PasswordD
有人知道 .NET 的 CryptoStream 的可寻找替代品吗? 如果替代方案仅支持“读取”模式下的搜索,或者它仅限于例如AES256. 最佳答案 按 block 加密非常安全。只是欧洲央行有问题
我有一个 CryptoStream 和一个底层 Stream。我不能使用 using block 来处理 CryptoStream,因为这也会处理我需要保持打开状态的底层 Stream。解决方案似乎只
我创建了一个MemoryStream,将它传递给CryptoStream 进行写入。我希望 CryptoStream 进行加密,并让 MemoryStream 保持打开状态以便我读取其他内容。但是一旦
编辑:我发现了问题。 我的对象的构造函数正在以写入模式初始化文件流,无论我们是否要加密或解密文件。即使写入文件流在解密之前会被关闭(用于此目的的另一个文件流),它仍然会导致静默崩溃。现在它工作正常。
我在这里有点绝望。我正在尝试将一个带有序列化对象的加密文件写入磁盘,然后检索该文件,对其进行解密并反序列化该对象。 更新:我将代码重构为: using (Stream innerStream = Fi
假设我们有一条使用 HMAC 签名的消息,然后该消息和 HMAC 被加密,然后通过 TCP 套接字发送: // endpoint info excluded TcpClient client = ne
我是一名优秀的程序员,十分优秀!