I've an Android (java) program which uses ChaCha20-Poly1305 to encrypt some strings, and a python script to decrypt the data.
Here's the code to encrypt data:
byte[] plainText = "This is a sample test".getBytes(StandardCharsets.UTF_8);
byte[] key = "11111111111111111111111111111111".getBytes();
byte[] nonce = "nnnnnnnnnnnn".getBytes();
IvParameterSpec ivSpec = new IvParameterSpec(nonce);
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "ChaCha20");
Cipher cipher = Cipher.getInstance("ChaCha20-Poly1305");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
String nt = java.util.Base64.getEncoder().encodeToString(ivSpec.getIV());
String ct = java.util.Base64.getEncoder().encodeToString(cipher.doFinal(plainText));
Here's the python script to decrypt:
chacha_key = '11111111111111111111111111111111'.encode()
nonce = 'nnnnnnnnnnnn'.encode()
chacha_cipher = ChaCha20_Poly1305.new(key=chacha_key, nonce=nonce)
plain_text = chacha_cipher.decrypt(ciphertext)
But the problem is, he plain text always have garbage characters at the trailing end:
b'This is a sample test\xcbn\x1c\xbd\xa4/\xfc\xc6X\xf4\x93\xb4\xeb3\xcf\xf5'
By default the string is UTF-8 encoded, and i can't think of an issue in the mechanism too. Any suggestions why I'm getting garbage values?