Q: Android - how to encrypt a string?

D: I am working on an Android app and have a couple strings that I would like to encrypt before sending to a database. I'd like something that's secure, easy to implement, will generate the same thing every time it's passed the same data, and preferably will result in a string that stays a constant length no matter how large the string being passed to it is. Maybe I'm looking for a hash.

Test Case #5


File ID: #3934409-0-cc


   public static String md5(String s) {
try {
	MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	digest.update(s.getBytes());
	byte messageDigest[] = digest.digest();
	StringBuffer hexString = new StringBuffer();
	for (int i=0; i<messageDigest.length; i++)
		hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}

  1. This code does not work properly. Some "0" characters becomes missing in the generated string. I do not know why, but that's the case.
  2. There's a special condition when this code fails. When the first of two digit Hex number is zero. This code is better: http://stackoverflow.com/a/6565597/221135
  3. MD5, afaik, is not considered reversible. Typically you'd hash something with it, commonly a password or something similar, and then to verify the password you'll run the same encryption and compare the results to what's stored.

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: