Blowfish Encryption Algorithm In java


Using javax.crypto liberary

   
     /**
     * Blowfish algorithm implementation for the encryption of a message
     * @param messageToEncrypt
     * @param encryptionKey
     * @return array of byte
     */
    private byte[] getBlowfishEncryptedData(String messageToEncrypt, String encryptionKey){
        byte[] encryptedMessage = null;
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(encryptionKey.getBytes(), "Blowfish");
            // create a cipher based upon Blowfish
            Cipher cipher = Cipher.getInstance("Blowfish/CBC/NoPadding");
            String initializationVector = "codesolution";  
            IvParameterSpec ivParameter =  new IvParameterSpec(initializationVector.getBytes());

            // Initialise cipher to with secret key
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameter);

            encryptedMessage = cipher.doFinal(messageToEncrypt.getBytes());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            e.printStackTrace();
        }
        
        return encryptedMessage;
    }