1.微信支付文檔 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_16&index=9php
解密步驟以下: html
(1)對加密串A作base64解碼,獲得加密串Bjava
(2)對商戶key作md5,獲得32位小寫key* ( key設置路徑:微信商戶平臺(pay.weixin.qq.com)-->帳戶設置-->API安全-->密鑰設置 )git
(3)用key*對加密串B作AES-256-ECB解密(PKCS7Padding)web
2.算法
解決方法:api
http://stackoverflow.com/questions/6481627/java-security-illegal-key-size-or-default-parameters數組
JDK8 jar包下載地址:安全
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html微信
JDK7 jar包下載地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
JDK6 jar包下載地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
把裏面的兩個jar包:local_policy.jar 和 US_export_policy.jar 替換掉原來jdk安裝目錄C:\Program Files\Java\jre6\lib\security 下的兩個jar包接能夠了
3.demo示例
package encode; import java.security.MessageDigest; public class MD5 { private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; /** * 轉換字節數組爲16進制字串 * @param b 字節數組 * @return 16進制字串 */ public static String byteArrayToHexString(byte[] b) { StringBuilder resultSb = new StringBuilder(); for (byte aB : b) { resultSb.append(byteToHexString(aB)); } return resultSb.toString(); } /** * 轉換byte到16進制 * @param b 要轉換的byte * @return 16進制格式 */ private static String byteToHexString(byte b) { int n = b; if (n < 0) { n = 256 + n; } int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } /** * MD5編碼 * @param origin 原始字符串 * @return 通過MD5加密以後的結果 */ public static String MD5Encode(String origin) { String resultString = null; try { resultString = origin; MessageDigest md = MessageDigest.getInstance("MD5"); resultString = byteArrayToHexString(md.digest(resultString.getBytes())); } catch (Exception e) { e.printStackTrace(); } return resultString; } public static void main(String[] args) { System.out.println("111"+MD5Encode("111")); } }
/** * */ package encode; import java.util.Base64; /** * @author jing.huang * @function jdk8支持 * @date 2018年1月10日 * @version */ public class Base64Util { public static byte[] decode(String encodedText){ final Base64.Decoder decoder = Base64.getDecoder(); return decoder.decode(encodedText); } public static String encode(byte[] data){ final Base64.Encoder encoder = Base64.getEncoder(); return encoder.encodeToString(data); } /** * @param args */ public static void main(String[] args) { try { final Base64.Decoder decoder = Base64.getDecoder(); final Base64.Encoder encoder = Base64.getEncoder(); final String text = "字串文字"; final byte[] textByte = text.getBytes("UTF-8"); //編碼 final String encodedText = encoder.encodeToString(textByte); System.out.println(encodedText); //解碼 System.out.println(new String(decoder.decode(encodedText), "UTF-8")); } catch (Exception e) { e.printStackTrace(); } // final Base64.Decoder decoder = Base64.getDecoder(); // final Base64.Encoder encoder = Base64.getEncoder(); // final String text = "字串文字"; // final byte[] textByte = text.getBytes("UTF-8"); // //編碼 // final String encodedText = encoder.encodeToString(textByte); // System.out.println(encodedText); // //解碼 // System.out.println(new String(decoder.decode(encodedText), "UTF-8")); } }
/** * */ package encode; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** * @author jing.huang * @function * @date 2018年1月10日 * @version */ public class AESUtil { /** * 密鑰算法 */ private static final String ALGORITHM = "AES"; /** * 加解密算法/工做模式/填充方式 */ private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS5Padding"; /** * 生成key */ private static SecretKeySpec key = new SecretKeySpec(MD5.MD5Encode("2IBtBXdrqC3kCBs4gaceL7nl2nnFadQv").toLowerCase().getBytes(), ALGORITHM); /** * AES加密 * * @param data * @return * @throws Exception */ public static String encryptData(String data) throws Exception { // 建立密碼器 Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING); // 初始化 cipher.init(Cipher.ENCRYPT_MODE, key); return Base64Util.encode(cipher.doFinal(data.getBytes())); } /** * AES解密 * * @param base64Data * @return * @throws Exception */ public static String decryptData(String base64Data) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING); cipher.init(Cipher.DECRYPT_MODE, key); return new String(cipher.doFinal(Base64Util.decode(base64Data))); } public static void main(String[] args) throws Exception { //解密 // String req_info="Ih5osM/5IbPfHouVrUmwebd1yAW2Gys91jv006W1237sSi3z022KxHafLIDMrQLYiBttTadgvy2cbx6DnmwDIQ52lPWfo6pAAHt7Q9DjBIpDRQ7JsbEBlomoQP2ZkdNHnWscVYuFEVlItaSlkSlcKLdB4UwMduqDYseFsUUthz6htPeBu987zXS6dKrgIbRwOxt5RfPmk1sf0oVB2yU3UH0Ly8SzBjmN1jrh4qAaUkfH6VkeMJcsZSGchQn2VresxJTbGH++JE1UsXUF3gyYpweyxBPtHoKdaggsIONR20UKNxJYPJLnEOnfQF/Ipmk8/QmTVRK7iqfVLC9EA1Auma0AlKBjZlYqynUlF3y+E2ZzgWMUlvDHZVWDbzp/TcE0q+Ukc7yQ3HBsibDR474SPlLTkCWz1iydXzkVcLqJKamsh76Liv1a0hzu0sI3qasMAfmwU6/q7/N6quq031toO1GxqkVaxBRK7e64gSOx9ArxxVFgZ7WN+JPq2OH/pTKH8ToxHA0rtxN5+aAgZGkXiIOUiHtp4mjpRxqe34WK7C7Nr0DQyOVwsXT2TTegSgWGm34aa//ZYxHedubv2iX+E7K222lptg9IqHlMXBbwKFtKtIcal61+8ciz+sB1FBpqHchC+3whTqWv5ZANiHBzaOhbIbA/mKX2XZ6Cy0iYh+bL/8Y/Hvz/UnMGzor+2anIUeBAGRQmseL4jY+Qic46WLuEhDcarCaO4JgJSAOC+VmsdrER9TRum26PFwTQwtNpxkrKCiO9Gv36Ood5D8hXnLHUH+4nbsek8ouxkCcFXq4Us0mipB3i5ksQpt23LiJm9Ahxyvptp9Q41SytS48NXiz3IxTOqDdknowedZwAtJ/fhBlwiOHD9N+pECXuNBKLaCZcatGycr0/DPELiCF+MIRQ6V60wzaZD74TKRFULd1ljNsoQIAbuGaT40WMDY6a28jBHQ/IXnD4gvSvfeumwQzp3Q9PiPyFtF6JxH7RBRj9/lmQuQozJIPZCaCNVTBfWQOdcFaBnP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="; String B = AESUtil.decryptData(req_info); System.out.println(B); //加密 String str = "<root>"+ "<out_refund_no><![CDATA[2531340110812300]]></out_refund_no>"+ "<out_trade_no><![CDATA[2531340110812100]]></out_trade_no>"+ "<refund_account><![CDATA[REFUND_SOURCE_RECHARGE_FUNDS]]></refund_account>"+ "<refund_fee><![CDATA[1]]></refund_fee>"+ "<refund_id><![CDATA[50000505542018011003064518841]]></refund_id>"+ "<refund_recv_accout><![CDATA[支付用戶零錢]]></refund_recv_accout>"+ "<refund_request_source><![CDATA[API]]></refund_request_source>"+ "<refund_status><![CDATA[SUCCESS]]></refund_status>"+ "<settlement_refund_fee><![CDATA[1]]></settlement_refund_fee>"+ "<settlement_total_fee><![CDATA[1]]></settlement_total_fee>"+ "<success_time><![CDATA[2018-01-10 10:31:24]]></success_time>"+ "<total_fee><![CDATA[1]]></total_fee>"+ "<transaction_id><![CDATA[4200000052201801101409025381]]></transaction_id>"+ "</root>"; System.out.println(encryptData(str)); } }