通过


SymmetricAlgorithm 类

定义

表示对称算法的所有实现必须继承的抽象基类。

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属性设置为加密时所用的相同值。 若要使对称算法有用,机密密钥必须仅对发送方和接收方知道。

AesDESRC2TripleDES 对称算法的实现。

请注意,使用派生类时,从安全角度来看,仅仅在使用完对象后强制垃圾回收是不够的。 在释放对象之前,必须显式调用 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

获取或设置对称算法中使用的填充模式。

方法

名称 说明
Clear()

释放类使用 SymmetricAlgorithm 的所有资源。

Create()
已过时.
已过时.

创建用于执行对称算法的默认加密对象。

Create(String)
已过时.

创建用于执行对称算法的指定加密对象。

CreateDecryptor()

使用当前 Key 属性和初始化向量创建对称解密器对象(IV)。

CreateDecryptor(Byte[], Byte[])

在派生类中重写时,使用指定的 Key 属性和初始化向量创建对称解密器对象(IV)。

CreateEncryptor()

使用当前 Key 属性和初始化向量IV () 创建对称加密器对象。

CreateEncryptor(Byte[], Byte[])

在派生类中重写时,使用指定的 Key 属性和初始化向量创建对称加密器对象(IV)。

DecryptCbc(Byte[], Byte[], PaddingMode)

使用具有指定填充模式的 CBC 模式解密数据。

DecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode)

使用具有指定填充模式的 CBC 模式解密数据。

DecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

使用具有指定填充模式的 CBC 模式将数据解密到指定的缓冲区。

DecryptCfb(Byte[], Byte[], PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式解密数据。

DecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式解密数据。

DecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式将数据解密到指定的缓冲区中。

DecryptEcb(Byte[], PaddingMode)

使用具有指定填充模式的 ECB 模式解密数据。

DecryptEcb(ReadOnlySpan<Byte>, PaddingMode)

使用具有指定填充模式的 ECB 模式解密数据。

DecryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

使用具有指定填充模式的 ECB 模式将数据解密到指定的缓冲区。

Dispose()

释放类的 SymmetricAlgorithm 当前实例使用的所有资源。

Dispose(Boolean)

释放由托管资源使用 SymmetricAlgorithm 的非托管资源,并选择性地释放托管资源。

EncryptCbc(Byte[], Byte[], PaddingMode)

使用具有指定填充模式的 CBC 模式加密数据。

EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode)

使用具有指定填充模式的 CBC 模式加密数据。

EncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

使用具有指定填充模式的 CBC 模式将数据加密到指定的缓冲区。

EncryptCfb(Byte[], Byte[], PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式加密数据。

EncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式加密数据。

EncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

使用具有指定填充模式和反馈大小的 CFB 模式将数据加密到指定的缓冲区中。

EncryptEcb(Byte[], PaddingMode)

使用具有指定填充模式的 ECB 模式加密数据。

EncryptEcb(ReadOnlySpan<Byte>, PaddingMode)

使用具有指定填充模式的 ECB 模式加密数据。

EncryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode)

使用具有指定填充模式的 ECB 模式将数据加密到指定的缓冲区中。

Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
Finalize()

此成员替代 Finalize(),并且该主题中可能提供了更完整的文档。

Object允许在垃圾回收回收之前Object尝试释放资源并执行其他清理作。

GenerateIV()

在派生类中重写时,生成用于算法的随机初始化向量(IV)。

GenerateKey()

在派生类中重写时,生成用于算法的随机键(Key)。

GetCiphertextLengthCbc(Int32, PaddingMode)

获取在 CBC 模式下具有给定填充模式和纯文本长度的密码文本的长度。

GetCiphertextLengthCfb(Int32, PaddingMode, Int32)

获取具有给定填充模式的密码文本长度和 CFB 模式下的纯文本长度。

GetCiphertextLengthEcb(Int32, PaddingMode)

获取具有给定填充模式的密码文本长度和 ECB 模式下的纯文本长度。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
SetKey(ReadOnlySpan<Byte>)

设置此实例的键。

SetKeyCore(ReadOnlySpan<Byte>)

设置此实例的键。

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
TryDecryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode)

尝试使用具有指定填充模式的 CBC 模式将数据解密到指定的缓冲区。

TryDecryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

在派生类中重写时,尝试使用具有指定填充模式的 CBC 模式将数据解密到指定的缓冲区。

TryDecryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode, Int32)

尝试使用具有指定填充模式和反馈大小的 CFB 模式,将数据解密到指定的缓冲区中。

TryDecryptCfbCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32, Int32)

在派生类中重写时,尝试使用具有指定填充模式和反馈大小的 CFB 模式将数据解密到指定的缓冲区中。

TryDecryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

尝试使用具有指定填充模式的 ECB 模式将数据解密到指定的缓冲区。

TryDecryptEcbCore(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

在派生类中重写时,尝试使用具有指定填充模式的 ECB 模式将数据解密到指定的缓冲区。

TryEncryptCbc(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode)

尝试使用具有指定填充模式的 CBC 模式将数据加密到指定的缓冲区。

TryEncryptCbcCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

在派生类中重写时,尝试使用具有指定填充模式的 CBC 模式将数据加密到指定的缓冲区。

TryEncryptCfb(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, PaddingMode, Int32)

尝试使用具有指定填充模式和反馈大小的 CFB 模式将数据加密到指定的缓冲区中。

TryEncryptCfbCore(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32, Int32)

在派生类中重写时,尝试使用具有指定填充模式和反馈大小的 CFB 模式将数据加密到指定的缓冲区中。

TryEncryptEcb(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

尝试使用具有指定填充模式的 ECB 模式将数据加密到指定的缓冲区。

TryEncryptEcbCore(ReadOnlySpan<Byte>, Span<Byte>, PaddingMode, Int32)

在派生类中重写时,尝试使用具有指定填充模式的 ECB 模式将数据加密到指定的缓冲区。

ValidKeySize(Int32)

确定指定的密钥大小是否对当前算法有效。

显式接口实现

名称 说明
IDisposable.Dispose()

此 API 支持产品基础结构,不能在代码中直接使用。

释放由托管资源使用 SymmetricAlgorithm 的非托管资源,并选择性地释放托管资源。

适用于

另请参阅