javax.crypto.BadPaddingException:解密时pad块损坏

时间:2017-01-23 13:47:57

标签: java arrays encryption stream padding

我知道这个错误一直是这个论坛上很多帖子的主题,但大多数时候这些帖子的问题都是charset ecoding beauxe人们想要密码字符串。 问题是,在我的情况下,我正在尝试加密2d字符串数组并将其从服务器传递到客户端。为此,我使用ByteArrayOutputStream我将传递给ObjectOutputStream以将此2d数组转换为字节,并在客户端白名输入流上使用相同的概念将其从字节转换回2d字符串数组。

从字符串数组到字节数组的转换是可以的,但是当我尝试使用pad block corrupted在客户端大小上解密我的字节数组时,我有一个doFinal()异常。

这是服务器代码:

case GET_NEXT_BILL:
{
    int lastGetId= (int)params.get(0);
    // recup params
    cs.TraceEvenements(adresseDistante+"#Get facture#"+Thread.currentThread().getName());

    System.out.println("Recuperation de la cle secrète");
    KeyStore.SecretKeyEntry secretKeyEntry = (KeyStore.SecretKeyEntry) servKs.getEntry("clesecComptChiffr", new KeyStore.PasswordProtection("pass".toCharArray()));
    SecretKey cléSecrète = secretKeyEntry.getSecretKey();

    Cipher chiffr = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
    chiffr.init(Cipher.ENCRYPT_MODE, cléSecrète);

    ResultSet rs = bdAccess.select("SELECT IDFacture, Montant, Destinataire, AdresseLivraison, DATE_FORMAT(DateEmission, '%d-%m-%Y') as DateEmission"
                     + " FROM FACTURES WHERE IDFacture > "+lastGetId+" AND Valide= 0");

    if(rs.next())
    {
        String[][] stringArray = {{String.valueOf(rs.getInt(1)), String.valueOf(rs.getFloat(2)), 
            rs.getString(3), rs.getString(4), rs.getString(5)}};

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(stringArray);

        rep.add(chiffr.doFinal(byteArrayOutputStream.toByteArray()));
         typeRep= TypesRep.OK; 
    }
    else
    {
        rep.add("Il n'y a plus de facture non validée");
        typeRep= TypesRep.ERREUR; 
    }    
    break;
}

客户端代码,抛出异常:

    EnvoiRequete(cliSock, new RequeteBIMAP(TypesReq.GET_NEXT_BILL, params));
    ReponseP rep = (ReponseP)LectureReponse(cliSock);

    if(rep.getTypeRet() == Reponse.TypesRep.OK){
        try {
            KeyStore.SecretKeyEntry secretKeyEntry = (KeyStore.SecretKeyEntry) cliKs.getEntry("clesecComptChiffr", new KeyStore.PasswordProtection("pass".toCharArray()));
            SecretKey cle = secretKeyEntry.getSecretKey();

            Cipher dechiffr = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
            dechiffr.init(Cipher.DECRYPT_MODE, cle);

            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(dechiffr.doFinal((byte[])rep.getParams().get(0)));

            String[][] stringArray2= null;
             try (ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream)) {
                stringArray2 = (String[][]) objectInputStream.readObject();
             }

            ListeFactures lf= new ListeFactures(stringArray2);
            lf.setVisible(true);

            lastGetID= Integer.valueOf(stringArray2[0][0]);
            FieldFacture.setText(stringArray2[0][0]);

        } catch (ClassNotFoundException | InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | IOException | IllegalBlockSizeException | BadPaddingException | UnrecoverableEntryException | KeyStoreException ex) {
            Logger.getLogger(Comptable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    else
        JOptionPane.showMessageDialog(null, rep.getParams().get(0));

0 个答案:

没有答案