How could I convert String to 128 or 256 bit key for chacha20 Encryption .
ChaCha20Encryptor chaCha20Encryptor = new ChaCha20Encryptor();
byte[] data = chaCha20Encryptor.encrypt(plaintext.getBytes(),key2.getBytes());
String enStr = BaseUtilityHelper.encodeBase64URLSafeString(data);
encryptedTv.setText(enStr);
ChaCha20Encryptor
public class ChaCha20Encryptor implements Encryptor {
private final byte randomIvBytes[] = {0, 1, 2, 3, 4, 5, 6, 7};
static {
Security.addProvider(new BouncyCastleProvider());
}
@Override
public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException,
InvalidAlgorithmParameterException, InvalidCipherTextException {
ChaChaEngine cipher = new ChaChaEngine();
cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
byte[] result = new byte[data.length];
cipher.processBytes(data, 0, data.length, result, 0);
return result;
}
@Override
public byte[] decrypt(byte[] data, byte[] randomKeyBytes)
throws InvalidKeyException, InvalidAlgorithmParameterException, IOException,
IllegalStateException, InvalidCipherTextException {
ChaChaEngine cipher = new ChaChaEngine();
cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes));
byte[] result = new byte[data.length];
cipher.processBytes(data, 0, data.length, result, 0);
return result;
}
@Override
public int getKeyLength() {
return 32;
}
@Override
public String toString() {
return "ChaCha20()";
}
}
where
private String key2 = "19920099-564A-4869-99B3-363F8145C0BB";
private String plaintext = "Hello";
I have also tried different keys. but it requires key2 to convert it to 128 or 256 bits. I have searched on SO. and find some links
Java 256-bit AES Password-Based Encryption
Turn String to 128-bit key for AES
but these doesn't look like relevant to my requirement