Java 不同的密文使用不同的钠库实现?

Java 不同的密文使用不同的钠库实现?,java,php,react-native,libsodium,Java,Php,React Native,Libsodium,我使用php使用libnaid创建密码文本,如下所示: $contents = 'Hello World!'; $key = sodium_hex2bin('9fcc21c22142e0cb30e80da941cf1fd2221a3d273b232dac12d0d195d50c0202', ''); $nonce = sodium_hex2bin('dd024212d41933c01417bd2aa2b64682224072c8aa1573f3', ''); $cipher = sodium_c

我使用php使用libnaid创建密码文本,如下所示:

$contents = 'Hello World!';
$key = sodium_hex2bin('9fcc21c22142e0cb30e80da941cf1fd2221a3d273b232dac12d0d195d50c0202', '');
$nonce = sodium_hex2bin('dd024212d41933c01417bd2aa2b64682224072c8aa1573f3', '');
$cipher = sodium_crypto_secretbox($contents, $nonce, $key);
echo bin2hex($cipher);
// output: 0af8d46269eae0788ce4c20569067724402e410b35d9a0c5197be955
然后,在我的react本机应用程序中,我使用(java包装器)生成相同的密码:

const test = async() => {
  let cipher = await Sodium.crypto_secretbox_easy(
    "Hello World!",
    "dd024212d41933c01417bd2aa2b64682224072c8aa1573f3",
    "9fcc21c22142e0cb30e80da941cf1fd2221a3d273b232dac12d0d195d50c0202"
  );
  console.log(cipher);
};
await test();
// output: 6d6ca73e3daa63a661a813c8ab6fb49f798e68965c3267eb7e1c6cdc
我用另外两个javascript库测试了该操作,然后。在这两种情况下,结果密码文本与php版本相同


出于某些原因,我更喜欢使用react native LibNade,但一天之后,我找不到为什么这个库会生成不同的密码文本,以及如何修复它。如何生成相同的密码文本?

crypto_secretbox_easy的第三个参数应该是消息的长度,然后是nonce。如图所示:

No,java函数需要消息长度作为参数,但在javascript包装器中需要3个参数,正如我在代码中使用的那样。