z_jun 0 Newbie Poster

Hi, I'm having problems with decryption of a string when it's long. The following are my codes for encryption and decryption. It works well when the string is short (20 chars) but throws an exception when it is about 30 chars.

The problem I have is that when I tested out this 2 functions alone in a program, it works perfectly fine no matter how long the string is. The only difference with the functions are that both functions has an extra parameter passed in which is the SecretKeySpec skeySpec.

In both of my programs, I intialized skeySpec in the main function. I'm confused as to why my program works fine in one but doesn't in another when it's obviously the same chunk of code. Please help. Thanks.

public static String encryption (String msg)
    {
        String encrypt = null;
        try {

            Cipher cipher = Cipher.getInstance("DES");
            byte[] data = msg.getBytes();
            System.out.println("Original data : " + new String(data));



            //put at encryption side
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] result = cipher.doFinal(data);
            System.out.println("Encrypted data: " + new String(result));

            encrypt = new sun.misc.BASE64Encoder().encode(result);


        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
            return encrypt;
    }
    /****************************************/
    public static String decryption (String msg)
    {
        byte[] result = null;
        byte[] original = null;

        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        try {
            try {
                result = new sun.misc.BASE64Decoder().decodeBuffer(msg);
            } catch (IOException ex) {
                Logger.getLogger(LogInPanel.class.getName()).log(Level.SEVERE, null, ex);
            }

            Cipher cipher = Cipher.getInstance("DES");
	    //put at decryption side
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            original = cipher.doFinal(result);
            //System.out.println("Decrypted data: " + new String(original));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }

        return new String(original);
       
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.