gpt4 book ai didi

C# 密码派生字节 : seems that Salt does'nt matter

转载 作者:行者123 更新时间:2023-11-30 22:58:05 25 4
gpt4 key购买 nike

我可能误会了什么。以下代码通过 CryptDeriveKey 使用两种不同的盐生成两个相等的 key 。

这是控制台结果:

盐1: 21 3e 18 a3 9a 8b 5f

--> 键 da 89 ea 3d 91 08 20 98 20 e9 dc 45 d5 97 10 7f 8f 4a 52 15 26 68 ef 83

盐2:9e db 4c 2b 49 b4 24

--> 键 da 89 ea 3d 91 08 20 98 20 e9 dc 45 d5 97 10 7f 8f 4a 52 15 26 68 ef 83

我的错误是什么?

using System;
using System.Security.Cryptography;

namespace PasswordDeriveBytes_SaltDoesntMatter
{
class Program
{
// for usage in CreateAndPrintKeyAndSalt
private static readonly string password = "secret123";
private static readonly TripleDESCryptoServiceProvider cryptoServiceProvider = new TripleDESCryptoServiceProvider();

static void Main(string[] args)
{
byte[] salt1 = new byte[] { 33, 62, 24, 163, 154, 139, 95 };
byte[] salt2 = new byte[] { 158, 219, 76, 43, 73, 180, 36 };

// a TripleDESCryptoServiceProvider-instance for getting an IV

CreateAndPrintKeyAndSalt("salt1", salt1);
CreateAndPrintKeyAndSalt("salt2", salt2);
Console.ReadKey();

}

/// <summary>
/// print the salt and the CryptDeriveKey based on this salt
/// !! uses the const password and cryptoServiceProvider
/// </summary>
/// <param name="saltName">name of the used salt</param>
/// <param name="salt">the used salt</param>
/// <param name="cryptoServiceProvider"></param>
private static void CreateAndPrintKeyAndSalt(string saltName, byte[] salt)
{
PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt);
byte[] aKey = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, cryptoServiceProvider.IV);
Console.WriteLine($"{saltName}: {ByteArrayInHexText(salt)} --> Key {ByteArrayInHexText(aKey)}");
}

/// <summary>
/// returns a Textstring of each byte in arr in hex-formatting separated by space
/// </summary>
/// <param name="arr">the array</param>
/// <returns>the formatted string</returns>
public static string ByteArrayInHexText(byte[] arr)
{
string s = "";
foreach (var item in arr)
{
s += $" {item:x2}";
}
return s.Substring(1);
}

}
}

最佳答案

根据 this MSDN 博客:

When calling CryptDeriveKey, the salt and iteration count that are set on the PasswordDeriveBytes object are not used, so even having different salts and iteration counts will produce the same key given that the rest of the inputs are also the same.

关于C# 密码派生字节 : seems that Salt does'nt matter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53216247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com