Jump to content

php & nodejs sodium - compatibility


Destramic

Recommended Posts

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.