渝快码网关返回信息:

{"errcode":"AGW.1433","errmsg":"请求签名错误或请求服务器时间戳误差大于 180 秒"}

接口调用测试流程

保证渝快码生成到发送的时间间隔少于180s

完整测试代码 java

import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.UUID;
import java.security.MessageDigest;
/**
 * Description: Sm4Demo
 *
 * @author txl
 * @date 2023/3/3 11:15
 */
public class Main {

    //秘钥
    private  static String KEY = "64EC7C763AB7BF64E2D75FF83A319918";
    public static void main(String[] args) {
          // 从个人的渝快码扫码获取该字符串
          String s = "ZWM3500000900100131793474755804024834010016000000980001500000010266909EBD0gV11YH3SttnuvhXxBdjM4ij5crqSrSWr9lOVI2RYtPmZ87gTE8Ru9dc5qtrG+SY47qmB1F1Si+NxqJ5d7yRNQ==";
          send(s);
    }

    private static String docode(String encodeStr){
        SM4 sm4 = SmUtil.sm4(hexToByte(KEY));
        byte[] decrypt = sm4.decrypt(encodeStr);
        //返回解密后明文
        return new String(decrypt);
    }
    private static byte[] hexToByte(String hexStr) {
        if (hexStr.length() < 1) {
            return null;
        } else {
            byte[] result = new byte[hexStr.length() / 2];
            for(int i = 0; i < hexStr.length() / 2; ++i) {
                int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
                int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
                result[i] = (byte)(high * 16 + low);
            }
            return result;
        }
    }

   private  static void send(String yukuaima){
         String passtoken = "yukuaima";
        try {
            URL url = new URL("https://riotest.cqdcg.com/ebus/yukuaima/idcode/api/decode");
            HttpURLConnection conn =(HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json; utf-8");
            conn.setRequestProperty("Accept", "application/json");
            Instant timestamp = Instant.now();
            //获取以毫秒为单位的时间戳
            long millis = timestamp.toEpochMilli();
//            System.out.println("当前时间戳(毫秒为单位): " + millis);
            conn.setRequestProperty("x-tif-timestamp", String.valueOf(millis));
            conn.setRequestProperty("x-tif-paasid", passtoken);
            // 生成随机 UUID
            UUID uuid = UUID.randomUUID();
            conn.setRequestProperty("x-tif-nonce", uuid.toString());
            StringBuilder sb = new StringBuilder();
            sb.append(millis).append(passtoken).append(uuid.toString()).append(millis);
            String orgStr = sb.toString();
            System.out.println("生成摘要前:"+ orgStr);
            String s256str = sha256(orgStr);
            System.out.println("生成摘要后:"+ s256str);
            conn.setRequestProperty("x-tif-signature", s256str);
            // For POST only - START
            conn.setDoOutput(true);

            String jsonInputString = "{\"acceptOrgId\":\"1\",\"codeContent\":\"" + yukuaima +"\",\"useCodeInfo\":\"政务办事\"}";
            OutputStream os = null;
            try{
                os = conn.getOutputStream();
                byte[] input = jsonInputString.getBytes("utf-8");
                os.write(input, 0, input.length);
                os.flush();
            }catch(Exception ex){
              throw new Exception(ex);
            }finally {
                os.close();
            }

            // 检查响应
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println("Request succeeded");
                BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();

                // print result
                System.out.println("响应结果:"+response.toString());
                // 处理响应
            } else {
                System.out.println("POST request failed with error code: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static String sha256(String originalString){
        MessageDigest digest = null;
        try {
            digest = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        byte[] encodedhash = digest.digest(
                originalString.getBytes(StandardCharsets.UTF_8));
        String sha256hex = bytesToHex(encodedhash);
        return sha256hex;
    }
    private static String bytesToHex(byte[] hash) {
        StringBuilder hexString = new StringBuilder(2 * hash.length);
        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b);
            if(hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }

}