Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to store data (encrypted with DES) and then take the encrypted data from database and present it as a list. But I have a problem. Here is the code.

Java
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);
    }
}


The encrypt and storing is allright. But when I take data from database, I get this error when decrypt at:

Java
byte[] plainText = cipher.doFinal(DataDemo);


The error is:
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


Any solution???
Posted
Updated 19-Jul-13 23:48pm
v4
Comments
AlphaDeltaTheta 21-Jul-13 11:25am    
Have you verified that the SQL call returns the correct data?
Minh Hikari 21-Jul-13 11:30am    
Yes, it has. The only problem is: I do the decrypting and face this error.
AlphaDeltaTheta 21-Jul-13 11:53am    
And is the length a multiple of eight
Minh Hikari 21-Jul-13 12:56pm    
You mean the definition of data type in database is varbinary(multiple of 8)? I tried it too and still stuck there.

1 solution

I found the solution. But I think it's not the best.

I changed the type of table DataDemo from varbinary to image and everything became alright. But my data size store in database is heavier (about 4 times) than the oigin data.

But at least I solved my problem.

Does anyone have a better solution? I willing to hear from you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900