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 ,並將加密的結果輸出至所指定的檔案 outNamedesKey方法的和 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 使用名為 cipher 區塊連結 (CBC) 的連結模式,這需要索引鍵 (Key) 和初始化向量 (IV) 來執行資料的密碼編譯轉換。 若要使用其中一個類別來解密已加密的資料 SymmetricAlgorithm ,您必須將 Key 屬性和 IV 屬性設定為加密所用的相同值。 若要讓對稱演算法很有用,秘密金鑰必須只有傳送者和接收者才知道。

AesDESRC2TripleDES 都是對稱演算法的實作為。

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

.NET Framework 中保存機密資料的所有密碼編譯類別都會執行 Clear 方法。 呼叫這個方法時, Clear 方法會以零覆寫物件內的所有機密資料,然後釋放物件以安全地進行垃圾收集。 當物件已清空並釋出時,您應該接著呼叫方法,並將 Dispose disposing 參數設為, True 以處置與物件相關聯的所有 managed 和非受控資源。

給實施者的注意事項

當您從類別繼承時 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 資源。

適用於

另請參閱