Rfc2898DeriveBytes.GetBytes(Int32) 方法
本文內容
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回這個物件的似隨機金鑰。
public:
override cli::array <System::Byte> ^ GetBytes(int cb);
public override byte[] GetBytes(int cb);
override this.GetBytes : int -> byte[]
Public Overrides Function GetBytes (cb As Integer) As Byte()
- cb
- Int32
要產生的似隨機金鑰位元組數量。
Byte[]
位元組陣列,以虛擬隨機金鑰位元組填入。
cb
超出範圍。 這個參數需要非負數的數字。
下列範例示範如何使用 GetBytes 方法來取得 實例的 Rfc2898DeriveBytes索引鍵。 此程式代碼範例是提供給 類別之較大範例的 Rfc2898DeriveBytes 一部分。
try
{
Rfc2898DeriveBytes ^ k1 = gcnew Rfc2898DeriveBytes( pwd1,salt1,myIterations );
Rfc2898DeriveBytes ^ k2 = gcnew Rfc2898DeriveBytes( pwd1,salt1 );
// Encrypt the data.
Aes^ encAlg = Aes::Create();
encAlg->Key = k1->GetBytes( 16 );
MemoryStream^ encryptionStream = gcnew MemoryStream;
CryptoStream^ encrypt = gcnew CryptoStream( encryptionStream,encAlg->CreateEncryptor(),CryptoStreamMode::Write );
array<Byte>^utfD1 = (gcnew System::Text::UTF8Encoding( false ))->GetBytes( data1 );
try
{
Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(pwd1, salt1,
myIterations);
Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(pwd1, salt1);
// Encrypt the data.
Aes encAlg = Aes.Create();
encAlg.Key = k1.GetBytes(16);
MemoryStream encryptionStream = new MemoryStream();
CryptoStream encrypt = new CryptoStream(encryptionStream,
encAlg.CreateEncryptor(), CryptoStreamMode.Write);
byte[] utfD1 = new System.Text.UTF8Encoding(false).GetBytes(
data1);
Try
Dim k1 As New Rfc2898DeriveBytes(pwd1, salt1, myIterations)
Dim k2 As New Rfc2898DeriveBytes(pwd1, salt1)
' Encrypt the data.
Dim encAlg As Aes = Aes.Create()
encAlg.Key = k1.GetBytes(16)
Dim encryptionStream As New MemoryStream()
Dim encrypt As New CryptoStream(encryptionStream, encAlg.CreateEncryptor(), CryptoStreamMode.Write)
Dim utfD1 As Byte() = New System.Text.UTF8Encoding(False).GetBytes(data1)
類別 Rfc2898DeriveBytes 會實作 PBKDF2 功能,方法是使用以 為基礎的 HMACSHA1虛擬隨機數產生器。 類別 Rfc2898DeriveBytes 會採用密碼、salt 和反覆專案計數,然後透過呼叫 GetBytes 方法來產生密鑰。 對這個方法的重複呼叫不會產生相同的索引鍵;相反地,使用 參數值20
附加方法的GetBytes兩個cb
呼叫,相當於使用 cb
參數值40
呼叫 GetBytes 方法一次。
適用於
產品 | 版本 |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.3, 1.4, 1.6, 2.0, 2.1 |