Problems with coding in Base64 C # [closed]

0

When trying to perform these functions (with salt being a string of 32 characters and passing a string of a variable number of characters):

public static string encodePassword(string salt, string pass)
{
    pass = mod4(pass);  /// Necesitamos que la cadena sea multiplo de 4
    pass = Convert.ToBase64String(Encoding.UTF8.GetBytes(pass));
    pass += salt;
    pass = mod4(pass);
    pass = Convert.ToBase64String(Encoding.UTF8.GetBytes(pass));
    return pass;
}

private static string mod4(string a)
{
    int mod4 = a.Length % 4;
    if (mod4 > 0)
    {
        a += new string('=', 4 - mod4);
    }
    return a;
}

Always gives the error: System.FormatException: Invalid length for a Base-64 char array or string.

What could be causing the problem?

The mod4 function has been extracted from this question.

    
asked by Slifer Dragon 15.12.2017 в 11:53
source

1 answer

2

Your code is working but I do not recommend it for passwords, look here:

link

To encode passwords use Hash, it's safer

Example:

public static string GetSha256FromString(string strData)
{
    var message = Encoding.ASCII.GetBytes(strData);
    var hashString = new SHA256Managed();

    var hashValue = hashString.ComputeHash(message);
    return hashValue.Aggregate("", (current, x) => current + $"{x:x2}");
}

My example on GitHub link

    
answered by 15.12.2017 / 12:08
source