Destramic Posted April 14, 2019 Share Posted April 14, 2019 (edited) i've been playing around with nodejs sodium, and im trying to make my js scipt compatable with my php script. Both are using xchacha20poly1305m but the problem is my php encryption looks like this: (whatever it is) ��Bd���||�%��AG�wU�Q��[�V���ȷ&6����_�Y:�q��T��X��e"v� and my js encrption look like that: Uint8Array {0: 195, 1: 119, 2: 234, 3: 247, 4: 236…} theres no way of testing the compatability of both scripts when both encodings differ. here is my php <?php CONST NONCE_LENGTH = SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES; class Sodium { private $key; public function __construct(string $key) { if (!extension_loaded('sodium')) { throw new Exception('Encryption: PHP Sodium extension not found.'); } $key = trim($key); if (!preg_match('/^[0-9A-Fa-f]{64}+$/', $key)) { throw new Exception('Encryption: Unrecognized key.'); } $this->key = hex2bin($key); } public function encrypt(string $plaintext) { $nonce = random_bytes(NONCE_LENGTH); $ciphertext = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt( $plaintext, null, $nonce, $this->key ); return $nonce . $ciphertext; } public function decrypt(string $encryption) { if (strlen($encryption) < 24) { throw new Exception('Encryption: Unrecognized Encryption.'); } $plaintext = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt( substr($encryption, 24), null, substr($encryption, 0, 24), $this->key ); if ($plaintext === false) { throw new Exception('Encryption: Decryption Failed.'); } return $plaintext; } } $sodium = new Sodium('724b092810ec86d7e35c9d067702b31ef90bc43a7b598626749914d6a3e033ed'); $encryption = $sodium->encrypt('shhh this is a secret'); echo $encryption; echo $sodium->decrypt($encryption); and you can view the js live here https://codesandbox.io/s/l45orjlnk7 note: when viewing page you may have to delete a character of the code and replace to see results ? or theres a screenshot https://ibb.co/XkcPV4B i hope you can help with my dilemma thank you Edited April 14, 2019 by Destramic typo Quote Link to comment Share on other sites More sharing options...
kicken Posted April 14, 2019 Share Posted April 14, 2019 Output your strings using bin2hex in PHP and sodium.to_hex in JS. Then you can compare them easily. Take your hex string that PHP outputs and use sodium.from_hex in the JS to import it. Quote Link to comment Share on other sites More sharing options...
Destramic Posted April 14, 2019 Author Share Posted April 14, 2019 i will give it ago...thank you for you post kicken ? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.