/**
 *
 * Fonction "formatée" de cryptage AES utilisant la fonction cryptoJsAesEncrypt()
 *
 * @param $data - Contenu à crypter/décrypter
 * @param $key - Phrase secrète
 * @param $action - Action (1:crypter/2:décrypter)
 *
 * @return array - array(data:json, string:contenu crypté/décrypter)
 */
function AesCryptoJsPhp($data, $key, $action)
{
    $obj = array('data' => NULL, 'string' => NULL);
    if ($action == 1) {
        $obj['data'] = cryptoJsAesEncrypt($key, $data);
        $obj['string'] = json_decode($obj['data'], true);
        $obj['string'] = $obj['string']['ct'];
    } elseif ($action == 2) {
        $obj['data'] = cryptoJsAesDecrypt($key, $data);
        $obj['string'] = $obj['data'];
    }
    return $obj;
}
<h1>CryptoJS AES and PHP</h1>

<h2>Example to encrypt with PHP on serverside side and decrypt on client side</h2>
<form name="e" method="post" action="">
    Value to encrypt: <input type="text" name="val" value="<?php 
echo isset($_POST["val"]) ? $_POST["val"] : "My string - Could also be an JS array/object";
?>
" class="val" size="45"/><br/>
    Passphrase: <input type="text" name="pass" class="pass" value="<?php 
echo isset($_POST["pass"]) ? $_POST["pass"] : "******";
?>
" size="45"/><br/>
    <input type="submit" name="encrypt" value="Send to server and encrypt, than decrypt with cryptoJS"/>
    <?php 
if (isset($_POST["encrypt"])) {
    include "../cryptojs-aes.php";
    ?>
        <hr/>
        <br/><br/>
        Encrypted value generated by PHP: <input type="text" value="<?php 
    echo htmlentities(cryptoJsAesEncrypt($_POST["pass"], $_POST["val"]));
    ?>
" size="90" disabled="disabled" class="encrypted"/><br/>
        Decrypted value: <input type="text" value="" size="90" disabled="disabled"/> <input class="decrypt" type="button" value="Decrypt now with cryptoJS"/>
        <?php 
}
?>
</form>

</body>
</html>
Example #3
0
function getProductLists()
{
    $sql = "SELECT i.item_no, i.item1_desc, i.item2_desc, i.unit_retail_amt,i.max_retail_amt,i.tax_pct,i.tax_calculation,i.price_with_tax, inv.on_hand_qty, DATE_FORMAT(FROM_UNIXTIME(i.updated), '%m/%d/%y %H:%i:%s') last_updated_on FROM item as i join inventory as inv on i.item_no = inv.item_no join item_category ic on ic.item_no = inv.item_no join category c on c.cid = ic.category_no where ic.status = 'A' and c.cid in (8,9,45,379) group by i.item_no order by i.updated asc";
    try {
        $db = getDB();
        $stmt = $db->query($sql);
        $products = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        $json = '{"products": ' . json_encode($products) . '}';
        echo cryptoJsAesEncrypt('pass', $json);
    } catch (PDOException $e) {
        //error_log($e->getMessage(), 3, '/var/tmp/php.log');
        echo '{"error":{"text":' . $e->getMessage() . '}}';
    }
}