Jump to content
Sign in to follow this  
Destramic

php & nodejs sodium - compatibility

Recommended Posts

Posted (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 by Destramic
typo

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

i will give it ago...thank you for you post kicken 😀

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.