0

I used php to create a cipher text using libsodium, like that:

$contents = 'Hello World!';
$key = sodium_hex2bin('9fcc21c22142e0cb30e80da941cf1fd2221a3d273b232dac12d0d195d50c0202', '');
$nonce = sodium_hex2bin('dd024212d41933c01417bd2aa2b64682224072c8aa1573f3', '');
$cipher = sodium_crypto_secretbox($contents, $nonce, $key);
echo bin2hex($cipher);
// output: 0af8d46269eae0788ce4c20569067724402e410b35d9a0c5197be955

Then, in my react native app I used react-native-libsodium (a wrapper for java libsodium-jni) to generate the same cipher:

const test = async() => {
  let cipher = await Sodium.crypto_secretbox_easy(
    "Hello World!",
    "dd024212d41933c01417bd2aa2b64682224072c8aa1573f3",
    "9fcc21c22142e0cb30e80da941cf1fd2221a3d273b232dac12d0d195d50c0202"
  );
  console.log(cipher);
};
await test();
// output: 6d6ca73e3daa63a661a813c8ab6fb49f798e68965c3267eb7e1c6cdc

I tested the operation with two other javascript libraries, sodium-native and sodium-plus. In the both these situations, the result cipher text is same as php version.

For some reasons, I perfer using react-native-libsodium, but after a day I could not find why this library generates the different cipher text and how can I fix that. How can I generate the same cipher text?

JalalJaberi
  • 2,417
  • 8
  • 25
  • 41

1 Answers1

0

The third argument of crypto_secretbox_easy should be the length of the message, and then after should be the nonce. As seen here: https://stackoverflow.com/a/23646032/5878961

Timberman
  • 647
  • 8
  • 24
  • No, the java function needs message length as parameter, but in the javascript wrapper 3 parameters are needed as I used in the code. – JalalJaberi Sep 23 '20 at 11:33