I am using the ExtJS framework. I have a MD5 function in JavaScript to encrypt some JSON. My backend uses Java so I wanted to know how to decrypt a
MD5 is a hash (i.e. one-way transformation), so you can't decrypt it. You can compare a known hash with hash computed from a cleartext to verify validity of input. Java has inbuilt library for this. I have used this code below, adapt as necessary, also verify that your hashes generated in javascript have the same encoding.
import java.security.MessageDigest;
import sun.misc.BASE64Encoder;
public class MD5Sample {
/**
* Cipher for encode
*/
private final MessageDigest md;
public MD5Sample() throws SecurityException {
try {
md = MessageDigest.getInstance("MD5", "SUN");
}catch(Exception se) {
throw new SecurityException("In MD5 constructor " + se);
}
}
public String encode(String in) throws Exception {
if (in == null) {
return null;
}
try {
byte[] raw = null;
byte[] stringBytes = null;
stringBytes = in.getBytes("UTF8");
synchronized(md) {
raw = md.digest(stringBytes);
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(raw);
} catch (Exception se) {
throw new Exception("Exception while encoding " + se);
}
}
public String decode(String in) {
throw new RuntimeException("NOT SUPPORTED");
}
/**
* Test harness
* @param args
*/
public static void main(String[] args) {
String clearText = "apple";
try {
MD5Sample app = new MD5Sample();
String encryptedHash = app.encode(clearText);
System.out.println(encryptedHash);
} catch (Exception e) {
e.printStackTrace();
}
}
}