我正在使用以下内容.NET代码从密码和salt生成密钥:
static byte[] GenerateKey(string password, string salt, int size)
{
var saltBytes = Encoding.Unicode.GetBytes(salt);
var derivedBytes = new Rfc2898DeriveBytes(password, saltBytes, iterations);
var key = derivedBytes.GetBytes(size);
Console.WriteLine(string.Format("Key: {0}", Convert.ToBase64String(key)));
return key;
}
// Console.Writeline() shows
// Key: tb6yBBYGdZhyFWrpWQ5cm5A1bAI5UF0KnDdom7BhVz0=
// for password="password" and salt="salt"
我需要使用python解码编码的消息,python是一种我只稍微熟悉的语言,使用相同的密码和salt.多亏了@Topaco,我现在知道有PBKDF2 equivalent个:
def decrypt_file(filename, password, salt):
key = PBKDF2(password, salt, 32, count=12345, hmac_hash_module=SHA1)
print(f"Key: {base64.b64encode(key).decode('utf-8')}");
# more lines redacted
# print() shows
# Key: 3ohW9ctQIXoNvGnvLaKmoQTG8/jJzoFThviHXqgM9Co=
# for password="password" and salt="salt"
我在从两个实现中获取相同的密钥时遇到了一些问题.我not岁,精通python的编码和解码;很可能是am生成了相同的键,但base64.b64encode(key).decode('utf-8')
行显示了不同的翻译.
我做错了什么?