Encrypt in Javascript and Decrypt in PHP

Encrypt in JavaScript and Decrypt in PHP

Now a days while passing the data from client side to serverside it’s necessary to encrypt it before sending it to the server. There are different ways for it. Here we are using normal encrpt and decrypt algoritham for data transmission on server.

Encrypt in JavaScript and Decrypt in PHP

Please find JavaScript code below.

function encode(str) {
var encoded = "";
str = btoa(str);
str = btoa(str);
for (i=0; i<str.length;i++) {
var a = str.charCodeAt(i);
var b = a ^ 10; // bitwise XOR with any number, e.g. 123
encoded = encoded+String.fromCharCode(b);
encoded = btoa(encoded);
return encoded;
var t = encode("112233445566");
See F12 console for output values.

Please find PHP code below.

function decode($encoded) {
$encoded = base64_decode($encoded);
$decoded = "";
for( $i = 0; $i < strlen($encoded); $i++ ) {
$b = ord($encoded[$i]);
$a = $b ^ 10; // $decoded .= chr($a);
return base64_decode(base64_decode($decoded));
$t = decode("XlxYTG9fO3teUnpFWExPcl5mWFBHbTc3");
echo $t;

Note : You can also use AES encryption or jcryption for the same.

Leave a Reply