• Stars
    star
    394
  • Rank 108,841 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 10 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CryptoJS AES encryption/decryption on client side with Javascript and on server side with PHP

CryptoJS 3.x AES encryption/decryption on client side with Javascript and on server side with PHP

A tool to AES encrypt/decrypt data in javascript and/or PHP. You can use it for PHP only, for Javascript only or mix it together.

It uses default aes-256-cbc implementation with random salts and random initialization vector. This library does not support other ciphers or modes.

Features

  • Encrypt any value in Javascript (objects/array/etc...) - Everything that can be passed to JSON.stringify
  • Encrypt any value in PHP (object/array/etc...) - Everything that can be passed to json_encode
  • Decrypt in PHP/Javascript, doesn't matter where you have encrypted the values

How to use

PHP | See dist/example-php.php

You need the file src/CryptoJsAes.php

<?php
use Nullix\CryptoJsAes\CryptoJsAes;
require "CryptoJsAes.php";

// encrypt
$originalValue = ["We do encrypt an array", "123", ['nested']]; // this could be any value
$password = "123456";
$encrypted = CryptoJsAes::encrypt($originalValue, $password);
// something like: {"ct":"g9uYq0DJypTfiyQAspfUCkf+\/tpoW4DrZrpw0Tngrv10r+\/yeJMeseBwDtJ5gTnx","iv":"c8fdc314b9d9acad7bea9a865671ea51","s":"7e61a4cd341279af"}

// decrypt
$encrypted = '{"ct":"g9uYq0DJypTfiyQAspfUCkf+\/tpoW4DrZrpw0Tngrv10r+\/yeJMeseBwDtJ5gTnx","iv":"c8fdc314b9d9acad7bea9a865671ea51","s":"7e61a4cd341279af"}';
$password = "123456";
$decrypted = CryptoJsAes::decrypt($encrypted, $password);

echo "Encrypted: " . $encrypted . "\n";
echo "Decrypted: " . print_r($decrypted, true) . "\n";
Javascript | See dist/example-js.html

You need the file dist/cryptojs-aes.min.js and dist/cryptojs-aes-format.js

<script src="dist/cryptojs-aes.min.js"></script>
<script src="dist/cryptojs-aes-format.js"></script>
<script>
      (function () {
        // encrypt value
        let valueToEncrypt = 'foobar' // this could also be object/array/whatever
        let password = '123456'
        let encrypted = CryptoJSAesJson.encrypt(valueToEncrypt, password)
        console.log('Encrypted:', encrypted)
        // something like: {"ct":"10MOxNzbZ7vqR3YEoOhKMg==","iv":"9700d78e12910b5cccd07304333102b7","s":"c6b0b7a3dc072248"}
      })()
    </script>
    <script>
      (function () {
         // decrypt value
        let encrypted = '{"ct":"hQDvpbAKTGp1mXgzSShR9g==","iv":"57fd85773d898d1f9f868c53b436e28f","s":"a2dac436512077c5"}'
        let password = '123456'
        let decrypted = CryptoJSAesJson.decrypt(encrypted, password)
        console.log('Decrypted:', decrypted)
      })()
    </script>

Composer Install

composer require brainfoolong/cryptojs-aes-php

Supported PHP versions

Security Notes

Since the time that this library has been created, encryption technologies has been evolved. This library and using AES-256-CBC encryption is still good and safe but there are (maybe) already better alternatives than this library or CryptoJS itself. If you require really high security, you should invest more time for what is suitable for you.

Also, there's a good article about PHP issues/info related to this library: https://stackoverflow.com/questions/16600708/how-do-you-encrypt-and-decrypt-a-php-string/30159120#30159120

Requirements

Changelog

  • 2.1.1 - 15. January 2021
    • just a few documentation and composer fixes
  • 2.1.0 - 30. December 2020
    • added quick decrypt and encrypt functions: CryptoJSAesJson.encrypt() and CryptoJSAesJson.decrypt() - See examples for more information
  • 7. April 2020
    • Upgraded project to namespaces

More Repositories

1

web-ftp-client

A web based, always online, FTP/SFTP client. Like filezilla but for your browser.
JavaScript
95
star
2

unity-procedural-mesh-bezier-curve

Unity Procedural Mesh Bezier Curve Tutorial
C#
84
star
3

rcon-web-admin

Self hosted, online RCON administration tool for your game server.
JavaScript
77
star
4

gpio-webinterface

Control GPIOs with a webinterface. Simple, powerful.
PHP
66
star
5

form-data-json

A zero dependency, cross browser library to easily get or set/manipulate form input values as/from a json object.
JavaScript
56
star
6

omxwebgui-v2

Web GUI for OMX Player on a Raspberry Pi
PHP
53
star
7

unity-music-visualizer

Basic music visualization project for Unity.
C#
43
star
8

better-battlelog

Better Battlelog legacy code and legacy website. It was fun :)
CSS
42
star
9

omxwebgui

Simple Web GUI for Omxplayer on a Raspberry Pi made in PHP
PHP
23
star
10

nreeda

Web-based Open Source RSS/XML/Atom Feed Reader - It's built to be an open source alternative to Feedly or Google Reader.
PHP
14
star
11

bomnibox

Optimized fuzzy omnibox history search for chrome - Like firefox quick history search
JavaScript
13
star
12

gdscript-midi-parser

Godot GDScript 4+ Midi File Parser and Player
GDScript
9
star
13

greaterfield

Greaterfield - Make your Web Battlefield great
JavaScript
6
star
14

IntoTheBreachTranslations

Inofficial Translations for PC Game Into the Breach.
JavaScript
6
star
15

php-ascon

PHP 8+ Implementation of Ascon, a family of authenticated encryption (AEAD) and hashing algorithms designed to be lightweight
PHP
5
star
16

rwa-rustboard

Rustboard - A widget for RCON Web Admin.
JavaScript
2
star
17

js-ascon

JavaScript / TypeScript Implementation of Ascon, a family of authenticated encryption (AEAD) and hashing algorithms designed to be lightweight.
TypeScript
2
star
18

rwa-timedcommands

TimedCommands - A widget for RCON Web Admin.
JavaScript
1
star
19

rwa-autobot

Autobot - A widget for RCON Web Admin. Automate everything.
JavaScript
1
star
20

typeof

Random Stuff
JavaScript
1
star