1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
@RestResource(urlMapping='/CoreService') global with sharing class ApexRESTCycleExample { @HttpGet global static void doGet(){ // 微信加密签名 String signature= RestContext.request.params.get('msg_signature') ; // 时间戳 String timestamp= RestContext.request.params.get('timestamp'); // 随机数 String nonce= RestContext.request.params.get('nonce'); // 随机字符串 String echostr= RestContext.request.params.get('echostr'); Blob encrypted = EncodingUtil.base64Decode(echostr); Blob key = EncodingUtil.base64Decode('xxxxxxxxxxxxxxxxxxxxxxxxxxx'); Blob key_v = EncodingUtil.base64Decode('xxxxxxxxxxxxxxxxxxxxxxxxxx'); Blob decrypted=Crypto.decrypt('AES256', key,key_v, encrypted); system.debug('decrypted'+decrypted.tostring()); String hex = EncodingUtil.convertToHex(decrypted);//转换成16进制 system.debug('$hex'+hex); String content = hex.substring(32); String msg_len = content.substring(0, 8); Integer msg_len_val = getMsgLen(msg_len); String msg = content.substring(8, msg_len_val*2 + 8); Blob msgblob = EncodingUtil.convertFromHex(msg); system.debug('$hex'+msgblob.toString()); RestContext.response.addHeader('Content-Type', 'text/plain'); RestContext.response.responseBody =msgblob; RestContext.response.statusCode = 200; RestContext.response.addHeader('Content-Type', 'text/plain'); RestContext.response.responseBody =null; RestContext.response.statusCode = 200; } public static Integer getMsgLen(String inputStr) { Map<String, Double> calcMap = new Map<String, Double>(); calcMap.put('0', 0); calcMap.put('1', 1); calcMap.put('2', 2); calcMap.put('3', 3); calcMap.put('4', 4); calcMap.put('5', 5); calcMap.put('6', 6); calcMap.put('7', 7); calcMap.put('8', 8); calcMap.put('9', 9); calcMap.put('a', 10); calcMap.put('b', 11); calcMap.put('c', 12); calcMap.put('d', 13); calcMap.put('e', 14); calcMap.put('f', 15); Double result = 0; for (Integer i = 0; i < inputStr.length(); i ++) { String charStr = inputStr.substring(i, i + 1).toLowerCase(); result += (Math.pow(16, inputStr.length() - i - 1) * calcMap.get(charStr)); } return result.intValue(); } } |