0

I am using following code to encrypt my email id in Java and sending it as a parameter in url (Using URLEncoder.encode(encrypteInput("email"))):

public static String encrypteInput(String input) {
        String output = null;
        input = input + ((int) Math.random()) % 1000;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            output = new String(md5.digest(input.getBytes()));
        } catch (Exception e) {
            output = "";
        }
        return output;
}

but, when I am getting the same parameter from servlet, it is not giving me the same output as encrypteInput("email").

Vinit Sharma
  • 461
  • 2
  • 8
  • 16

1 Answers1

0

Whenever you have a byte array that you want to store in a string, you should be Hex- or Base64-encoding the byte array (hex-encoding is probably better in this particular case).

Apache commons-codec has a Hex class you can use for this:

byte[] bytes = ...
char[] encoded = Hex.encodeHex(bytes);
String encodedString = new String(encoded);
dnault
  • 8,340
  • 1
  • 34
  • 53