-1

Why when converting string to byte[] using Convert.FromBase64String(...) i can't put two same characters?

For example I have

 class User
{
    public string Login { get; set; }
    public byte[] Password { get; set; }

}
class Program
{
    static void Main(string[] args)
    {
        var login = Console.ReadLine();
        var password = Console.ReadLine();
        var bytePass = Convert.FromBase64String(password);

        var user = new User() { Login = login, Password = bytePass };

    }
}

When I put as password string with two same characters ("testt" for example) it throw exception

System.FormatException: „The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.”

What I should to do if I want convert string which multiple same characters to byte[]? Is it even possible?

8
  • 2
    You are using FromBase64String so the password is expected to be a base64strin and not testt Commented Dec 3, 2021 at 16:05
  • Convert.FromBase64String(...) converts a base64 encoded string to byte array. Your input string is not base64 encoded that's why you are getting this error. Can you explain what you are trying to do? Why are you using Convert.FromBase64String(...)? Commented Dec 3, 2021 at 16:07
  • @Alen.Toma so, is there a method that would do that? Commented Dec 3, 2021 at 16:07
  • @Chetan trying to convert string value to byte[] value Commented Dec 3, 2021 at 16:08
  • 2
    Please tell me you're not going to store passwords without hashing and salting? Commented Dec 3, 2021 at 16:09

1 Answer 1

1

The string testt is not a Base64-encoded string. The length of a Base64 string must be a multiple of four.

To convert a regular string to a sequence of bytes, use the Encoding.GetBytes method.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.