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!

encryption/decryption using Blowfish

843811Dec 20 2007 — edited Dec 21 2007
Hi. I was reading through javaalmanc.com and I came across a few pages on cryptography. I thought I would try my hands on it and decided to use Blowfish since I heard it is rather good encryption. Below is my cryptography class and my main class . I have a weird error message while I was trying to decrypt . Can anyone suggest or provide samples to fix the code and I would like to convert the random code generated from the Blowfish key to String so I can print it out to read the random bytes and also how should I convert the key in String format back to Key. How should I go about doing that ?
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Owner
 */
public class Cryptography {

    Cipher ecipher;
    Cipher dcipher;

    public Cryptography(SecretKey key) {
        try {
            byte[] iv = new byte[8]; // not sure if I should put the 8 bytes and paramSpec
            AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
            ecipher = Cipher.getInstance("Blowfish");
            dcipher = Cipher.getInstance("Blowfish");
            ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
            dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
        } catch (Exception ex) {
            Logger.getLogger(Cryptography.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String encrypt(String str) {
        try {

            byte[] value = str.getBytes("UTF8");
            byte[] etext = ecipher.doFinal(value);
            BASE64Encoder base64 = new BASE64Encoder();
            return base64.encode(value);
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(Cryptography.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(Cryptography.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Cryptography.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public String decrypt(String str) throws IOException {
        try {
            BASE64Decoder base64 = new BASE64Decoder();
            byte[] value = base64.decodeBuffer(str);
            byte[] dtext = dcipher.doFinal(value);
            return new String(dtext, "UTF8");
        } catch (IllegalBlockSizeException ex) {
            Logger.getLogger(Cryptography.class.getName()).log(Level.SEVERE, null, ex);
        } catch (BadPaddingException ex) {
            Logger.getLogger(Cryptography.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Cryptography.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
Main class content:
SecretKey key = KeyGenerator.getInstance("Blowfish").generateKey();
            crypto = new Cryptography(key);
            System.out.println(key.toString());
            String encryptText = crypto.encrypt("This is Blowfish encryption");
            String decryptText = crypto.decrypt(encryptText);
Error:

javax.crypto.spec.SecretKeySpec@d97afe4d
Dec 20, 2007 4:55:29 PM passmanager.Cryptography decrypt
SEVERE: null
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)


You do notice the javax.crypto.spec........ is an effort I tried to print out the Blowfish key using toString() but is not the key bytes I wanted to read.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 18 2008
Added on Dec 20 2007
8 comments
852 views