Wednesday, April 04, 2007

SimpleBlowfish encryption code

import base64.Base64;

import javax.crypto.NoSuchPaddingException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.io.UnsupportedEncodingException;

/**
* This class implements a brain-dead-simple BLowfish encryption and decryption scheme. Don't use this if you want "real"
* security, but when ROT13's just a leeeetle lightweight. Consider the results opaque--that is, don't attempt to take
* the results from the encrypt step and decrypt it using anything but the decrypt step here.
*/
public class SimpleBlowfish {

private static final String pk = "ah90y34n0a";
private static final String ALGORITHM = "Blowfish";
private static final String STRING_ENCODING = "UTF-8";


/**
* Encrypts and encodes the secret using the default encryption key. The encoded result is base64, thus safe as 7-bit ASCII.
* @param secret
* @return
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws IllegalBlockSizeException
* @throws UnsupportedEncodingException
* @throws InvalidKeyException
* @throws NoSuchPaddingException
*/
public static String encryptAndEncode(String secret) throws BadPaddingException, NoSuchAlgorithmException,
IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException, NoSuchPaddingException
{
return encryptAndEncode(secret, pk);
}

/**
* Encrypts and encodes the secret using the given encryptionKey. The encoded result is base64, thus safe as 7-bit ASCII.
* @param secret
* @param encryptionKey
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws UnsupportedEncodingException
*/
public static String encryptAndEncode(String secret, String encryptionKey)
throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException
{
byte[] kbytes = encryptionKey.getBytes(STRING_ENCODING);
SecretKeySpec key = new SecretKeySpec(kbytes, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] secretBytes = secret.getBytes(STRING_ENCODING);
byte[] secretEncryptedBytes = cipher.doFinal(secretBytes);
String secretEncryptedAndEncoded = Base64.encodeBytes(secretEncryptedBytes);
return secretEncryptedAndEncoded;
}

/**
* Decode and decrypts a secret encrypted and encoded by one of the two encrypt-and-encode methods above.
* @param secretEncryptedAndEncoded
* @return
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws IllegalBlockSizeException
* @throws UnsupportedEncodingException
* @throws InvalidKeyException
* @throws NoSuchPaddingException
*/
public static char[] decodeAndDecrypt(String secretEncryptedAndEncoded) throws BadPaddingException, NoSuchAlgorithmException,
IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException, NoSuchPaddingException
{
return decodeAndDecrypt(secretEncryptedAndEncoded, pk);
}

/**
* Decode and decrypts a secret encrypted and encoded by one of the two encrypt-and-encode methods above.
* @param secretEncryptedAndEncoded
* @param encryptionKey
* @return
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws UnsupportedEncodingException
*/
public static char[] decodeAndDecrypt(String secretEncryptedAndEncoded, String encryptionKey)
throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException
{
byte[] kbytes = encryptionKey.getBytes(STRING_ENCODING);
SecretKeySpec key = new SecretKeySpec(kbytes, ALGORITHM);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);

byte[] secretEncryptedBytes = Base64.decode(secretEncryptedAndEncoded);
byte[] secretBytes = cipher.doFinal(secretEncryptedBytes);
String secret = new String(secretBytes,STRING_ENCODING);
char[] secretChars = secret.toCharArray();
return secretChars;
}

public static void main(String[] args) throws NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException, InvalidKeyException, NoSuchPaddingException {
System.out.println("encryptAndEncode(\"marty\") = " + encryptAndEncode("marty"));
}

}