使用填充密码解密时,输入长度必须是8的倍数

时间:2018-11-12 00:57:17

标签: java encryption utf-8 data-security

我有这个客户端-服务器应用程序:在一侧加密消息,然后在另一侧解密 但在解密时出现此错误“使用填充密码解密时,输入长度必须是8的倍数” 服务器类

public class ServerApp   {

    public static byte[] encrypt(String input, Key k) {
        try {

            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, k);
            byte[] data = input.getBytes();
            byte[] result = cipher.doFinal(data);

            return result;
        } catch (Exception ex) {
            return null;
        }
    }

    public static String decrypt(byte[] cipher, Key k) {
        try {

            Cipher cipher1 = Cipher.getInstance("DES");
            cipher1.init(Cipher.DECRYPT_MODE, k);
            byte[] original = cipher1.doFinal(cipher);
            return new String(original);

        } catch (Exception ex) {
            return null;
            //Logger.getLogger(DES.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        try {

              Key key=KeyGen.getSecretKey();
            ServerSocket ser = new ServerSocket(3333);
            System.out.println("Server Started");
            Socket client = ser.accept();
            DataInputStream in = new DataInputStream(client.getInputStream());
            DataOutputStream out = new DataOutputStream(client.getOutputStream());
            Scanner scan = new Scanner(System.in);
           // SecretSocket sc = new SecretSocket(client, KeyGen.getSecretKey());
            String serMsg, cliMsg, plain;
           // OutputStream sout = sc.getOutputStream();
          //  InputStream sin = sc.getInputStream();
            do {
                System.out.print("You say: ");
                serMsg = scan.nextLine();

             //   sout.write(serMsg.getBytes());
            //    System.out.println("cli server "+sin.read());

                byte[] ci = encrypt(serMsg, KeyGen.getSecretKey());
            System.out.println("encrypt " +ci.toString());
             out.writeUTF(ci.toString());
              plain = decrypt(ci, KeyGen.getSecretKey() );
              System.out.println("decrypt " + plain);
            } while (!serMsg.equals("end"));
            client.close();
            ser.close();
        } catch (IOException ex) {
            Logger.getLogger(ServerApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

这是客户代码

public class ClientApp  {

        public static byte[] encrypt(String input, Key k) {
        try {

            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, k);
            byte[] data = input.getBytes();
            byte[] result = cipher.doFinal(data);

            return result;
        } catch (Exception ex) {
            return null;
        }
    }

    public static String decrypt(byte[] cipher, Key k) {
        try {

            Cipher cipher1 = Cipher.getInstance("DES/CBC/PKCS5Padding");
            cipher1.init(Cipher.DECRYPT_MODE, k);
            byte[] original = cipher1.doFinal(cipher);
            return new String(original);

        } catch (Exception ex) {
            return null;
            //Logger.getLogger(DES.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        try {
            Socket s = new Socket("localhost", 3333);
            DataInputStream in = new DataInputStream(s.getInputStream());
            DataOutputStream out = new DataOutputStream(s.getOutputStream());
            Scanner scan = new Scanner(System.in);
            String serMsg, cliMsg, plain;

            do {

                System.out.println("server server "+in.readUTF());

                serMsg = in.readUTF();
                System.out.println("enc: " + serMsg);
                plain = decrypt(serMsg.getBytes("UTF-8"), KeyGen.getSecretKey());
                System.out.println("Server says: " + plain);

            } while (!serMsg.equals("end"));
            s.close();
        } catch (IOException ex) {
            Logger.getLogger(ClientApp.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

我尝试过此操作(“ DES / CBC / PKCS5Padding”)和(“ UTF-8”) 但仍然是同样的问题 有帮助吗??

1 个答案:

答案 0 :(得分:0)

        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, k);
        byte[] data = input.getBytes();
        byte[] result = cipher.doFinal(data);
  1. 在不使用IV的情况下使用DES就是在隐式使用DES/ECB/PKCS5Padding

  2. 不直接打印字节数组。 Java将仅输出可打印字符。 在打印时始终对字节数组进行编码和解码(十六进制或Base64是最常见的编码)

这里是example project

Base64.getEncoder().encodeToString(byteArray) 
  1. 我希望您知道DES被认为是当今的弱密码,仅应用于向后兼容。