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 類別加密的數據,您必須將 Key 屬性和 IV 屬性設定為用於加密的相同值。 若要讓對稱演算法很有用,只有傳送者和接收者才知道秘密密鑰。

AesDESRC2TripleDES 是對稱演算法的實作。

請注意,使用衍生類別時,從安全性觀點來看,只要在使用 物件完成之後強制垃圾收集就不夠。 您必須在對象上明確呼叫 Clear 方法,以在釋放物件之前,將物件內的任何敏感數據清除為零。 請注意,垃圾收集不會將所收集對象的內容零,只是將記憶體標示為可供重新配置。 因此,垃圾收集物件中包含的數據可能仍存在於未配置記憶體中的記憶體堆積中。 在密碼編譯對象的情況下,此數據可能包含機密資訊,例如金鑰數據或純文字區塊。

保存敏感數據之 .NET Framework 中的所有密碼編譯類別都會實作 Clear 方法。 呼叫時, Clear 方法會以零覆寫物件內的所有敏感數據,然後釋放 物件,以便安全地進行垃圾收集。 當物件已零並釋放時,您應該呼叫 Dispose 方法, disposing 並將 參數設為 True ,以處置與 對象相關聯的所有 Managed 和 Unmanaged 資源。

給實施者的注意事項

當您繼承自 類別時 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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

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(),您可以在該主題中找到更完整的文件。

在記憶體回收 (GC) 回收 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 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

適用於

另請參閱