首页 > 代码库 > java-参考支付宝加密方式
java-参考支付宝加密方式
package com.util; import java.security.MessageDigest; import java.util.Map; import java.util.TreeMap; import com.config.ConstantConfiguer; import com.exp.RunException; public class EncryptUtils { public static String getSign(Map<String, String> map) { String sign = ""; if (Helper.isNull(map)) { throw new RunException("encrypt data is empty"); } TreeMap<String, String> treeMap = new TreeMap<String, String>(); treeMap.putAll(map); treeMap.put("privateKey", "Key" ); StringBuffer buffer = new StringBuffer(); for (Map.Entry<String, String> entry : treeMap.entrySet()) { buffer.append(entry.getKey()).append("=").append(entry.getValue()) .append("&"); } int length = buffer.length(); buffer.delete(length - 1, length); sign = SHA1.sha(buffer.toString()).toLowerCase(); return sign; } /** * 加密字符串 */ public static String getSign(String txt) { return getSign(txt, "HelloWord" ); } public static String getSign(String txt, String key) { if (Helper.isNull(txt)) { throw new RunException("encrypt data is empty"); } String sign = null; txt = txt + "-" + key; sign = SHA1.sha(txt).toLowerCase(); return sign; } } class SHA1 { public static void main(String[] args) { System.out.println(sha("")); } public static String md5(String inputText) { return encrypt(inputText, "md5"); } public static String sha(String inputText) { return encrypt(inputText, "sha-1"); } private static String encrypt(String inputText, String algorithmName) { inputText = Helper.checkNull(inputText); if (algorithmName == null || "".equals(algorithmName.trim())) { algorithmName = "md5"; } String encryptText = null; try { MessageDigest m = MessageDigest.getInstance(algorithmName); m.update(inputText.getBytes("UTF8")); byte s[] = m.digest(); return hex(s); } catch (Exception e) { e.printStackTrace(); } return encryptText; } private static String hex(byte[] arr) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < arr.length; ++i) { sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3)); } return sb.toString(); } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。