Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Base64 string to byte array in .NET (or PowerShell)

I am working with a Linux guy at work (I work on Windows). He sends me a base64 string which I need to turn into a byte array using PowerShell. This byte array needs to duplicate the bytes he originally started with (he is using Phyton).

So basically, he takes some data and converts it to base64 string, then I need to take the base64 string and convert back to bytes so we end up with the same bytes. (As latter I need to check a signed digest of these bytes, not relevant for now.)

Therefore, let us say he sends me the following base64 string:

U29tZSBEYXRh

I do the following

[byte[]][char[]]"U29tZSBEYXRh"

85
50
57
116
90
83
66
69
89
88
82
104

but he says my output is an ASCII code representation of the string and not the actual bytes (he thinks the bytes should be returned as non-sense to the screen e.g. unprintable characters as it were).

Am I getting a genuine byte array of the input data or not?

like image 242
user7340057 Avatar asked Dec 23 '22 18:12

user7340057


1 Answers

Use System.Convert.FromBase64String:

[System.Convert]::FromBase64String("U29tZSBEYXRh")
like image 162
Dmitry Egorov Avatar answered Feb 03 '23 20:02

Dmitry Egorov