0

我想存储数据(用 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

有人有这个解决方案吗???

4

2 回答 2

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;
}
于 2013-07-19T06:10:44.863 回答
0

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

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

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

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

于 2013-07-22T02:26:37.420 回答