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));
    }
}
Google+

3 Comments

Budi Oktaviyan

about 4 years ago

Just another way bro :D 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)); } }

Reply

edwin

about 4 years ago

thank you bek :p

marcelo.lima.2011@gmail.com

about 4 months ago

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) ..

Reply

Leave a Comment

Please be polite. We appreciate that.
Your email address will not be published and required fields are marked


:-[ (B) (^) (P) (@) (O) (D) :-S ;-( (C) (&) :-$ (E) (~) (K) (I) (L) (8) :-O (T) (G) (F) :-( (H) :-) (*) :-D (N) (Y) :-P (U) (W) ;-)