IllegalArgumentException ChaCha20需要128位或256位密钥 [英] IllegalArgumentException ChaCha20 requires 128 bit or 256 bit key

查看:228
本文介绍了IllegalArgumentException ChaCha20需要128位或256位密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何将字符串转换为128或256位的chacha20加密密钥。

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()";
    }
}

其中

private String key2 = "19920099-564A-4869-99B3-363F8145C0BB";
private String plaintext = "Hello";

我也尝试过不同的键。但它需要key2将其转换为128位或256位。我在SO上搜索过。并找到一些链接

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位AES密码加密

将字符串转换为AES的128位密钥

但这些看起来不像我的要求

but these doesn't look like relevant to my requirement

推荐答案

您的密钥,没有破折号,是一个完全有效的128位十六进制密钥。它是32个字符,它是16个字节=> 128位。

Your key, without the dashes, is a perfectly valid 128 bits hexadecimal key. It is 32 characters, which is 16 bytes => 128 bits.

所有你需要做的是:


  1. 删除破折号 key2.replace( - ,);

  2. 将十六进制字符串转换为其byte []表示形式。我个人使用了$ 1.7以上的 javax.xml.bind.DatatypeConverter 。使用如下: DatatypeConverter.parseHexBinary(hexString);

  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("-","")));

这篇关于IllegalArgumentException ChaCha20需要128位或256位密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆