CryptoStream:为什么要使用CryptoStreamMode.Write进行加密,并选择CryptoStreamMode.Read进行解密? [英] CryptoStream: Why CryptoStreamMode.Write to encrypt and CryptoStreamMode.Read to decrypt?

查看:737
本文介绍了CryptoStream:为什么要使用CryptoStreamMode.Write进行加密,并选择CryptoStreamMode.Read进行解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让e ='password',我正在将其转换为CryptoStream中的'as9kio0736'.

Let e = 'password' and I am transforming it to 'as9kio0736' in a CryptoStream.

让d ='as9kio0736',然后将其转换为'CryptoStream中的密码.

Let d = 'as9kio0736' and I am transforming it to 'password in a CryptoStream.

当我将d转换回'password'时,为什么不考虑将其写在CryptoStream中?

When I am transforming d back to 'password' why is it not considered writing in a CryptoStream?

using (MemoryStream msEncrypt = new MemoryStream()) {
    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) {
        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) {
            swEncrypt.Write(plainText);
        }
    }
}

using (MemoryStream msDecrypt = new MemoryStream(cipherText)) {
    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
        using (StreamReader srDecrypt = new StreamReader(csDecrypt)) {
            plaintext = srDecrypt.ReadToEnd();
        }
    }
}

推荐答案

CryptoStream设计为仅执行从一个流到另一个流的转换,并允许转换链接.例如,您可以加密数据流,然后Base 64对加密输出进行编码.

CryptoStream is designed to perform transformation from a stream to another stream only and allows transformations chaining. For instance you can encrypt a data stream then Base 64 encode the encryption output.

根据要写入转换输入流还是要从转换输出流读取选择模式.

Chose the mode depending on whether you want to write to the transformation input stream or read from the transformation output stream.

CryptoStream文档具有误导性.
第一个CrytoStream构造函数参数描述为:
"要在其上执行密码转换的流"

CryptoStream documentation is misleading.
The first CrytoStream constructor argument is described as:
"The stream on which to perform the cryptographic transformation"

如果构造函数的第三个参数值为CryptoStreamMode.Read,则此描述正确.

This description is ok if constructor third argument value is CryptoStreamMode.Read.

但是如果第三个参数值为CryptoStreamMode.Write,则第一个构造函数参数说明应为:
"将密码转换结果写入其上的流"

But if third argument value is CryptoStreamMode.Write the first constructor argument description should be:
"The stream on which the result of cryptographic transformation is written to"

此外,文档中没有明确提及如果使用CryptoStreamMode.Write,则必须在完成编写后在CryptoStream对象上调用FlushFinalBlock.

Also, documentation does not mention clearly that if you use CryptoStreamMode.Write, you MUST call FlushFinalBlock on your CryptoStream object after you finish writing.

总结一下:

写入转换输入流:

CryptoStream构造函数参数:

CryptoStream constructor arguments:

  • 参数1:目标流
  • 参数3:CryptoStreamMode.Write

CryptoStream对象使用:

CryptoStream object use:

  • 将数据写入CryptoStream对象
  • 在CryptoStream对象上调用FlushFinalBlock

从转换输出流中读取:

CryptoStream构造函数参数:

CryptoStream constructor arguments:

  • 参数1:源流
  • 参数3:CryptoStreamMode.Read

CryptoStream对象使用:

CryptoStream object use:

  • 从CryptoStream对象读取数据,直到到达流末尾

这篇关于CryptoStream:为什么要使用CryptoStreamMode.Write进行加密,并选择CryptoStreamMode.Read进行解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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