diff --git a/README.md b/README.md index 39946f7..170cc54 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,6 @@ It uses default aes-256-cbc implementation with random salts and initialization * 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 -## Upgrade Info -Breaking changes: This library has changed to PSR-4 namespaces as of 7. April 2020. Also parameters and behaviour has changed to the previous version. For the old version of this library head to the [legacy branch](https://github.com/brainfoolong/cryptojs-aes-php/tree/legacy). - ## How to use ###### PHP | See [dist/example-php.php](https://github.com/brainfoolong/cryptojs-aes-php/blob/master/dist/example-php.php) You need the file `src/CryptoJsAes.php` @@ -44,7 +41,7 @@ You need the file `dist/cryptojs-aes.min.js` and `dist/cryptojs-aes-format.js` // encrypt value let valueToEncrypt = 'foobar' // this could also be object/array/whatever let password = '123456' - let encrypted = CryptoJS.AES.encrypt(JSON.stringify(valueToEncrypt), password, { format: CryptoJSAesJson }).toString() + let encrypted = CryptoJSAesJson.encrypt(valueToEncrypt, password) console.log('Encrypted:', encrypted) // something like: {"ct":"10MOxNzbZ7vqR3YEoOhKMg==","iv":"9700d78e12910b5cccd07304333102b7","s":"c6b0b7a3dc072248"} })() @@ -54,9 +51,8 @@ You need the file `dist/cryptojs-aes.min.js` and `dist/cryptojs-aes-format.js` // decrypt value let encrypted = '{"ct":"hQDvpbAKTGp1mXgzSShR9g==","iv":"57fd85773d898d1f9f868c53b436e28f","s":"a2dac436512077c5"}' let password = '123456' - let decrypted = CryptoJS.AES.decrypt(encrypted, password, { format: CryptoJSAesJson }).toString(CryptoJS.enc.Utf8) - console.log('Decrypted JSON stringified string - You have to pass this through JSON.parse() to get original values:', decrypted) - console.log('Decrypted JSON parsed (Original Value):', JSON.parse(decrypted)) + let decrypted = CryptoJSAesJson.decrypt(encrypted, password) + console.log('Decrypted:', decrypted) })() ``` @@ -66,10 +62,15 @@ You need the file `dist/cryptojs-aes.min.js` and `dist/cryptojs-aes-format.js` * 7.x * 5.x (head to the [legacy branch](https://github.com/brainfoolong/cryptojs-aes-php/tree/legacy)) +## Upgrade Info +Breaking changes: This library has changed to PSR-4 namespaces as of 7. April 2020. Also parameters and behaviour has changed to the previous version. For the old version of this library head to the [legacy branch](https://github.com/brainfoolong/cryptojs-aes-php/tree/legacy). + ## Requirements * PHP with OpenSSL Support: http://php.net/manual/en/openssl.installation.php * Does not work with following php.ini option enabled: http://php.net/manual/en/mbstring.overload.php ## Changelog +* 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 diff --git a/composer.json b/composer.json index b5b6775..d40eefd 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ } }, "minimum-stability": "dev", - "version": "2.0.1", + "version": "2.1.0", "require": { "php": ">=7.0.0" } diff --git a/dist/cryptojs-aes-format.js b/dist/cryptojs-aes-format.js index 93e18f4..fd78c2a 100644 --- a/dist/cryptojs-aes-format.js +++ b/dist/cryptojs-aes-format.js @@ -1,18 +1,44 @@ /** * AES JSON formatter for CryptoJS - * * @link https://github.com/brainfoolong/cryptojs-aes-php - * @link https://0x.at (My other projects) - * @author BrainFooLong + * @version 2.1.0 */ var CryptoJSAesJson = { + /** + * Encrypt any value + * @param {*} value + * @param {string} password + * @return {string} + */ + 'encrypt': function (value, password) { + return CryptoJS.AES.encrypt(JSON.stringify(value), password, { format: CryptoJSAesJson }).toString() + }, + /** + * Decrypt a previously encrypted value + * @param {string} jsonStr + * @param {string} password + * @return {*} + */ + 'decrypt': function (jsonStr, password) { + return JSON.parse(CryptoJS.AES.decrypt(jsonStr, password, { format: CryptoJSAesJson }).toString(CryptoJS.enc.Utf8)) + }, + /** + * Stringify cryptojs data + * @param {Object} cipherParams + * @return {string} + */ 'stringify': function (cipherParams) { var j = { ct: cipherParams.ciphertext.toString(CryptoJS.enc.Base64) } if (cipherParams.iv) j.iv = cipherParams.iv.toString() if (cipherParams.salt) j.s = cipherParams.salt.toString() return JSON.stringify(j).replace(/\s/g, '') }, + /** + * Parse cryptojs data + * @param {string} jsonStr + * @return {*} + */ 'parse': function (jsonStr) { var j = JSON.parse(jsonStr) var cipherParams = CryptoJS.lib.CipherParams.create({ ciphertext: CryptoJS.enc.Base64.parse(j.ct) }) diff --git a/dist/example-js.html b/dist/example-js.html index 7eb56c7..deef1f0 100644 --- a/dist/example-js.html +++ b/dist/example-js.html @@ -10,7 +10,7 @@ // encrypt value let valueToEncrypt = 'foobar' // this could also be object/array/whatever let password = '123456' - let encrypted = CryptoJS.AES.encrypt(JSON.stringify(valueToEncrypt), password, { format: CryptoJSAesJson }).toString() + let encrypted = CryptoJSAesJson.encrypt(valueToEncrypt, password) console.log('Encrypted:', encrypted) // something like: {"ct":"10MOxNzbZ7vqR3YEoOhKMg==","iv":"9700d78e12910b5cccd07304333102b7","s":"c6b0b7a3dc072248"} })() @@ -20,9 +20,8 @@ // decrypt value let encrypted = '{"ct":"hQDvpbAKTGp1mXgzSShR9g==","iv":"57fd85773d898d1f9f868c53b436e28f","s":"a2dac436512077c5"}' let password = '123456' - let decrypted = CryptoJS.AES.decrypt(encrypted, password, { format: CryptoJSAesJson }).toString(CryptoJS.enc.Utf8) - console.log('Decrypted JSON stringified string - You have to pass this through JSON.parse() to get original values:', decrypted) - console.log('Decrypted JSON parsed (Original Value):', JSON.parse(decrypted)) + let decrypted = CryptoJSAesJson.decrypt(encrypted, password) + console.log('Decrypted:', decrypted) })() diff --git a/src/CryptoJsAes.php b/src/CryptoJsAes.php index 19558aa..1c0d6b4 100644 --- a/src/CryptoJsAes.php +++ b/src/CryptoJsAes.php @@ -6,10 +6,8 @@ * Encrypt/Decrypt data from Javascript's CryptoJS * PHP 7.x and later supported * If you need PHP 5.x support, goto the legacy branch https://github.com/brainfoolong/cryptojs-aes-php/tree/legacy - * * @link https://github.com/brainfoolong/cryptojs-aes-php - * @link https://0x.at (My other projects) - * @author BrainFooLong + * @version 2.1.0 */ class CryptoJsAes {