jsp中怎么实现MD5加密
1个回答
展开全部
写编写一个MD5加密工具类
通过import方式将MD5加密工具类引入到jsp中,然后使用MD5工具类进行加密
下面是MD5加密工具类代码
import java.security.MessageDigest;
public class MD5Util {
private static String byteArrayToHexString(byte b[]) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++)
resultSb.append(byteToHexString(b[i]));
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n += 256;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString
.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
public static void main(String[] args){
String s= "appid=wxc7dac6b76548e969&attach=106&auth_code=130099732281083883&body=医泰天下远程会诊-20161019162200QCVV&mch_id=1393510702&nonce_str=cwou747cgu24fit97n3wazidm6g2ii6n&out_trade_no=20161019162200QCVV&time_expire=20161028192424&time_start=20161028182424&total_fee=1&key=szetmed20160408dehui013691632869";
System.out.println(MD5Encode(s,null));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询