Share via


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文件。 desKey方法的 和 desIV 参数是 8 字节数组。 必须安装高加密包才能运行此示例。

void EncryptData( String^ inName, String^ outName, array<Byte>^aesKey, array<Byte>^aesIV )
{
   
   //Create the file streams to handle the input and output files.
   FileStream^ fin = gcnew FileStream( inName,FileMode::Open,FileAccess::Read );
   FileStream^ fout = gcnew FileStream( outName,FileMode::OpenOrCreate,FileAccess::Write );
   fout->SetLength( 0 );
   
   //Create variables to help with read and write.
   array<Byte>^bin = gcnew array<Byte>(100);
   long rdlen = 0; //This is the total number of bytes written.

   long totlen = (long)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 = gcnew 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 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类加密的数据,必须将 属性和 IV 属性设置为Key用于加密的相同值。 要使对称算法有用,密钥必须只有发送方和接收方知道。

AesDESRC2TripleDES 是对称算法的实现。

请注意,使用派生类时,从安全角度来看,仅仅在对象使用完后强制垃圾回收是不够的。 您必须对 对象显式调用 Clear 方法,以在释放对象之前将对象中的任何敏感数据归零。 请注意,垃圾回收不会将回收的对象的内容归零,而只是将内存标记为可用于重新分配。 因此,垃圾回收对象中包含的数据可能仍存在于未分配内存的内存堆中。 对于加密对象,此数据可能包含敏感信息,例如密钥数据或纯文本块。

.NET Framework中保存敏感数据的所有加密类都实现 方法Clear。 调用时, Clear 方法用零覆盖 对象中的所有敏感数据,然后释放对象,以便可以安全地对其进行垃圾回收。 当对象被归零并释放后,应调用 Dispose 方法,并将 disposing 参数设置为 True 以释放与对象关联的所有托管和非托管资源。

实施者说明

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)
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 占用的非托管资源,还可以另外再释放托管资源。

适用于

另请参阅