目录
二、CryptoStream.Read(Byte[], Int32, Int32) 方法
2.实例:用CryptoStreamMode.Read来加密
三、CryptoStream.Write(Byte[], Int32, Int32) 方法
2.实例:使用CryptoStreamMode.Write来加密
命名空间:
System.Security.Cryptography
程序集:
System.Security.Cryptography.dll
一、使用Read或Write模式都可以实现加密操作
一般理解认为加密、解密需要两个ICryptoTransform,通过 SymmetricAlgorithm类的CreateEncryptor和CreateDecryptor方法来创建。而用CryptoStream操作这两个ICryptoTransform时,加密就用CryptoStreamMode.Write,解密则用Read,这个是合情合理的,但是其实CryptoStream的操作模式不是局限于如此。
其实,Read和Write仅仅是把ICryptoTransform操作的两个数据状态换了个位置,最终操作仍然是将ICryptoTransform的源数据转换成目标数据(单向),只不过不同的模式操作不一样。
使用Read或Write模式都可以实现加密操作,请看下面的实例吧。
二、CryptoStream.Read(Byte[], Int32, Int32) 方法
1.定义
从当前流读取字节序列,并将流中的位置向前移动读取的字节数。
public override int Read (byte[] buffer, int offset, int count);
参数
buffer Byte[]
字节数组。 从当前流中最多读取 count 个字节,并将它们存储在 buffer 中。
offset Int32
buffer 中的字节偏移量,从该偏移量开始存储从当前流中读取的数据。
count Int32
要从当前流中最多读取的字节数。
返回
Int32
读入缓冲区中的总字节数。 如果当前可用的字节数没有请求的字节数那么多,则总字节数可能小于请求的字节数;如果已到达流的末尾,则为零。
例外
NotSupportedException
与当前 CryptoStreamMode 对象关联的 CryptoStream 与基础流不匹配。 例如,对只写的基础流使用 Read 时会引发此异常。
ArgumentOutOfRangeException
offset 参数小于零。
- 或 -
count 参数小于零。
ArgumentException
count 参数和 offset 参数的和比缓冲区的长度长。
2.实例:用CryptoStreamMode.Read来加密
//用CryptoStreamMode.Read也可以完成加密
using System.Security.Cryptography;
using System.Text;
namespace CryptoStreamRead
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
var data = Encoding.Unicode.GetBytes("Mgen!");
using var aes = Aes.Create();
Console.WriteLine(BitConverter.ToString(Encrypt_Read(aes.CreateEncryptor(), data).ToArray()));
}
static MemoryStream Encrypt_Read(ICryptoTransform ict, byte[] data)
{
using var ms = new MemoryStream(data);
using var cstream = new CryptoStream(ms, ict, CryptoStreamMode.Read);
using var destMs = new MemoryStream();
byte[] buffer = new byte[100];
int readLen;
while ((readLen = cstream.Read(buffer, 0, 100)) > 0)
destMs.Write(buffer, 0, readLen);
return destMs;
}
}
}
// 运行结果:
/*
8D-C0-54-88-1F-62-14-5B-01-06-8D-6E-4D-E5-73-7A
*/
三、CryptoStream.Write(Byte[], Int32, Int32) 方法
将一字节序列写入当前的 CryptoStream,并将通过写入的字节数确定该流的当前位置向前移动的量。
1.定义
public override void Write (byte[] buffer, int offset, int count);
参数
buffer Byte[]
字节数组。 此方法将 count 个字节从 buffer 复制到当前流。
offset Int32
buffer 中的字节偏移量,从此偏移量开始将字节复制到当前流。
count Int32
要写入当前流的字节数。
例外
NotSupportedException
与当前 CryptoStreamMode 对象关联的 CryptoStream 与基础流不匹配。 例如,对只读的基础流使用 Write 时会引发此异常。
ArgumentOutOfRangeException
offset 参数小于零。
或 - count 参数小于零。
ArgumentException
count 参数和 offset 参数的和比缓冲区的长度长。
2.实例:使用CryptoStreamMode.Write来加密
// 使用CryptoStreamMode.Write来加密
//+ using System.IO;
//+ using System.Security.Cryptography;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApp10
{
internal class Program
{
private static void Main(string[] args)
{
ArgumentNullException.ThrowIfNull(args);
var data = Encoding.Unicode.GetBytes("Mgen!");
using var aes = Aes.Create();
Console.WriteLine(BitConverter.ToString(Encrypt_Write(aes.CreateEncryptor(), data).ToArray()));
}
static MemoryStream Encrypt_Write(ICryptoTransform ict, byte[] data)
{
using var ms = new MemoryStream();
using var cstream = new CryptoStream(ms, ict, CryptoStreamMode.Write);
cstream.Write(data, 0, data.Length);
return ms;
}
}
}
//运行结果:
/*
F9-DE-73-1C-4B-0C-0F-86-01-5A-48-23-D4-3D-E2-39
*/