A Simple Blowfish Encryption / Decryption using Java

This is a simple encryption using Blowfish Algorithm that i use to encrypt several properties on my application. On this example im using username appended with password as salt to encrypt password variables.

package com.edw.main;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class BlowfishTest {

    public static void main(String[] args) throws Exception {
        encrypt("edwin","password");
        decrypt("6VsVtA/nhHKUZuWWmod/BQ==");
    }

    private static void encrypt(String username, String password) throws Exception {
        byte[] keyData = (username+password).getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyData, "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] hasil = cipher.doFinal(password.getBytes());
        System.out.println(new BASE64Encoder().encode(hasil));
    }
    
    private static void decrypt(String string) throws Exception {
        byte[] keyData = ("edwin"+"password").getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(keyData, "Blowfish");
        Cipher cipher = Cipher.getInstance("Blowfish");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] hasil = cipher.doFinal(new BASE64Decoder().decodeBuffer(string));
        System.out.println(new String(hasil));
    }
}

3 thoughts on “A Simple Blowfish Encryption / Decryption using Java”

  1. Just another way bro 😀


    public class BlowfishTest {

    public static final String BLOWFISH = "Blowfish";
    public static String x;

    public static void main(String[] args) throws Exception {
    encrypt("geriii","p@ssw0rd");
    decrypt("HDXiT9dQeGXUsX8hoWNH5w==");
    }

    public static void encrypt(String username, String password) throws Exception {
    x = new StringBuilder(username).append(password).toString();
    byte[] keyData = (x).getBytes();
    SecretKeySpec secretKeySpec = new SecretKeySpec(keyData, BLOWFISH);
    Cipher cipher = Cipher.getInstance(BLOWFISH);
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    byte[] result = cipher.doFinal(password.getBytes());
    System.out.println(new BASE64Encoder().encode(result));
    }

    public static void decrypt(String param) throws Exception {
    byte[] keyData = (x).getBytes();
    SecretKeySpec secretKeySpec = new SecretKeySpec(keyData, BLOWFISH);
    Cipher cipher = Cipher.getInstance(BLOWFISH);
    cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
    byte[] result = cipher.doFinal(new BASE64Decoder().decodeBuffer(param));
    System.out.println(new String(result));
    }
    }

  2. marcelo.lima.2011@gmail.com

    I’m not Java programmer, and I’m trying to make this implementation. I know it’s been four years, but I can not help noticing that the decript function requires the encrypted text size (which will not have) ..

Leave a Comment

Your email address will not be published.