terl / lazysodium-java

A Java implementation of the Libsodium crypto library. For the lazy dev.
https://github.com/terl/lazysodium-java/wiki
Mozilla Public License 2.0
134 stars 46 forks source link

Random Output For sodiumJava.crypto_aead_xchacha20poly1305_ietf_encrypt() function #92

Closed shuguang101 closed 3 years ago

shuguang101 commented 3 years ago

my code: byte[] key = Hex.decode("2be077349f80bf45faa1f427e81d90dbdc90a1d8d4212c1dacf2bd870000bfdf"); byte[] data = Hex.decode("23dbad0780"); byte[] nonce = Hex.decode("ddfa69041ecc3feeb077cf45"); byte[] nSec = Hex.decode("00000000");

SodiumJava sodiumJava = new SodiumJava(); byte[] cipherData = new byte[data.length]; long[] cLen = new long[]{cipherData.length}; int isSuccess = sodiumJava.crypto_aead_xchacha20poly1305_ietf_encrypt(cipherData, cLen, data, data.length, null, 0, nSec, nonce, key); System.out.println(isSuccess + " " + Hex.toHexString(cipherData));

like this: 0 bde70da3a0 0 4835fac26e 0 bde70da3a0

The same code, using the sodiumJava.crypto_aead_chacha20poly1305_ietf_encrypt() the output always the:
0 24862db2bb

shuguang101 commented 3 years ago

nonce should be 24 bytes for crypto_aead_xchacha20poly1305_ietf_encrypt nonce should be 12 bytes for crypto_aead_chacha20poly1305_ietf_encrypt

I used 12bytes nonce for crypto_aead_xchacha20poly1305_ietf_encrypt(should use 24 bytes nonce)