|
@@ -15,16 +15,6 @@ public class Encrypt {
|
|
|
|
|
|
public static final String SIGN = "sign";
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- Map<String,String> param = new HashMap<>();
|
|
|
- param.put("idChannel","1001");
|
|
|
- param.put("idDevice","10011");
|
|
|
-
|
|
|
- String ihOTiTyMLDNNLFuP = createSHA256Sign(param, "IhOTiTyMLDNNLFuP");
|
|
|
- System.out.println(ihOTiTyMLDNNLFuP);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
public static String createSHA256Sign(Map<String, String> paramMap, String signKey) {
|
|
|
|
|
|
List<String> sortedKeys = new ArrayList<String>();
|
|
@@ -63,66 +53,7 @@ public class Encrypt {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * MD5加密 生成32位md5码
|
|
|
- */
|
|
|
- public static String string2MD5(String inStr){
|
|
|
- MessageDigest md5 = null;
|
|
|
- try{
|
|
|
- md5 = MessageDigest.getInstance("MD5");
|
|
|
- }catch (Exception e){
|
|
|
- System.out.println(e.toString());
|
|
|
- e.printStackTrace();
|
|
|
- return "";
|
|
|
- }
|
|
|
- char[] charArray = inStr.toCharArray();
|
|
|
- byte[] byteArray = new byte[charArray.length];
|
|
|
-
|
|
|
- for (int i = 0; i < charArray.length; i++)
|
|
|
- byteArray[i] = (byte) charArray[i];
|
|
|
- byte[] md5Bytes = md5.digest(byteArray);
|
|
|
- StringBuffer hexValue = new StringBuffer();
|
|
|
- for (int i = 0; i < md5Bytes.length; i++){
|
|
|
- int val = ((int) md5Bytes[i]) & 0xff;
|
|
|
- if (val < 16)
|
|
|
- hexValue.append("0");
|
|
|
- hexValue.append(Integer.toHexString(val));
|
|
|
- }
|
|
|
- return hexValue.toString().toUpperCase();
|
|
|
- }
|
|
|
- /**
|
|
|
- * SHA1加密
|
|
|
- * @param str
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String string2Sha1(String str){
|
|
|
- if(str==null||str.length()==0){
|
|
|
- return null;
|
|
|
- }
|
|
|
- char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
|
|
|
- 'a','b','c','d','e','f'};
|
|
|
- try {
|
|
|
- MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
|
|
|
- mdTemp.update(str.getBytes("UTF-8"));
|
|
|
-
|
|
|
- byte[] md = mdTemp.digest();
|
|
|
- int j = md.length;
|
|
|
- char buf[] = new char[j*2];
|
|
|
- int k = 0;
|
|
|
- for (int i = 0; i < j; i++) {
|
|
|
- byte byte0 = md[i];
|
|
|
- buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
|
|
|
- buf[k++] = hexDigits[byte0 & 0xf];
|
|
|
- }
|
|
|
- return new String(buf);
|
|
|
- } catch (Exception e) {
|
|
|
- // TODO: handle exception
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 利用Apache的工具类实现SHA-256加密
|
|
|
- * 所需jar包下載 http://pan.baidu.com/s/1nuKxYGh
|
|
|
* @param str 加密后的报文
|
|
|
* @return
|
|
|
*/
|