Php

How to Encrypt in JavaScript and Decrypt String in PHP using AES and Cryptojs

This tutorial help to encrypt and decrypt a string using cryptojs and PHP. Cryptojs is a very popular library that is used to convert string data into encrypted text and vice versa.

I am using Angularjs/javascript Cryptojs library for encryption data. You can encrypt and decrypt string, forms data or any header parameters.

You can create your own public salt key which will secure your encrypted data. The SALT string is a user-defined public key that will use for the encryption and decryption of data/string.

This example will work with CryptoJS 3.x and PHP5+ with openssl support.

Files structure for decrypt string in PHP

Angular application – Angularjs application converts to string and sends them to PHP application for decrypt data.
index.php – This file is responsible to decrypt string using mcrypt_decrypt and display data.

We are using cryptojs Hex method to encode key and iv in angularjs application. I am using the below key and iv –

Related Post
  • key : 0123456789abcdef0123456789abcdef
  • iv : abcdef9876543210abcdef9876543210
  • Encrypted string : MwOfGGCYPBEpQ0ImKQsgyA==

Above salt string is the public key which is available only for both party server and client front-end side.

Decrypt String in PHP using Cryptojs and AES

There are following php code help to convert encrypted data into a plain string.

<?php
$key = pack("H*", "0123456789abcdef0123456789abcdef");
$iv =  pack("H*", "abcdef9876543210abcdef9876543210");
$encrypted = base64_decode('MwOfGGCYPBEpQ0ImKQsgyA==');
$decrypt_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv);

echo $decrypt_string;
?>

I am using mcrypt_decrypt() method for decrypting data and MCRYPT_RIJNDAEL_128 cipher key.

This PHP tutorial help with basic encryption/decryption string using AES and PHP. You are free to use and customized this code.

You can download the source code and Demo from the below link.

Tags: php tutorial

View Comments

  • Dear Phpflow team,

    I also liked . But no content .
    I tried below code in ionic 3 . But no agreement

    import { Component } from '@angular/core';
    import { NavController, NavParams } from 'ionic-angular';
    import CryptoJS from 'crypto-js';
    /**
    * Generated class for the HomePage page.
    *
    * See https://ionicframework.com/docs/components/#navigation for more info on
    * Ionic pages and navigation.
    */

    @Component({
    selector: 'page-home',
    templateUrl: 'home.html',
    })
    export class HomePage {
    key: any;
    iv: any;
    plain_text: string;
    encrypted_text: string;
    payment_var: string;
    decrypted_text: string;
    constructor(public navCtrl: NavController, public navParams: NavParams) {
    this.key = "0123456789abcdef0123456789abcdef";
    this.iv = "abcdef9876543210abcdef9876543210";
    this.plain_text = '';
    this.payment_var = '';
    this.encrypted_text = '';
    this.decrypted_text = '';
    }

    encrypt() {
    var text = this.plain_text;
    // padding and truncating
    var encrypted = CryptoJS.AES.encrypt(text, this.key, {
    iv: this.iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.NoPadding
    });

    this.encrypted_text = CryptoJS.enc.Hex.stringify(encrypted.ciphertext);
    console.log("enc text:"+this.encrypted_text);
    alert(this.encrypted_text);
    }

    ionViewDidLoad() {
    console.log('ionViewDidLoad HomePage');
    this.plain_text = 'parvez.alam';
    this.encrypt();
    }

    }

    Please help

    Thanks

    Anes

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

2 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories