Illegalargumentexception Chacha20 Requires 128 Bit Or 256 Bit Key
How could I convert String to 128 or 256 bit key for chacha20 Encryption . ChaCha20Encryptor chaCha20Encryptor = new ChaCha20Encryptor(); byte[] data = chaCha20Encryptor.
Solution 1:
Your key, without the dashes, is a perfectly valid 128 bits hexadecimal key. It is 32 characters, which is 16 bytes => 128 bits.
All you have to do is :
- remove the dashes
key2.replace("-","");
- Convert the hexadecimal String to its byte[] representation. I personally use
javax.xml.bind.DatatypeConverter
available for java 1.7 and above. Use it like this :DatatypeConverter.parseHexBinary(hexString);
Your complete call should look like this :
chaCha20Encryptor.encrypt(
plaintext.getBytes(),
DatatypeConverter.parseHexBinary(key2.replace("-","")));
Post a Comment for "Illegalargumentexception Chacha20 Requires 128 Bit Or 256 Bit Key"