message digests are sufficient for most simple applications. I have used them myself for encrypting passwords. The flip side is you cannot 'decrypt' a digest to get the original password. The way they work is that a given password
string will *always* yeild the same message digest string. Here is some code you can use for this purpose. This always gives a 32 byte MD5 hash of the passed in String(password). Note that you will need to *always* compare the digest of the passwords for authentication.
public String getHash(String inputString) throws EncryptionException
{
String hashCode = null;
MessageDigest msd = MessageDigest.getInstance("MD5", "SUN");
BigInteger bInt= null;
StringBuffer stBuf = null;
try{
// msd.digest() calculates the hash and returns a byte array.
// The byte array is then converted into String equivalent and returned
// 1 stands for + sign
// The byte array is taken to be in big-endian byte-order:
// the most significant byte is in the zeroth element.
bInt = new BigInteger( 1, msd.digest(inputString.getBytes()) );
stBuf = new StringBuffer(bInt.toString( 16 ));
}catch(NumberFormatException nfe){
///log
}
//Packing the left side of the resultant String with 0s if it is
//not equal to 32 chars.
int lt = stBuf.length();
for(int len = 0; len <32-lt;len++) {
stBuf.insert(0,'0');
}
hashCode = stBuf.toString();
return hashCode;
}
However, if you are keen on a key based cipher encryption algorithm. Then a simple way is to use a secret key algorithm (eg Blowfish). Here there is only ONE key used for both encryption and decryption. Here is some example code you can use. Note this code generates a key first then uses it to encrypt some string then decrypts it. However, you will need to generate the secret key ONLY once, then store it in some secure place and thereafter read the key first & then use it to encrypt/decrypt. To run this u need jvm 1.4 and need to configure the SUN JCE security provider:
KeyGenerator kg = null;
try {
kg = KeyGenerator.getInstance("Blowfish","SunJCE");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
kg.init(128);
SecretKey sk = kg.generateKey();
Cipher ci = null;
try {
ci = Cipher.getInstance("Blowfish");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
}
try {
ci.init(Cipher.ENCRYPT_MODE,sk);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
//Encrypting
String st = "SomeString";
byte[] inputBytes = null;
try {
inputBytes = st.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte [] encryptedBytes = null;
try {
encryptedBytes = ci.doFinal(inputBytes);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
String encryptedData = null;
try {
encryptedData = new String(encryptedBytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("encrypted string "+encryptedData);
//Decrypting
try {
ci.init(Cipher.DECRYPT_MODE,sk);
} catch (InvalidKeyException e) {
e.printStackTrace();
}
byte [] decryptedBytes = null;
try {
decryptedBytes = ci.doFinal(encryptedBytes);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
String decryptedData = null;
try {
decryptedData = new String(decryptedBytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("decrypted string "+decryptedData);