BadPaddingException:给定最终块未正确填充(chat client-server java)

时间:2018-02-15 17:08:16

标签: java

我正在尝试实现一个允许交换文本和文件的聊天客户端服务器(在本地)。我利用java.security和java.crypto来实现混合加密(以及Swing工具)。 我实现了文本交换,它工作正常,现在我正在尝试使用AES-128加密实现文件传输。 encrypt方法接受输入由FileChoser选择的选定文件,然后它接受文件的字节并将它们插入字节数组。使用“cipher2”后我加密这些字节并且由于“cipher2.doFinal”我取字节[ ] fileBytes。最后我在文件文件中写了“fileBytes”。

public File encryptFile(File file) throws  BadPaddingException,IllegalBlockSizeException,NoSuchPaddingException,NoSuchAlgorithmException,InvalidKeyException, InvalidAlgorithmParameterException, FileNotFoundException, IOException {


      cipher2 = null;
    byte[] encfile=null;
    byte[] fileBytes=new byte[(int) file.length()];
    cipher2=Cipher.getInstance("AES/CBC/PKCS5PADDING");//creo il cifrario cipher2 con le specifiche riportate
    cipher2.init(Cipher.ENCRYPT_MODE,aesKey,new IvParameterSpec(IV.getBytes()));//inizializzo il cifrario specificando la chiave di cifratura
    FileInputStream fis=new FileInputStream(file);
    BufferedInputStream buf=new BufferedInputStream(fis);
    buf.read(fileBytes,0,fileBytes.length);
    encfile=cipher2.doFinal(fileBytes);//creo il messaggio cifrato
    FileUtils.writeByteArrayToFile(file, encfile);//file (that is selectedFile) results not encrypted in his path
   File ff=new File("C:/Users/ferdinando/Desktop/ciaocript/" + file.getName()); //ff result encrypted in ciaocript
     FileUtils.writeByteArrayToFile(ff, encfile);
    fis.close();
    buf.close();
       return file;

}

我以序列化方式发送加密文件后感谢ObjectOutputStream的方法writeObject():

public void sendFile(){
       try{


    sOutput.writeObject( encryptFile(selectedFile));
    jTextArea1.append("File inviato" + "\n");

    }
    catch(IOException ioe){
    System.out.println("Errore nell'invio del file " + ioe.getMessage());

    }
       catch(InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e ){
   System.out.println(" Errore nell'invio del file" + e.getMessage());
  }

  }

另一方面,服务器验证对象是否是File对象,如果这是真的,则以这种方式解密文件:

public void decryptFile(File encfs){

    ServerDecryptCipher = null;

    try {
        byte[] fileBytes;
        byte[] encBytes=new byte[(int) encfs.length()];
        FileInputStream fis=new FileInputStream(encfs);
        BufferedInputStream buf=new BufferedInputStream(fis);
        buf.read(encBytes,0,encBytes.length);
        ServerDecryptCipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
    ServerDecryptCipher.init(Cipher.DECRYPT_MODE, AESKey, new IvParameterSpec(IV.getBytes()));
    fileBytes = ServerDecryptCipher.doFinal(encBytes);
        FileUtils.writeByteArrayToFile(receivedFile, fileBytes);
        File saveFile=new File(path+"/"+ receivedFile.getName());
       save(receivedFile,saveFile);
        TextAreaChat.append("File ricevuto: "+ receivedFile.getName() + "\n");


    } catch(IOException | InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException | NullPointerException e) {
    e.getCause();
    e.printStackTrace();
    TextAreaEventiLog.append("INFO: Errore nella decrittografia del file ricevuto dal client. Errore:"  + e.getMessage() + "\n");
}
}

0 个答案:

没有答案