9

我正在尝试在 C#中实现warpwallet代码。我将 cryptsharp 用于其 scrypt 和 PBKDF2 实现。但是,我似乎得到了与真实网站不同的结果。

这是我的代码:

static void Main(string[] args)
{
    string randomString = "mypassword";
    byte[] passwordBytes = Encoding.UTF8.GetBytes(randomString);
    byte[] passwordBytesScrypt = new byte[passwordBytes.Length + 1];
    Array.Copy(passwordBytes, 0, passwordBytesScrypt, 0, passwordBytes.Length);
    passwordBytesScrypt[passwordBytes.Length] = 0x1;

    string salt = "salt@gmail.com";
    byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
    byte[] saltBytesScrypt = new byte[saltBytes.Length + 1];
    Array.Copy(saltBytes, 0, saltBytesScrypt, 0, saltBytes.Length);
    saltBytesScrypt[saltBytes.Length] = 0x1;

    byte[] scryptBytes = CryptSharp.Utility.SCrypt.ComputeDerivedKey(passwordBytesScrypt, saltBytes, 524288, 8, 1, null, 32);

    byte[] passwordBytesPBKDF2 = passwordBytesScrypt;
    passwordBytesPBKDF2[passwordBytes.Length] = 0x2;

    byte[] saltBytesPBKDF2 = saltBytesScrypt;
    saltBytesScrypt[saltBytes.Length] = 0x2;

    byte[] pbkdf2Bytes = CryptSharp.Utility.Pbkdf2.ComputeDerivedKey(new HMACSHA256(passwordBytesPBKDF2), saltBytes, 65536, 32);
}

根据网站(我修改了代码以登录到控制台),scrypt 哈希结果应该是

4dfe98afd8f279e856abdcccce09aa54031fbd7fa39a912bb3caf5ce28648fe6

并且 PBKDF2 哈希结果应该是

847c053e66c093927d1f1258b52455675fe6788e537c2073927fbddddfacc0d3

我对 scrypt 的结果是

790BE1F92DDDF297CF7BACAA69218BE2C67680C706B2A214081A559B8E0EF43D

和 PBKDF2

CB640D1C8C13B44712EBCF341FA68F22F90D69AA5BC8427CD5ABED37FDFFE4EF

我不明白我做错了什么。cryptsharp 的实现不正确吗?我在某个地方错过了一步吗?我不太了解javascript,所以这是完全可能的。任何帮助,将不胜感激。

4

3 回答 3

1

我发现你的代码有两个问题:

  1. 2^18 不是 524288 而是 262144。
  2. 您注入 0x1 的方式似乎不起作用,我以不同的方式完成了它并且它有效:

        string randomString = "mypassword";
    
        string salt = "salt@gmail.com";
    
        //Setup Lists to take the extra byte of the byte array to the end
        var passArrList = new List<byte>();
        var saltArrList = new List<byte>();
    
        //Get the byte array of incoming passphrase
        byte[] passArr = Encoding.UTF8.GetBytes(randomString);
        //Add the pass byte array to the list
        passArrList.AddRange(passArr);
        //Append the needed 0x1 to the end of the array
        passArrList.Add(1);
    
        //Get the bytes of the salt
        byte[] saltArr = Encoding.UTF8.GetBytes(salt);
        //Add the salt to the list
        saltArrList.AddRange(saltArr);
        //Append the needed salt to the end
        saltArrList.Add(1);
    
        byte[] scryptBytes = CryptSharp.Utility.SCrypt.ComputeDerivedKey(passArrList.ToArray(), saltArrList.ToArray(), 262144, 8, 1, null, 32);
    
        Console.WriteLine(BitConverter.ToString(scryptBytes).Replace("-", ""));
    
于 2018-06-06T17:48:02.867 回答
0

您在代码中犯了一些错字,为了更清楚,我使用 LINQ,您可以按照这些步骤操作而无需操作数组。

在第 15 行,您应该使用 1 << 18 和 passwordBytesScrypt:

byte[] scryptBytes = CryptSharp.Utility.SCrypt.ComputeDerivedKey(passwordBytesScrypt, saltBytes, 524288, 8, 1, null, 32);

第 23 行,您应该使用 saltBytesPBKDF2:

byte[] pbkdf2Bytes = CryptSharp.Utility.Pbkdf2.ComputeDerivedKey(new HMACSHA256(passwordBytesPBKDF2), saltBytes, 65536, 32);

最终代码如下所示:

static void Main(string[] args)
{
    List<byte> extend1 = new List<byte> { 0x1 }, extend2 = new List<byte> { 0x2 };

    string randomString = "mypassword";
    IEnumerable<byte> passwordBytesScrypt = Encoding.UTF8.GetBytes(randomString).Concat(extend1);

    string salt = "salt@gmail.com";
    IEnumerable<byte> saltBytesScrypt = Encoding.UTF8.GetBytes(salt).Concat(extend1);

    byte[] scryptBytes = CryptSharp.Utility.SCrypt.ComputeDerivedKey(passwordBytesScrypt.ToArray(), saltBytesScrypt.ToArray(), 1 << 18, 8, 1, null, 32);

    byte[] passwordBytesPBKDF2 = passwordBytesScrypt.Take(passwordBytesScrypt.Count() - 1).Concat(extend2).ToArray();
    byte[] saltBytesPBKDF2 = saltBytesScrypt.Take(saltBytesScrypt.Count() - 1).Concat(extend2).ToArray();

    byte[] pbkdf2Bytes = CryptSharp.Utility.Pbkdf2.ComputeDerivedKey(new HMACSHA256(passwordBytesPBKDF2), saltBytesPBKDF2, 65536, 32);

    Console.WriteLine(BitConverter.ToString(scryptBytes).Replace("-", ""));
    Console.WriteLine(BitConverter.ToString(pbkdf2Bytes).Replace("-", ""));
}
于 2018-06-08T19:22:42.560 回答
0

此行有错误:

byte[] scryptBytes = CryptSharp.Utility.SCrypt.ComputeDerivedKey(passwordBytesScrypt, 
  saltBytes, 524288, 8, 1, null, 32);
  • 正如@knocte 解释的那样 2^18 不是 524288
  • 你正在使用saltBytes而不是saltBytesScrypt你已经准备好

更正的行是:

byte[] scryptBytes = CryptSharp.Utility.SCrypt.ComputeDerivedKey(passwordBytesScrypt, 
  saltBytesScrypt, 1 << 18, 8, 1, null, 32);
于 2018-06-07T05:20:12.950 回答