Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell base64 vs regular base64

I use powershell to convert string

$Text = 'ouser:v3$34@#85b&g%fD79a3nf' $Bytes = [System.Text.Encoding]::Unicode.GetBytes($Text) $EncodedText =[Convert]::ToBase64String($Bytes) $EncodedText  

However when using https://www.base64decode.org/, or some java libraries for base64 encoding I get a different, shorter version.

Sample string:

This is a secret and should be hiden

powershell result:

VABoAGkAcwAgAGkAcwAgAGEAIABzAGUAYwByAGUAdAAgAGEAbgBkACAAcwBoAG8AdQBsAGQAIABiAGUAIABoAGkAZABlAG4A

normal base64 result:

VGhpcyBpcyBhIHNlY3JldCBhbmQgc2hvdWxkIGJlIGhpZGVu

While using the website I am able to decode both versions, however using my java code I am only able to decode the latter. Why is that? Is there more than one version of base64? Where those differences come from?

like image 466
Zerg Avatar asked Aug 11 '17 07:08

Zerg


People also ask

What is Base64 PowerShell?

PowerShell Base64 is a technique or mechanism that is used to encode and decode data. The encoding and decoding are important in order to prevent the data from malware attacks. Base64 encoding and decoding is a popular method to encrypt and decrypt the data.

Is Base64 encoding always the same?

Artjom B. Base64 is not encryption. But yes, different input strings will always encode to different Base64-encoded strings, and the same input string will always encode to the same Base64-encoded string. It's not a hash though, so small changes in the input will only result in small changes in the output.

Why does Base64 strings end with ==?

From Wikipedia: The final '==' sequence indicates that the last group contained only one byte, and '=' indicates that it contained two bytes.


1 Answers

Adding the comment from @Raziel as an answer for better discoverability of this question.

[System.Text.Encoding]::Unicode] is UTF-16, the later is UTF-8. There's [System.Text.Encoding]::UTF8 that you can use.

like image 103
Andy Lamb Avatar answered Sep 24 '22 01:09

Andy Lamb