0

I am opening a file encoded in base64 and I need to decode it

string[] lines = File.ReadAllLines(open1.FileName);

is there anyway to do this? I have tried doing this.

string[] lines = File.ReadAllLines(Convert.ToBase64String(open1.FileName));
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    `Convert.ToBase64String()` will take collection of bytes and will convert it to base64 string. – Guru Stron Jul 20 '22 at 23:37
  • oh how do i turn the lines variable into base 64 instead – Eleventeen Jul 20 '22 at 23:38
  • `Convert.FromBase64String` takes a base64 string and converts it to byte array. Note that it requires 1 string and your whole file should be read as one (`File.ReadAllText`) – Guru Stron Jul 20 '22 at 23:41

1 Answers1

0

you question is not clear

if you want to convert string[] to array of base64 you can do this :

public static string[] ToBase64(string[] text)
{
    var encodedArray = new string[text.Length];

    for (var index = 0; index < text.Length; index++)
    {
        var line = text[index];
        var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(line));
        encodedArray[index] = encoded;
    }
    return encodedArray;
}

this returns your array with each line encoded in base64. and to reverse it :

public static string[] ToString(string[] encodedString)
{
    var decodedArray = new string[encodedString.Length];
    for (var i = 0; i < encodedString.Length; i++)
    {
        var line = encodedString[i];
        var decoded = Convert.FromBase64String(line);
        decodedArray[i] = Encoding.UTF8.GetString(decoded);
    }

    return decodedArray;
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
AliSalehi
  • 159
  • 1
  • 1
  • 13