解密数据库中的数据时出现“javax.crypto.IllegalBlockSizeException”

时间:2013-07-19 04:59:25

标签: java

我想存储数据(使用DES加密),然后从数据库中获取加密数据并将其作为列表显示。但我有一个问题。这是代码。

    public void EncryptDemo(){
    try {
        FileInputStream keyfis = new FileInputStream("mainkey.key");
        byte[] encodedKey = new byte[keyfis.available()];
        keyfis.read(encodedKey);
        keyfis.close();
        Key KeyFromFile = new SecretKeySpec(encodedKey, "DES");
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        String text=txtToEncryptData.getText(), output;
        cipher.init(Cipher.ENCRYPT_MODE, KeyFromFile);
        DataDemo = cipher.doFinal(text.getBytes());
        InsertIntoDataBase();
        //I store it as varbinary in database
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

    public void DecryptDemo(){
    try {
        FileInputStream keyfis = new FileInputStream("mainkey.key");
        byte[] encodedKey = new byte[keyfis.available()];
        keyfis.read(encodedKey);
        keyfis.close();
        Key KeyFromFile = new SecretKeySpec(encodedKey, "DES");
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, KeyFromFile);
        String sql = "{call SelectAll}";
        CallableStatement call = conn.prepareCall(sql);
        call.execute();
        ResultSet result = call.getResultSet();
        DefaultListModel model = new DefaultListModel();
        while(result.next()){
            DataDemo = result.getBytes("TestData");
            byte[] plainText = cipher.doFinal(DataDemo);
            String after = new String(plainText);
            model.addElement(after);
        }
        lstDecryptResult.setModel(model);
    } catch (SQLException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchAlgorithmException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (NoSuchPaddingException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (InvalidKeyException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IllegalBlockSizeException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BadPaddingException ex) {
        Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

加密和存储是好的。但是当我从数据库中获取数据时,我在解密时遇到此错误(在byte [] plainText = cipher.doFinal(DataDemo);)

Jul 19, 2013 11:40:05 AM databaseencryptdecryptdemo.MainGUI DecryptDemo
SEVERE: null
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

任何人都有这个解决方案???

2 个答案:

答案 0 :(得分:0)

您应该将DataDemo变量分成8个字节。

public List<Byte[]> divideInto8(Byte[] bytes) {
    int length = bytes.length;
    List<Byte[]> returnValues = new ArrayList<Byte[]>();
    for (int i = 0; i < length; i = i + 8) {
        Byte[] thebytes = new Byte[8];
        for (int j = 0; j < 8; j++) {

            thebytes[j] = bytes[i * 8 + j];
        }
        returnValues.add(thebytes);
    }
    return returnValues;
}

答案 1 :(得分:0)

我找到了解决方案。但我认为这不是最好的。

我将表DataDemo的类型从varbinary更改为image,一切都变好了。但是我在数据库中的数据大小存储比oigin数据更重(约4倍)。

但至少我解决了我的问题。

有没有人有更好的解决方案?我愿意听取你的意见。