Search the Community
Showing results for tags 'crypto'.
-
after more help from jacques1 i've been tring to use nodejs's crypto...i want to be able to encryt data via js and decrypt via php and visa versa. in my aes.js i have created function so i can encrypt and decrypt data although i get an error on both and i'm completely stuck! to be honest all this encrypting and decrypting is quite confusing to say the least and could really do with some more help...the iv length is set at 32 which is obviously too shore or too long...where am i going wrong here please? here is all my code that i am using for this test.js var aes = require('./aes'), master_key = "5e2a0626516f3108e55e25e4bb6a62835c2f5d2b2b8d194c9acca63ef8beff6bfb947233bd83cfda9021e5a80bc183bcd835180c9955b733fd1a6d9d"; var decrypt = aes.decrypt("yUB2jQe88yWT2yUNHstMCw==", "2a3dc736bc316e9a20566b9a108eb23a", master_key); console.log('decrypt', decrypt); var encrypted = aes.encrypt('destramic', master_key); console.log('encrypt', encrypted); // array from aes.php encrypted and decrypted. //Array //( // [0] => Array // ( // [name] => yUB2jQe88yWT2yUNHstMCw== // [name_init_vector] => 2a3dc736bc316e9a20566b9a108eb23a // [age] => PjDxZq2x5IrhsLcqaJd5JQ== // [age_init_vector] => 01b53388ddcd8352db17e30f44e9b9e8 // ) // // [1] => Array // ( // [name] => LRxu9zAdlkSgS+wmyi4PrQ== // [name_init_vector] => 1c3d3bf95168ab025ca5c63ec3926313 // [age] => ys6yPHqjtutq/PJ5G3r0FQ== // [age_init_vector] => f19b6975eece8d8995e86b0ee6a33569 // ) // //) //Array //( // [0] => Array // ( // [name] => destramic // [age] => 28 // ) // // [1] => Array // ( // [name] => alan // [age] => 99 // ) // //) aes.js - where my problem ara... var crypto = require('crypto'), encryption_algorithem = 'AES-128-CBC'; module.exports = { encrypt: function (text, master_key){ try { var init_vector = new Buffer(crypto.randomBytes(32)); ciphertext = crypto.createCipheriv(encryption_algorithem, master_key, init_vector); init_vector = init_vector.update('hex'); return {'ciphertext' : ciphertext, 'init_vector' : init_vector }; }catch(error){ console.log(error); return null; } }, decrypt: function (ciphertext, init_vector, master_key){ try { var decipher = crypto.createDecipheriv(encryption_algorithem, master_key, init_vector), decrypted = decipher.update(ciphertext, 'bin'); return decrypted; }catch(error){ console.log(error); return null; } } }; aes.php <?php const ENCRYPTION_ALGORITHM = 'AES-128-CBC'; class AES { private $_master_key; public function __construct() { $this->_master_key = "5e2a0626516f3108e55e25e4bb6a62835c2f5d2b2b8d194c9acca63ef8beff6bfb947233bd83cfda9021e5a80bc183bcd835180c9955b733fd1a6d9d"; } public function generate_master_key($length = 64) { if (!is_numeric($length)) { return null; } $max_attempts = 10; $attempts = 0; do { $bytes = openssl_random_pseudo_bytes($length, $cryptographically_strong); $attempts++; } while (!$cryptographically_strong && $attempts < $max_attempts); if (!$cryptographically_strong) { return false; } $hex = bin2hex($bytes); return $hex; } public function encrypt($value, $master_key) { $init_vector = openssl_random_pseudo_bytes(openssl_cipher_iv_length(ENCRYPTION_ALGORITHM)); $ciphertext = openssl_encrypt($value, ENCRYPTION_ALGORITHM, $master_key, false, $init_vector); return array( 'init_vector' => bin2hex($init_vector), 'ciphertext' => $ciphertext ); } public function decrypt($ciphertext, $init_vector, $master_key) { $plaintext = openssl_decrypt($ciphertext, ENCRYPTION_ALGORITHM, $master_key, false, hex2bin($init_vector)); return $plaintext; } public function encrypt_array($array) { $encrypted_array = array(); $master_key = $this->_master_key; foreach ($array as $key => $data) { foreach ($data as $column => $value) { $encryption = $this->encrypt($value, $master_key); $init_vector_column = $column . '_init_vector'; $encrypted_array[$key][$column] = $encryption['ciphertext']; $encrypted_array[$key][$init_vector_column] = $encryption['init_vector']; } } return $encrypted_array; } public function decrypt_array($array) { $decrypted_array = array(); $master_key = $this->_master_key; foreach ($array as $key => $data) { foreach ($data as $column => $value) { $init_vector = $column . '_init_vector'; if (array_key_exists($init_vector, $data)) { $init_vector = $data[$init_vector]; $decrypted_value = $this->decrypt($value, $init_vector, $master_key); $decrypted_array[$key][$column] = $decrypted_value; } } } return $decrypted_array; } } $aes = new AES; $data = array( array('name' => 'destramic', 'age' => '28'), array('name' => 'alan', 'age' => '99') ); $encryption = $aes->encrypt_array($data); print_r($encryption); $decryption = $aes->decrypt_array($encryption); print_r($decryption); i hope someone can educate me a bit more here...thank you