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

10,216

Use System.Convert.FromBase64String:

[System.Convert]::FromBase64String("U29tZSBEYXRh")
Share:
10,216
user7340057
Author by

user7340057

Updated on June 07, 2022

Comments

  • user7340057
    user7340057 about 2 years

    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?