如何添加寻求和定位功能的CryptoStream [英] How to add seek and position capabilities to CryptoStream

查看:103
本文介绍了如何添加寻求和定位功能的CryptoStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用CryptoStream的AWS与.NET SDK它失败,因为追求的是不支持的CryptoStream。我知道我们应该能够将这些功能添加到CryptoStream的内容长度的地方读取。我想知道如何做到这一点;任何示例代码将是有用的。



我有这样被传递了FieStream并返回的CryptoStream的方法。我将返回的Stream对象AWS SDK PutObjectRequest对象的InputStream。

 公共静态流GetEncryptStream(流existingStream,
SymmetricAlgorithm cryptoServiceProvider,
串的encryptionKey,串encryptionIV)
{
流existingStream = this.dataStream;

cryptoServiceProvider.Key = ASCIIEncoding.ASCII.GetBytes(的encryptionKey);
cryptoServiceProvider.IV = ASCIIEncoding.ASCII.GetBytes(encryptionIV);
的CryptoStream的CryptoStream =新的CryptoStream(existingStream,
cryptoServiceProvider.CreateEncryptor(),CryptoStreamMode.Read);

返回CryptoStream的;
}


解决方案

一般用加密有ISN T A 1:输入字节和输出字节1之间的映射,所以为了谋求的向后的(尤其是)它会做很多的工作 - 甚至打算马上回起点和移动转发处理数据消耗[N]从的解密的流中。即使知道在哪里映射,加密状态的每个字节是依赖于它之前(它不是一个解码器环; P)数据,如此反复 - 它要么必须从一开始就阅读(重新回到初始化向量),或将它必须跟踪的立场和加密状态的快照,并返回到最近的快照,然后往前行走。大量的工作和储存。



这将适用于寻求相对两端,太。



向前移动从电流的位置不会太糟糕,但你又不得不的处理的数据 - 不只是跳基流的地位



有不是的的方式来实现这一点,大多数消费者可以使用 - 通常,如果你收到了真正 CanSeek ,意思是随机访问,但不是在这种情况下,有效的。



作为解决方法 - 考虑到解密的数据复制到一个的MemoryStream 或文件;那么你就可以访问随机访问的方式完全解密数据。


I was trying to use CryptoStream with AWS .NET SDk it failed as seek is not supported on CryptoStream. I read somewhere with content length known we should be able to add these capabilities to CryptoStream. I would like to know how to do this; any sample code will be useful too.

I have a method like this which is passed with a FieStream and returns a cryptoStream. I assign the returned Stream object to InputStream of AWS SDk PutObjectRequest object.

public static Stream GetEncryptStream(Stream existingStream,
    SymmetricAlgorithm cryptoServiceProvider,
    string encryptionKey, string encryptionIV)
{
    Stream existingStream = this.dataStream;

    cryptoServiceProvider.Key = ASCIIEncoding.ASCII.GetBytes(encryptionKey);
    cryptoServiceProvider.IV = ASCIIEncoding.ASCII.GetBytes(encryptionIV);
    CryptoStream cryptoStream = new CryptoStream(existingStream,
        cryptoServiceProvider.CreateEncryptor(), CryptoStreamMode.Read);

    return cryptoStream ;
}

解决方案

Generally with encryption there isn't a 1:1 mapping between input bytes and output bytes, so in order to seek backwards (in particular) it would have to do a lot of work - perhaps even going right back to the start and moving forwards processing the data to consume [n] bytes from the decrypted stream. Even if it knew where each byte mapped to, the state of the encryption is dependent on the data that came before it (it isn't a decoder ring ;p), so again - it would either have to read from the start (and reset back to the initialisation-vector), or it would have to track snapshots of positions and crypto-states, and go back to the nearest snapshot, then walk forwards. Lots of work and storage.

This would apply to seeking relative to either end, too.

Moving forwards from the current position wouldn't be too bad, but again you'd have to process the data - not just jump the base-stream's position.

There isn't a good way to implement this that most consumers could use - normally if you get a true from CanSeek that means "random access", but that is not efficient in this case.

As a workaround - consider copying the decrypted data into a MemoryStream or a file; then you can access the fully decrypted data in a random-access fashion.

这篇关于如何添加寻求和定位功能的CryptoStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆