简体   繁体   English

哈希密码算法问题

[英]Hashing password algorith issue

I am new in C# wpf programming but I am trying to connect to MySQL database and to hash my password. 我是C#wpf编程的新手,但是我试图连接到MySQL数据库并哈希密码。 Unfortunately while I was implementing the algorith I get error in this code: 不幸的是,当我实现算法时,此代码出现错误:

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

The error is: 错误是:

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

By any chance do you know what is causing this errors and how to fix it? 您是否有机会知道导致此错误的原因以及如何解决该错误?

You need to copy plainTextBytes , not plainText : 您需要复制plainTextBytes ,而不是plainText

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

If you need to do simple hash, this bit of code may encrypt your password: 如果您需要进行简单的哈希处理,那么这段代码可以加密您的密码:

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));
}

This code will give you a nice encrypted hash of 60 characters. 此代码将为您提供60个字符的加密哈希。 But remember that you can't regenerate your original username and password from the hash, because this is a one way algorithm. 但是请记住,您无法从哈希中重新生成原始的用户名和密码,因为这是一种单向算法。 There are few more encryption algorithms in System.Security.Cryptography that you can use. System.Security.Cryptography中还可以使用更多加密算法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM