如何使用包含 128 个 0 的 128 位字符串创建 AES 密码

How to create a AES Cipher using 128 bit string contains 128 0's

我正在使用以下代码创建 128 位 AES 密码,

String initializer = "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";

byte[] initializer2 = new BigInteger(initializer, 2).toByteArray();

String encrypt = encrypt(binary_string_firsthalf, initializer2);

public static String encrypt(String plainText, byte[] key) {
    try {
        SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] cipherText = cipher.doFinal(plainText.getBytes("UTF8"));
        String encryptedString = new String(Base64.encode(cipherText));
        return encryptedString;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

但是代码返回一个异常,指出 AES 密钥长度无效。 如何使用 128 个 0 创建密码。

byte[] initializer2 = new byte[16];

就是初始化一个默认值为 0x00 的字节数组。


不要使用静态可预测密钥(例如所有 0x00 字节)进行实际加密。