Rfc2898DeriveBytes.GetBytes(Int32) 方法

定义

返回此对象的伪随机密钥。

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 通过使用基于 HMACSHA1的伪随机数生成器来实现 PBKDF2 功能。 类 Rfc2898DeriveBytes 采用密码、salt 和迭代计数,然后通过调用 GetBytes 方法生成密钥。 对此方法的重复调用不会生成相同的密钥;相反,将方法的GetBytes两次cb调用追加为 参数值20相当于使用 参数值 40调用GetBytes方法一次cb

适用于

另请参阅