项目中总是有一些安全问题的隐患,
首先我们的接口设计是,需要参数加密生成一个签名传上去。
具体化就是把每个参数的参数名进行排序,同时拼成字符串。把字符串进行MD5加密传给服务器,服务器在做一次自己的签名效验看看是否一致,一致接受不一致不接受保证用户通过我们的app进行访问数据。避免了其他的前端攻击我们的服务器。
通过MessgeDigest拿到MD5的信息摘要,用MD5把传进来的字符串进行转化,转化成数组。遍历数组根据16进制的1进行与运算拿到String字符串
主要代码
/**
* 对密码进行加密和验证的类
*/
public class Md5Encryption {
/**
* @param str
* @return
* @Date: 2013-9-6
* @Author: lulei
* @Description: 32位小写MD5
*/
public static String parseStrToMd5L32(String str) throws NoSuchAlgorithmException {
String reStr = null;
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(str.getBytes());
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bytes){
int bt = b&0xff;
if (bt < 16){
stringBuffer.append(0);
}
stringBuffer.append(Integer.toHexString(bt));
}
reStr = stringBuffer.toString();
return reStr;
}
/**
* @param str
* @return
* @Date: 2013-9-6
* @Author: lulei
* @Description: 32位大写MD5
*/
public static String parseStrToMd5U32(String str) throws NoSuchAlgorithmException {
String reStr = parseStrToMd5L32(str);
if (reStr != null){
reStr = reStr.toUpperCase();
}
return reStr;
}
/**
* @param str
* @return
* @Date: 2013-9-6
* @Author: lulei
* @Description: 16位小写MD5
*/
public static String parseStrToMd5U16(String str) throws NoSuchAlgorithmException {
String reStr = parseStrToMd5L32(str);
if (reStr != null){
reStr = reStr.toUpperCase().substring(8, 24);
}
return reStr;
}
/**
* @param str
* @return
* @Date: 2013-9-6
* @Author: lulei
* @Description: 16位大写MD5
*/
public static String parseStrToMd5L16(String str) throws NoSuchAlgorithmException {
String reStr = parseStrToMd5L32(str);
if (reStr != null){
reStr = reStr.substring(8, 24);
}
return reStr;
}
}