Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
added handy js encrypt and decrypt function
Browse files Browse the repository at this point in the history
  • Loading branch information
brainfoolong committed Dec 30, 2020
1 parent c7f20a8 commit afef8f0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 18 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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"}
})()
Expand All @@ -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)
})()
</script>
```
Expand All @@ -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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
},
"minimum-stability": "dev",
"version": "2.0.1",
"version": "2.1.0",
"require": {
"php": ">=7.0.0"
}
Expand Down
32 changes: 29 additions & 3 deletions dist/cryptojs-aes-format.js
Original file line number Diff line number Diff line change
@@ -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) })
Expand Down
7 changes: 3 additions & 4 deletions dist/example-js.html
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
})()
Expand All @@ -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)
})()
</script>
</head>
Expand Down
4 changes: 1 addition & 3 deletions src/CryptoJsAes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit afef8f0

Please sign in to comment.