SymmetricAlgorithm 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示对称算法的所有实现必须继承的抽象基类。
public ref class SymmetricAlgorithm abstract : IDisposable
public abstract class SymmetricAlgorithm : IDisposable
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class SymmetricAlgorithm : IDisposable
type SymmetricAlgorithm = class
interface IDisposable
[<System.Runtime.InteropServices.ComVisible(true)>]
type SymmetricAlgorithm = class
interface IDisposable
Public MustInherit Class SymmetricAlgorithm
Implements IDisposable
- 继承
-
SymmetricAlgorithm
- 派生
- 属性
- 实现
示例
下面的代码示例使用 Aes 具有指定 Key 属性和初始化向量(IV)的类来加密指定的 inName文件,并将加密结果输出到指定的 outName文件。
desIV方法desKey的参数是 8 字节数组。 必须安装高加密包才能运行此示例。
private static void EncryptData(string inName, string outName, byte[] aesKey, byte[] aesIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
Aes aes = Aes.Create();
CryptoStream encStream = new CryptoStream(fout, aes.CreateEncryptor(aesKey, aesIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting...");
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
encStream.Close();
fout.Close();
fin.Close();
}
Private Shared Sub EncryptData(inName As String, outName As String, _
rijnKey() As Byte, rijnIV() As Byte)
'Create the file streams to handle the input and output files.
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
FileAccess.Write)
fout.SetLength(0)
'Create variables to help with read and write.
Dim bin(100) As Byte 'This is intermediate storage for the encryption.
Dim rdlen As Long = 0 'This is the total number of bytes written.
Dim totlen As Long = fin.Length 'Total length of the input file.
Dim len As Integer 'This is the number of bytes to be written at a time.
'Creates the default implementation, which is RijndaelManaged.
Dim rijn As SymmetricAlgorithm = SymmetricAlgorithm.Create()
Dim encStream As New CryptoStream(fout, _
rijn.CreateEncryptor(rijnKey, rijnIV), CryptoStreamMode.Write)
Console.WriteLine("Encrypting...")
'Read from the input file, then encrypt and write to the output file.
While rdlen < totlen
len = fin.Read(bin, 0, 100)
encStream.Write(bin, 0, len)
rdlen = Convert.ToInt32(rdlen + len)
Console.WriteLine("{0} bytes processed", rdlen)
End While
encStream.Close()
fout.Close()
fin.Close()
End Sub
注解
派生自类的 SymmetricAlgorithm 类使用名为密码块链的链模式(CBC),这需要密钥(Key)和初始化向量(IV)对数据执行加密转换。 若要解密使用SymmetricAlgorithm类之一加密的数据,必须将Key属性和IV属性设置为加密时所用的相同值。 若要使对称算法有用,机密密钥必须仅对发送方和接收方知道。
Aes、 DES、 RC2和 TripleDES 对称算法的实现。
请注意,使用派生类时,从安全角度来看,仅仅在使用完对象后强制垃圾回收是不够的。 在释放对象之前,必须显式调用 Clear 对象上的方法以将对象中的任何敏感数据从零出来。 请注意,垃圾回收不会将收集的对象的内容从零到,而只是将内存标记为可用于重新分配。 因此,垃圾回收对象中包含的数据可能仍存在于未分配内存中的内存堆中。 对于加密对象,此数据可能包含敏感信息,例如密钥数据或纯文本块。
保存敏感数据的 .NET Framework 中的所有加密类都实现了一种方法 Clear 。 调用时,该方法 Clear 会用零覆盖对象中的所有敏感数据,然后释放该对象,以便可以安全地进行垃圾回收。 当对象为零并释放时,应调用Dispose参数设置为True释放与对象关联的所有托管和非托管资源的方法disposing。
实施者说明
从 SymmetricAlgorithm 类继承时,必须重写以下成员: CreateDecryptor(Byte[], Byte[])、 CreateEncryptor(Byte[], Byte[])、 GenerateIV()和 GenerateKey()。
构造函数
| 名称 | 说明 |
|---|---|
| SymmetricAlgorithm() |
初始化 SymmetricAlgorithm 类的新实例。 |
字段
| 名称 | 说明 |
|---|---|
| BlockSizeValue |
表示加密操作的块大小(以位为单位)。 |
| FeedbackSizeValue |
表示加密操作的反馈大小(以位为单位)。 |
| IVValue |
表示对称算法的初始化向量(IV)。 |
| KeySizeValue |
表示对称算法使用的密钥的大小(以位为单位)。 |
| KeyValue |
表示对称算法的密钥。 |
| LegalBlockSizesValue |
指定对称算法支持的块大小(以位为单位)。 |
| LegalKeySizesValue |
指定对称算法支持的密钥大小(以位为单位)。 |
| ModeValue |
表示对称算法中使用的密码模式。 |
| PaddingValue |
表示对称算法中使用的填充模式。 |
属性
| 名称 | 说明 |
|---|---|
| BlockSize |
获取或设置加密操作的块大小(以位为单位)。 |
| FeedbackSize |
获取或设置密码反馈(CFB)和输出反馈(OFB)密码模式的加密操作的反馈大小(以位为单位)。 |
| IV |
获取或设置对称算法的初始化向量(IV)。 |
| Key |
获取或设置对称算法的密钥。 |
| KeySize |
获取或设置对称算法使用的密钥的大小(以位为单位)。 |
| LegalBlockSizes |
获取对称算法支持的块大小(以位为单位)。 |
| LegalKeySizes |
获取对称算法支持的密钥大小(以位为单位)。 |
| Mode |
获取或设置对称算法操作的模式。 |
| Padding |
获取或设置对称算法中使用的填充模式。 |
方法
显式接口实现
| 名称 | 说明 |
|---|---|
| IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。 释放由托管资源使用 SymmetricAlgorithm 的非托管资源,并选择性地释放托管资源。 |