0

I am new in C# wpf programming but I am trying to connect to MySQL database and to hash my password. 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?

1
  • What is the type of plainText? I suspect it's a String. You can't copy a string to an array of bytes. Commented May 16, 2012 at 23:04

2 Answers 2

2

You need to copy plainTextBytes, not plainText:

   byte[] plainTextWithSaltBytes = new byte[plainTextBytes.Length + saltBytes.Length];
   salt.CopyTo(plainTextWithSaltBytes, 0);
   plainTextBytes.CopyTo(plainTextWithSaltBytes, salt.Length); 
Sign up to request clarification or add additional context in comments.

3 Comments

... make that saltBytes, too.
@Thomas Levesque you really solve my problem but now when I run my WPF and try to insert data I am getting error on this line: ' using (var cmd = new MySqlCommand("Select salt From niki where user_name = @username")) { cmd.Parameters.AddWithValue("@username", username); salt = cmd.ExecuteScalar() as string; }' The error is connection must be valid and open. Do you have any clue where can be the problem?
@NikolayDyankov, this is a completely unrelated question, and we can't easily discuss it in the comments. You should open a new question.
1

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. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.