0

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

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
  • Possible duplicate of [How can I hash a password in Java?](http://stackoverflow.com/questions/2860943/how-can-i-hash-a-password-in-java) – Artjom B. Jun 27 '16 at 16:22
  • If `key2` was generated with very good randomness and has no internal structure beside the dashes then a simple hash might be enough to get a 128 bit key. – Artjom B. Jun 27 '16 at 16:26

1 Answers1

0

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 :

  1. remove the dashes key2.replace("-","");
  2. 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("-","")));
TheCopycat
  • 401
  • 2
  • 6
  • I don't have latest java 1.7 . Is there any other alternate. I can't test it right now – Zar E Ahmer Jun 27 '16 at 10:09
  • There are also alternatives using apache common librairies using Hex object. There is also a solution using Google guava (but i never used it). – TheCopycat Jun 27 '16 at 11:12