Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem with blowfish decrypting

843811Jul 27 2005 — edited Jul 28 2005
When i'm trying to decrypt a string using Blowfish it gives me an exception ( encrypting is fine)

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher

When i move the decrypting part to the encrypting method , it works fine.
i'm in JDK 1.5 and using SunJCE
Attaching the code
  
  public static String bfEncrypt(String input) throws Exception {

            Provider sunjce = new com.sun.crypto.provider.SunJCE();
            Security.addProvider(sunjce);
            SecretKey ky = readKey(keyFile, "Blowfish");
            Cipher cipher = Cipher.getInstance("Blowfish");
            cipher.init(Cipher.ENCRYPT_MODE, ky);
            byte[] inpByte = input.getBytes("UTF8");
            byte[] ciphertext = cipher.doFinal(inpByte);
            String citxt = new String(ciphertext, "UTF8");
            return citxt;

    }

    public static String bfDecrypt(String cipherTxt) throws Exception {
        Provider sunjce = new com.sun.crypto.provider.SunJCE();
        Security.addProvider(sunjce);
        SecretKey ky = readKey(keyFile, "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.DECRYPT_MODE, ky);
        byte[] inputByte = cipherTxt.getBytes("UTF8");
        byte[] decryptedText = cipher.doFinal(inputByte);
        String output = new String(decryptedText, "UTF8");
        return output;
    }
Thanks in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 25 2005
Added on Jul 27 2005
4 comments
314 views