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
。 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 使用名為 cipher 區塊連結 (CBC) 的連結模式,這需要索引鍵 (Key) 和初始化向量 (IV) 來執行資料的密碼編譯轉換。 若要使用其中一個類別來解密已加密的資料 SymmetricAlgorithm ,您必須將 Key 屬性和 IV 屬性設定為加密所用的相同值。 若要讓對稱演算法很有用,秘密金鑰必須只有傳送者和接收者才知道。
Aes、 DES 、 RC2 和 TripleDES 都是對稱演算法的實作為。
請注意,使用衍生類別時,從安全性的觀點來看,在您完成使用物件之後,就只會強制執行垃圾收集。 您必須明確地將 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 |
取得或設定對稱演算法中使用的填補模式。 |
方法
明確介面實作
IDisposable.Dispose() |
此 API 支援此產品基礎結構,但無法直接用於程式碼之中。 釋放 SymmetricAlgorithm 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。 |