0

我是 C# wpf 编程的新手,但我正在尝试连接到 MySQL 数据库并散列我的密码。不幸的是,在我实现算法时,我在这段代码中遇到了错误:

byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
salt.CopyTo(plainTextWithSaltBytes, 0);
plainText.CopyTo(plainTextWithSaltBytes, salt.Length); 

错误是:

Error: no overload for method 'Copy To' takes 2 arguments Exceptions: System.ArgumentNullException System.ArgumentOutOfRangeException
enter code here

您是否知道导致此错误的原因以及如何解决?

4

2 回答 2

2

您需要复制plainTextBytes,而不是plainText

   byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
   salt.CopyTo(plainTextWithSaltBytes, 0);
   plainTextBytes.CopyTo(plainTextWithSaltBytes, salt.Length); 
于 2012-05-16T23:06:01.447 回答
1

如果你需要做简单的哈希,这段代码可能会加密你的密码:

String GetEncryptedPassword (String prmUser, String prmPassword)
{
    // Concatenating the password and user name.
    String encryptedPassword = prmUserName + prmPassword;

    // Converting into the stream of bytes.
    Byte[] passInBytes = Encoding.UTF8.GetBytes(encryptedPassword);

    // Encrypting using SHA1 encryption algorithm.
    passInBytes = SHA1.Create().ComputeHash(passInBytes);

    // Formatting every byte into %03d to make a fixed length 60 string.
    return passInBytes.Aggregate(String.Empty, (pass, iter) => pass += String.Format("{0:000}", iter));
}

这段代码会给你一个很好的 60 个字符的加密散列。但请记住,您无法从哈希中重新生成原始用户名和密码,因为这是一种单向算法。System.Security.Cryptography您可以使用的加密算法很少。

于 2012-05-17T01:20:25.717 回答