By stefam mierz

2019-06-12 07:07:52 8 Comments

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


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?


@Marc Gravell 2019-06-12 07:10:58

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

@stefam mierz 2019-06-12 12:22:51

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

Related Questions

Sponsored Content

62 Answered Questions

[SOLVED] What is the difference between String and string in C#?

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

6 Answered Questions

[SOLVED] How do I encode and decode a base64 string?

  • 2012-07-31 15:06:24
  • Kevin Driedger
  • 642656 View
  • 740 Score
  • 6 Answer
  • Tags:   c# base64

38 Answered Questions

40 Answered Questions

[SOLVED] How do you convert a byte array to a hexadecimal string, and vice versa?

  • 2008-11-22 10:03:13
  • alextansc
  • 763632 View
  • 1267 Score
  • 40 Answer
  • Tags:   c# arrays hex

28 Answered Questions

[SOLVED] Catch multiple exceptions at once?

13 Answered Questions

[SOLVED] Converting string to byte array in C#

13 Answered Questions

[SOLVED] How to convert UTF-8 byte[] to string?

14 Answered Questions

3 Answered Questions

[SOLVED] How to do Base64 encoding in node.js?

Sponsored Content