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 –
- 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.
Liked page still no content ???
i sent you link into email id
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
Hi Anes,
We do not have expertise in ionic, sorry we could not help on this.
i downloaded the zip file, but doesnt have the full example, is there any way to have it?
it was for php 5, you can get latest one from https://phpflow.com/php/how-to-encrypt-and-decrypt-string-in-php7/