[PHP][JS] CryptoJS encrypt decrypt

 CryptoJS encrypt decrypt support PHP 5, PHP 7.x.

See the Pen PHP CryptoJS Encrypt Decrypt by dimas lanjaka (@dimaslanjaka) on CodePen.


Code PHP and details variable

[JS]

    /**
    * @package https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.js
    */
   var salt = 'salt'; //salt
   var iv = '1111111111111111'; //pass salt minimum length 12 chars
   var iterations = '999'; //iterations

   /**
    * Get key
    * @param string passphrase
    * @param string salt
    */
   function getKey(passphrase, salt) {
     var key = CryptoJS.PBKDF2(passphrase, salt, {
       hasher: CryptoJS.algo.SHA256,
       keySize: 64 / 8,
       iterations: iterations
     });
     return key;
   }
   /**
    * Encrypt function
    * @param string passphrase
    * @param string plainText
    */
   function userJSEncrypt(passphrase, plainText) {
     var key = getKey(passphrase, salt);
     var encrypted = CryptoJS.AES.encrypt(plainText, key, {
       iv: CryptoJS.enc.Utf8.parse(iv)
     });
     return encrypted.ciphertext.toString(CryptoJS.enc.Base64);
   }
   /**
    * Decrypt function
    * @param string passphrase
    * @param string encryptedText
    */
   function userJSDecrypt(passphrase, encryptedText) {
     var key = getKey(passphrase, salt);
     var decrypted = CryptoJS.AES.decrypt(encryptedText, key, {
       iv: CryptoJS.enc.Utf8.parse(iv)
     });
     return decrypted.toString(CryptoJS.enc.Utf8);
   }
[PHP 5.6++]

<?php
const SALT = 'salt'; //salt
const IV = '1111111111111111'; //pass salt minimum length 12 chars or it'll be show warning messages
const ITERATIONS = 999; //iterations
function userPHPEncrypt($passphrase, $plainText)
{
  $key = \hash_pbkdf2("sha256", $passphrase, SALT, ITERATIONS, 64);
  $encryptedData = \openssl_encrypt($plainText, 'AES-256-CBC', \hex2bin($key), OPENSSL_RAW_DATA, IV);
  return \base64_encode($encryptedData);
}
function userPHPDecrypt($passphrase, $encryptedTextBase64)
{
  $encryptedText = \base64_decode($encryptedTextBase64);
  $key = \hash_pbkdf2("sha256", $passphrase, SALT, ITERATIONS, 64);
  $decryptedText = \openssl_decrypt($encryptedText, 'AES-256-CBC', \hex2bin($key), OPENSSL_RAW_DATA, IV);
  return $decryptedText;
}
Fix Problems
How to fix openssl_encrypt() and openssl_decrypt() errors

2 Responses to "[PHP][JS] CryptoJS encrypt decrypt "

Bila Ada posting yang kurang, atau error atau yang lainnya, silahkan tinggalkan komentar agar artikel/post di perbaiki.
Jangan Lupa Cek Box "Notify Me" agar tahu komentar kamu dibalas oleh saya.

If there are any posts that are missing, or error or anything else, please leave a comment for the article / post to be fixed.
Do not Forget Check Box "Notify Me" to know our comments replied by me.

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel