Some of the links are affiliate links and I earn commission from them. Moreover, as an Amazon Associate, I earn from qualifying purchases.
Do you want to store any string or text secretly, so that no one can read the actual text? Well, an easy way to do that is to use base64 to encode and decode the encoded string with base64 whenever you want to read it.
In this post, we have an online convertor using which you can encode a string with Base64. Additionally, you can use another convertor tool to decode the same string.
What Is Base64 encode and decode?
Base 64 is an encoding scheme that takes binary information and changes it into complex text by translating the data to a radix-64 representation. The reversal process is called decode.
The below table shows the binary data and its respective base64 character.
Binary | Char | Binary | Char | Binary | Char | Binary | Char |
---|---|---|---|---|---|---|---|
000000 | A | 010000 | Q | 100000 | g | 110000 | w |
000001 | B | 010001 | R | 100001 | h | 110001 | x |
000010 | C | 010010 | S | 100010 | i | 110010 | y |
000011 | D | 010011 | T | 100011 | j | 110011 | z |
000100 | E | 010100 | U | 100100 | k | 110100 | 0 |
000101 | F | 010101 | V | 100101 | l | 110101 | 1 |
000110 | G | 010110 | W | 100110 | m | 110110 | 2 |
000111 | H | 010111 | X | 100111 | n | 110111 | 3 |
001000 | I | 011000 | Y | 101000 | o | 111000 | 4 |
001001 | J | 011001 | Z | 101001 | p | 111001 | 5 |
001010 | K | 011010 | a | 101010 | q | 111010 | 6 |
001011 | L | 011011 | b | 101011 | r | 111011 | 7 |
001100 | M | 011100 | c | 101100 | s | 111100 | 8 |
001101 | N | 011101 | d | 101101 | t | 111101 | 9 |
001110 | O | 011110 | e | 101110 | u | 111110 | + |
001111 | P | 011111 | f | 101111 | v | 111111 | / |
Padding = |
How To Use Base64 encode decode in Programming Language
You can use the above tool base64 encoding and decoding without having knowledge of programming language. However, the below section describes the code you can use to encode and decode base64.
Base64 Encode In JavaScript
Javascript is a very popular language. In fact, the above Base64 encoder and decoder are built with Javascript only. In javascript, you can use window.btoa(“string”) to encode a string in Base64 whereas to decode you can use window.atob(“string”).
<input type="textarea" name="inputtext" id="encode">
<button onclick="myFunction1()">Encode Base64</button>
<br>
<input type="textarea" name="inputtext" id="decode">
<button onclick="myFunction2()">Decode Base64</button>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
function myFunction1() {
var str = document.getElementById("encode").value;
var enc = window.btoa(str);
var res = "Encoded String: " + enc;
document.getElementById("demo1").innerHTML = "The original string: " + str + "<br>" + res;
}
function myFunction2() {
var str = document.getElementById("decode").value;
var enc = window.atob(str);
var res = "Encoded String: " + enc;
document.getElementById("demo2").innerHTML = "The original string: " + str + "<br>" + res;
}
</script>
Encode and Decode String With Base64 using Python
Python has an inbuilt library called Base64 for encoding and decoding strings. First, you have to import the library, convert the strings to byte-like objects, and then encode it using the base64 library. Follow the exact reverse process to decode the strings.
>>> import base64
>>> string = "techieblogging"
>>> string_bytes = string.encode("ascii")
>>> base64_bytes = base64.b64encode(string_bytes)
>>>
>>> base64_string = base64_bytes.decode("ascii")
>>> print(f"Encoded string: {base64_string}")
Output: Encoded string: dGVjaGllYmxvZ2dpbmc=
>>> #decoding string
>>> base64_string ="dGVjaGllYmxvZ2dpbmc="
>>> base64_bytes = base64_string.encode("ascii")
>>> string_bytes = base64.b64decode(base64_bytes)
>>> string = string_bytes.decode("ascii")
>>> print(f"Decoded string: {string}")
Output: Decoded string: techieblogging
>>>
Base64 encoding and Decoding using PHP
In this post, we will see the PHP code using which you can encode and decode a string using base64. Like many programming languages, PHP has inbuilt libraries to perform encoding and decoding. In PHP language you perform base64 encode using base64_encode(“string”). Similarly, you can use base64_decode(“string”) to decode an encoded string.
<!DOCTYPE html>
<html>
<body>
<?php
// PHP code to encode strings
$str = 'techieblogging';
echo base64_encode($str);
?>
<?php
// PHP code to decode strings
$str = 'dGVjaGllYmxvZ2dpbmc=';
echo base64_decode($str);
?>
</body>
</html>
What is the use of base64 encoding?
There are various ways you can use base64 encoding. One of the widely used scenarios is sending email attachments over email. The email software usually converts the attachment into a base64 encoded string and sends it over communication channels.
You can also use this approach to store confidential data. Though it is not powerful as encryption, still a better approach to store confidential data such as user name and password.
Conclusion
I Hope, the online base64 encoder and decoder will be useful to you in your day-to-day activities. Though it is not a replacement for encryption still a good approach for many cases.
If you feel any improvement is necessary, feel free to tell us in the comment section.