How to encode Base64 in most popular programming languages

Base64 is a group of similar binary-to-text encoding schemes that represents binary data in an ASCII string format by translating it into radix-64 representation.

The example below uses ASCII text for simplicity, but this is not a typical use case, as it can already be safely transferred across all systems that can handle Base64.

A quote from Albert Einstein –

Two things are infinite – the universe and human stupidity and I am not sure about the universe.

Encoded Format –

VHdvIHRoaW5ncyBhcmUgaW5maW5pdGUgLSB0aGUgdW5pdmVyc2UgYW5kIGh1bWFuIHN0dX
BpZGl0eSBhbmQgSSBhbSBub3Qgc3VyZSBhYm91dCB0aGUgdW5pdmVyc2Uu

In above example, the encoded value of “Two” is “VHdv

Using the below base64 examples which actually turns your binary data into text (encode) because to allow binary data to be transferred with textual data, it must be encoded.

In PHP Language –

<?php
$str = ‘base64 encoded string’;
echo base64_encode($str);
?>

In Python Language –

import base64
encoded = base64.b64encode(‘base64 encoded string’)
print encoded

In Perl Language –

use MIME::Base64;
$string = ‘base64 encoded string’;
$encoded = encode_base64($string);
print $encoded

in C Sharp Language –

string base64Decoded = “base64 encoded string”;
string base64Encoded;
byte[] data = System.Text.ASCIIEncoding.ASCII.GetBytes(base64Decoded);
base64Encoded = System.Convert.ToBase64String(data);
Console.WriteLine(base64Encoded)

In GO Language –

package main
import b64 “encoding/base64”

func main() {
data := “base64 encoded string”
sEnc := b64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(sEnc)
}

In PGSQL Language –

select encode(‘base64 encoded string’, ‘base64’);

In Ruby Language –

require “base64”

string = ‘base64 string’
encoded_string = Base64.encode64(string)
puts encoded_string

In Shell Language –

echo linux base64 encode | base64

In SQL Language –

SELECT TO_BASE64(‘base64 encoded string’);

In VB Language –

Dim base64Decoded as String = “base64 encoded string”
Dim base64Encoded As String
Dim data As Byte()
data = System.Text.ASCIIEncoding.ASCII.GetBytes(base64Decoded)
base64Encoded = System.Convert.ToBase64String(data)
Console.WriteLine(base64Encoded)

You may also like:

Sarcastic Writer

Step by step hacking tutorials about wireless cracking, kali linux, metasploit, ethical hacking, seo tips and tricks, malware analysis and scanning.

Related Posts