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.
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 –
Above salt string is the public key which is available only for both party server and client front-end side.
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.
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
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
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/