首页 > 解决方案 > IllegalBlockSizeException:使用填充密码解密时输入长度必须是 8 的倍数?

问题描述

对于此代码,我正在尝试解密消息。但我不断收到 IllegalBlockSizeException。

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Decrypt {

  static  Cipher DESCipher;
  static  KeyGenerator KEY_generator;
  static  SecretKey myDesKey;
  static byte[] textEncrypted;

    public static byte[] Decrypt(byte[] input, String sk_string) {
        try {
            //Convert String to secret key
            byte[] decodedKey = Base64.getDecoder().decode(sk_string);
            SecretKeySpec originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
            //init the Mode
            DESCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

            // Initialize the same cipher for decryption
            DESCipher.init(Cipher.DECRYPT_MODE, originalKey);

            // Decrypt the text
            byte[] textDecrypted = DESCipher.doFinal(input);

            //Return the text has been decrypted
            System.out.println("Text Decryted : " + new String(textDecrypted));
            return textDecrypted;
        } catch (IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String sk = "3Kh/EMEHxBA=";
        String input = "[B@7a92922";
        byte[] gb= input.getBytes();
        Decrypt(gb,sk);

    }
}

我的加密类有效,但我无法正确填充输入以进行解密以取回消息。这是我的加密类:

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.io.*;
import java.util.Base64;


public class Encrypt {

   static Cipher DESCipher;
   static KeyGenerator KEY_generator;
   static SecretKey myDesKey;
   static  byte[] textEncrypted;

    public static byte[] Encrypt(String user_input, String sk_string) {
        try {
            //Convert String to secret key
            byte[] decodedKey = Base64.getDecoder().decode(sk_string);
            SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
            // Create the cipher type as DES
            DESCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

            // Initialize the cipher for encryption
            DESCipher.init(Cipher.ENCRYPT_MODE, originalKey);


            //Turn the String of user_input to the byte mode
            byte[] text = user_input.getBytes();

            // Encrypt the text -> Do final
            textEncrypted = DESCipher.doFinal(text);

            //Return the text;
            //System.out.println(textEncrypted);
            return textEncrypted;

        } catch (NoSuchAlgorithmException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String s= "hello everyone";
        String sk= "3Kh/EMEHxBA=";
        System.out.println(s);
        System.out.println("Text encrypted : " + Encrypt(s,sk));

    }

}

谁能告诉我出了什么问题以及我的代码应该如何修复它?我必须将类分开并将密钥、密码和明文作为字符串输入。谢谢!

标签: javaencryptioncryptographypaddingdes

解决方案


推荐阅读