|
5精币
传入
Function.encrypt("{\"Phone\":\"" + LoginActivity.this.post_phone + "^android_" + Function.getVersionName(LoginActivity.this) + "\",\"Password\":\"" + Function.md5(LoginActivity.this.post_psd) + "\"}")
下面是代码
import org.json.JSONObject;
public static String encrypt(String data) throws Exception {
String result = "";
List<String> dataString = new ArrayList();
JSONObject jsonData = new JSONObject(data);
Iterator<String> jsonList = jsonData.keys();
while (jsonList.hasNext()) {
String key = (String) jsonList.next();
String val = jsonData.getString(key);
if (val.indexOf("{") > -1 && val.indexOf("}") > -1 && val.indexOf("[引号]") < 0) {
JSONArray list = new JSONArray(val);
List<List<String>> valDataString = new ArrayList();
for (int i = 0; i < list.length(); i++) {
JSONObject jsonObject = list.getJSONObject(i);
List<String> subDataString = new ArrayList();
Iterator<String> jsonObjectList = jsonObject.keys();
while (jsonObjectList.hasNext()) {
String subKey = (String) jsonObjectList.next();
subDataString.add(subKey + "|" + jsonObject.getString(subKey));
}
Collections.sort(subDataString);
valDataString.add(subDataString);
}
val = "[";
for (List<String> subList : valDataString) {
val = val + "{";
for (String subItem : subList) {
String[] lastData = subItem.split("\\|");
val = val + "\"" + lastData[0] + "\":\"" + lastData[1] + "\",";
}
val = val.substring(0, val.length() - 1) + "},";
}
val = val.substring(0, val.length() - 1) + "]";
}
dataString.add(key + "|" + val);
}
String sig = "";
Collections.sort(dataString);
for (String sub : dataString) {
String[] strArray = sub.split("\\|");
if (strArray.length > 1) {
sig = sig + strArray[0] + "=" + strArray[1].replace("\"", "") + "&";
} else {
sig = sig + strArray[0] + "=&";
}
}
if (!sig.equals("")) {
sig = sig.substring(0, sig.length() - 1);
}
dataString.add("Signature|" + md5(md5("h$^kl67(" + sig) + "h$^kl67("));
result = "{";
for (String sub2 : dataString) {
strArray = sub2.split("\\|");
if (strArray.length <= 1) {
result = result + "\"" + strArray[0] + "\":\"\",";
} else if (strArray[1].indexOf("{") <= -1 || strArray[1].indexOf("}") <= -1 || strArray[1].indexOf("[引号]") >= 0) {
result = result + "\"" + strArray[0] + "\":\"" + strArray[1] + "\",";
} else {
result = result + "\"" + strArray[0] + "\":" + strArray[1] + ",";
}
}
if (!result.equals("")) {
result = result.substring(0, result.length() - 1);
}
return result + "}";
}
整个Function 传附件了
|
最佳答案
查看完整内容
先解析JSON参数名和参数值。然后排序。再取MD5就是Signature的值。最后转为JSON数据。。。
比如传入参数:
{"Phone":"111111111^android_22222","Password":"333333"}
然后排序之后就是:
sig = “Password=333333&Phone=111111111^android_22222”
最后计算Signature
signature = md5(md5("h$^kl67(" + sig) + "h$^kl67(")
|