【问题标题】:Java AES Encryption and Decryption: javax.crypto.IllegalBlockSizeExceptionJava AES 加密和解密:javax.crypto.IllegalBlockSizeException
【发布时间】:2022-11-03 20:01:06
【问题描述】:

Java AES 的新手!我正在探索并遵循baeldung 的教程,我在为自己看到 256 个密钥长度时遇到了这个错误:线程“主”javax.crypto.IllegalBlockSizeException 中的异常:消息必须是块大小的倍数,没有填充

我有以下内容:主要方法`

public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
        System.out.println("Encrypt/Decrypt a string");
        //3 params for AES algo: (1) input data, (2) secret key, (3) and IV
        Scanner scanner = new Scanner(System.in);
        String inputKey;
        int inputSecretKey = 256;
        IvParameterSpec IV;
        
        
        //step 1: input
        System.out.print("Input: ");
        inputKey = scanner.nextLine();
        
        //step 2: generate secret key

        System.out.println("Generating secret key with size "+inputSecretKey);
        SecretKey secretKey1 = generateKey(inputSecretKey);
        
        //step 3: generate IV
        IV = generateIv();
        
        //step 4: print
        String cipherText = encrypt("AES/CBC/PKCS5Padding", inputKey, secretKey1, IV);
        String plainText = decrypt("AES/CBC/PKCS5Padding", inputKey, secretKey1, IV);
        Assertions.assertEquals(inputKey, plainText);
        System.out.println("Encrypted: "+cipherText+" [size : "+cipherText.length()+"]");
        System.out.println("Decrypted: "+plainText+" [size : "+plainText.length()+"]");
        
        
        scanner.close();
        
    }

`

generateKey 方法`

public static SecretKey generateKey(int n) throws NoSuchAlgorithmException {
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(n);
        SecretKey key = keyGen.generateKey();
        return key;
    }

`

generateIv 方法`

public static IvParameterSpec generateIv() {
        byte[] iv = new byte[16];
        new SecureRandom().nextBytes(iv);
        return new IvParameterSpec(iv);
    }

`

加密方法`

public static String encrypt(String algorithm, String input, SecretKey key,
            IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidAlgorithmParameterException, InvalidKeyException,
            BadPaddingException, IllegalBlockSizeException {
            
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
            byte[] cipherText = cipher.doFinal(input.getBytes());
            return Base64.getEncoder()
                .encodeToString(cipherText);
    }

`

解密方法(我包括了日食指向我的具体线) `

public static String decrypt(String algorithm, String cipherText, SecretKey key,
            IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
            InvalidAlgorithmParameterException, InvalidKeyException,
            BadPaddingException, IllegalBlockSizeException {
            
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            byte[] plainText = cipher.doFinal(Base64.getDecoder()
                .decode(cipherText)); //<-------- GETTING ERROR IN THIS LINE
            return new String(plainText);
        }

`

我尝试搜索,但我认为还没有人遇到过这个问题。我唯一理解的是 AES/CBC/PKCS5Padding 代表算法/模式/填充。我确实找到了this,尽管我不确定该使用什么。我对我应该在代码中更改什么以及错误的根本原因感到非常困惑。

如果有帮助,我正在尝试像“hello”或“dFet4Q2fi”这样的输入。

【问题讨论】:

  • 您可以添加错误堆栈跟踪,也可以尝试 int inputSecretKey = 128 并检查吗?

标签: java encryption aes


【解决方案1】:

decrypt 方法的第二个参数必须是密文而不是输入:

    String plainText = decrypt("AES/CBC/PKCS5Padding", cipherText
            , secretKey1, IV);

顺便说一句:注意 java 命名约定。变量名应以小写字符开头

【讨论】:

    【解决方案2】:

    看起来您将 256 而不是 128 的参数提供给 keygenerator 方法的 alogorithm("AES/CBC/PKCS5Padding") 与 keysize 128 。

    将变量 int inputSecretKey = 256; 更改为 int inputSecretKey = 128; 它应该可以解决问题。

    【讨论】:

      猜你喜欢
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-23
      相关资源
      最近更新 更多