source: admin-center/trunk/fuentes/admin-center.install/usr/share/admin-center/libphp/cryptojs-aes.php @ 2185

Last change on this file since 2185 was 2185, checked in by joamuran, 3 years ago

Added aes encryption for n4d answers

File size: 1.7 KB
Line 
1<?php
2/**
3* Helper library for CryptoJS AES encryption/decryption
4* Allow you to use AES encryption on client side and server side vice versa
5*
6* @author BrainFooLong (bfldev.com)
7* @link https://github.com/brainfoolong/cryptojs-aes-php
8*/
9
10/**
11* Decrypt data from a CryptoJS json encoding string
12*
13* @param mixed $passphrase
14* @param mixed $jsonString
15* @return mixed
16*/
17function cryptoJsAesDecrypt($passphrase, $jsonString){
18    $jsondata = json_decode($jsonString, true);
19    try {
20        $salt = hex2bin($jsondata["s"]);
21        $iv  = hex2bin($jsondata["iv"]);
22    } catch(Exception $e) { return null; }
23    $ct = base64_decode($jsondata["ct"]);
24    $concatedPassphrase = $passphrase.$salt;
25    $md5 = array();
26    $md5[0] = md5($concatedPassphrase, true);
27    $result = $md5[0];
28    for ($i = 1; $i < 3; $i++) {
29        $md5[$i] = md5($md5[$i - 1].$concatedPassphrase, true);
30        $result .= $md5[$i];
31    }
32    $key = substr($result, 0, 32);
33    $data = openssl_decrypt($ct, 'aes-256-cbc', $key, true, $iv);
34    return json_decode($data, true);
35}
36
37/**
38* Encrypt value to a cryptojs compatiable json encoding string
39*
40* @param mixed $passphrase
41* @param mixed $value
42* @return string
43*/
44function cryptoJsAesEncrypt($passphrase, $value){
45    $salt = openssl_random_pseudo_bytes(8);
46    $salted = '';
47    $dx = '';
48    while (strlen($salted) < 48) {
49        $dx = md5($dx.$passphrase.$salt, true);
50        $salted .= $dx;
51    }
52    $key = substr($salted, 0, 32);
53    $iv  = substr($salted, 32,16);
54    $encrypted_data = openssl_encrypt(json_encode($value), 'aes-256-cbc', $key, true, $iv);
55    $data = array("ct" => base64_encode($encrypted_data), "iv" => bin2hex($iv), "s" => bin2hex($salt));
56    return json_encode($data);
57}
Note: See TracBrowser for help on using the repository browser.