我正在使用以下代码在Java中加密我的电子邮件ID,并将其作为参数发送到url(使用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;
}
但是,当我从servlet获取相同的参数时,它没有提供与cryptoeInput(“ email”)相同的输出。
每当您有要存储在字符串中的字节数组时,都应该对字节数组进行十六进制或Base64编码(在这种特殊情况下,十六进制编码可能更好)。
Apache commons编解码器具有一个Hex类,您可以使用此类:
byte[] bytes = ...
char[] encoded = Hex.encodeHex(bytes);
String encodedString = new String(encoded);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句