Learn html, css, javascript online and how to make a webpage design

How to Base64 encode from javascript and decode on PHP

Friday 5th of February 2021 02:07:43 PMBase64JavascriptPHP

Base64 encryption technology appears a lot in online applications, sometimes we need to encrypt data on the client side and perform decompilation from the server side. Very simple in the example below:

The client side uses javascript

The javascript language provides us with two functions btoa () and function atob ().

 

Function btoa () - encrypts the data into a base64 string

This function is usually used to encrypt data before sending it to the server

let base64_string = btoa("Welcome to htmlcssdownload.com");
console.log(base64_string);
// output: "V2VsY29tZSB0byBodG1sY3NzZG93bmxvYWQuY29t"

 

Function atob () - Decompile base64 string into original data

This function is often used to decompile data from the base64 string

let base64_string = "V2VsY29tZSB0byBodG1sY3NzZG93bmxvYWQuY29t";
let origin_data = atob(base64_string);
console.log(origin_data);
// output : "Welcome to htmlcssdownload.com"

 

The server side uses PHP language

The PHP language provides us with two functions base64_decode () and function base64_encode ().

Base64_encode () function - Encodes data into a string base64

This function is usually used to encrypt data before sending it to the client

$origin_text = "Welcome to htmlcssdownload.com";
echo base64_encode($origin_text);
// output "V2VsY29tZSB0byBodG1sY3NzZG93bmxvYWQuY29t"

 

Base64_decode () function - Decompile base64 string into original data

This function is often used to decompile the base64 string to the original data

$base64 = "V2VsY29tZSB0byBodG1sY3NzZG93bmxvYWQuY29t";
echo base64_decode($base64);
// output "Welcome to htmlcssdownload.com"

 

 

We are Recommending you: