Rfc2898DeriveBytes
Соль - 8 байт "12345678",
пароль "abcde". В примерах
1 итерация; на практике
счётчик большой. Хеш -
HashAlgorithmName.SHA256.
Два объекта с теми же входами
дают одинаковый GetBytes(32):
SequenceEqual → True.
Сам ключ не печатают.
Синтаксис
using var derive = new Rfc2898DeriveBytes(
password, salt, iterations, HashAlgorithmName.SHA256);
byte[] key = derive.GetBytes(32);
Пример
Давайте сравнить два вывода ключа:
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main()
{
byte[] salt = Encoding.UTF8.GetBytes("12345678");
byte[] first = new Rfc2898DeriveBytes(
"abcde", salt, 1, HashAlgorithmName.SHA256)
.GetBytes(32);
byte[] second = new Rfc2898DeriveBytes(
"abcde", salt, 1, HashAlgorithmName.SHA256)
.GetBytes(32);
bool res = first.SequenceEqual(second);
Console.WriteLine(res);
}
}
Результат выполнения кода:
True
Пример
Давайте сравнить ключи для разных паролей:
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
class Program
{
static void Main()
{
byte[] salt = Encoding.UTF8.GetBytes("12345678");
byte[] first = new Rfc2898DeriveBytes(
"abcde", salt, 1, HashAlgorithmName.SHA256)
.GetBytes(32);
byte[] second = new Rfc2898DeriveBytes(
"12345", salt, 1, HashAlgorithmName.SHA256)
.GetBytes(32);
bool res = first.SequenceEqual(second);
Console.WriteLine(res);
}
}
Результат выполнения кода:
False
Пример
Давайте проверить длину соли:
using System;
using System.Text;
class Program
{
static void Main()
{
byte[] salt = Encoding.UTF8.GetBytes("12345678");
int res = salt.Length;
Console.WriteLine(res);
}
}
Результат выполнения кода:
8
Смотрите также
-
Pbkdf2,
статический вывод -
Key,
ключ AES -
Aes,
симметричный шифр -
CryptoStream,
поток шифра