1

I have two methods, where I need to convert string to base64 at begin and reverse this operation at the end. Problem is when my input string lenght is not divisible by 4 an conversion method throws exception.

public class Hashing
{
    public string Encrypt(string encrypted)
    {
        byte[] byteData = Convert.FromBase64String(encrypted);
        byte[] byteResult = Encrypt(byteData); // pt.1
        return Convert.ToBase64String(byteResult);
    }

    public string Decrypt(string decrypted)
    {
        byte[] byteData = Convert.FromBase64String(decrypted);
        byte[] byteResult = Decrypt(byteData); //pt.2
        return Convert.ToBase64String(byteResult);
    }

    /*
    ...
    */
}

class Program
{
    static void Main(string[] args)
    {
        Hashing cryptographyContext = new Hashing();

        var cryptoTest = "123456789"; //someStringThatNotGonnaBeConverted;

        string enc = cryptographyContext.Encrypt(password);
        string dec = cryptographyContext.Decrypt(enc);

        Console.WriteLine(dec);
        Console.ReadLine();
    }
}

Problem is I need base64 format at input of Decrypt and Encrypt methods (these at pt. 1 and 2) And I need returning strings from these methods. Do someone have an idea how to workaround this behaviour?

2
  • 1
    Minor note: your method names and parameter names are odd... one doesn't Encrypt an encrypted string - nor does one Decrypt a decrypted string; you might want to normalize those parameter names Commented Jun 12, 2019 at 7:11
  • maybe this link will be usefull Commented Jun 12, 2019 at 7:18

1 Answer 1

3

You are using base-64 incorrectly; base-64 translates:

  • forwards, arbitrary byte[] to structured string
  • backwards, structured string to the original byte[]

Conversely, a regular text encoding works the other way:

  • forwards, arbitrary string to structured byte[]
  • backwards, structured byte[] to the original string

You are trying to use base-64 to get a byte[] from an arbitrary string, which isn't a thing that it does. For that, you want a regular text encoding, such as UTF-8. Try using Encoding.UTF8.GetBytes() etc instead for one half, and base-64 for the other:

public string Encrypt(string plainText)
{
    byte[] byteData = Encoding.UTF8.GetBytes(plainText);
    byte[] byteResult = Encrypt(byteData);
    return Convert.ToBase64String(byteResult);
}

public string Decrypt(string cipherText)
{
    byte[] byteData = Convert.FromBase64String(cipherText);
    byte[] byteResult = Decrypt(byteData);
    return Encoding.UTF8.GetString(byteResult);
}
Sign up to request clarification or add additional context in comments.

1 Comment

this works very well, thank you ! Also thanks for your suggestion about code.

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.