Configuration __KDXZvc8mnEmDqMARhK7G __KDXZvc8mnEmDqMARhK7G >> 开始SM2签名"); var privateKeyHex = "4255978e4c55a025fe7bd394f5cac4d786be16542c53b4d2c24ddca350e61ed6"; println(">>> 私钥长度: " + privateKeyHex.length); var Security = Packages.java.security.Security; var BouncyCastleProvider = Packages.org.bouncycastle.jce.provider.BouncyCastleProvider; Security.addProvider(new BouncyCastleProvider()); println(">>> BC Provider已添加"); var Hex = Packages.org.bouncycastle.util.encoders.Hex; println(">>> 准备解析私钥"); var ECNamedCurveTable = Packages.org.bouncycastle.asn1.gm.GMNamedCurves; var sm2Spec = ECNamedCurveTable.getByName("sm2p256v1"); var ecParams = new Packages.org.bouncycastle.jce.spec.ECNamedCurveSpec( "sm2p256v1", sm2Spec.getCurve(), sm2Spec.getG(), sm2Spec.getN() ); println(">>> EC参数已创建"); var BigInteger = Packages.java.math.BigInteger; println(">>> 准备创建BigInteger, privateKeyHex类型: " + typeof(privateKeyHex)); var privateKeyInt = new BigInteger(privateKeyHex, 16); println(">>> BigInteger已创建"); var privKeySpec = new Packages.java.security.spec.ECPrivateKeySpec(privateKeyInt, ecParams); println(">>> 私钥Spec已创建"); var KeyFactory = Packages.java.security.KeyFactory; var keyFactory = KeyFactory.getInstance("EC", "BC"); var privateKey = keyFactory.generatePrivate(privKeySpec); println(">>> 私钥已生成"); var Signature = Packages.java.security.Signature; var signature = Signature.getInstance("SM3withSM2", "BC"); signature.initSign(privateKey); println(">>> 签名器已初始化"); var contentBytes = new Packages.java.lang.String(content).getBytes("UTF-8"); signature.update(contentBytes, 0, contentBytes.length); var signBytes = signature.sign(); println(">>> 签名完成(DER格式)"); // 将DER格式转换为RS格式(r+s拼接) var derHex = Hex.toHexString(signBytes); println(">>> DER签名: " + derHex); // 解析DER格式: 30 [len] 02 [r-len] [r] 02 [s-len] [s] var offset = 0; if (signBytes[offset++] != 0x30) throw "Invalid DER format"; var totalLen = signBytes[offset++] & 0xff; if (signBytes[offset++] != 0x02) throw "Invalid DER format"; var rLen = signBytes[offset++] & 0xff; var rBytes = new Packages.java.lang.reflect.Array.newInstance(Packages.java.lang.Byte.TYPE, rLen); Packages.java.lang.System.arraycopy(signBytes, offset, rBytes, 0, rLen); offset += rLen; if (signBytes[offset++] != 0x02) throw "Invalid DER format"; var sLen = signBytes[offset++] & 0xff; var sBytes = new Packages.java.lang.reflect.Array.newInstance(Packages.java.lang.Byte.TYPE, sLen); Packages.java.lang.System.arraycopy(signBytes, offset, sBytes, 0, sLen); // 转换为hex并确保长度为64 var rHex = Hex.toHexString(rBytes); var sHex = Hex.toHexString(sBytes); // 去除前导0,保留最后64位 while (rHex.length > 64) rHex = rHex.substring(2); while (sHex.length > 64) sHex = sHex.substring(2); // 补齐到64位 while (rHex.length < 64) rHex = "0" + rHex; while (sHex.length < 64) sHex = "0" + sHex; var rsSign = rHex + sHex; println(">>> RS签名: " + rsSign); return rsSign; } catch(e) { println("SM2签名异常: " + e); if (e.javaException) { println("Java异常: " + e.javaException); e.javaException.printStackTrace(); } return null; } } // 配置对象 var AppraiseConfig = { appMark: "BAFW", appWord: "1wFbhXkuBAFW", apiUrl: "http://43.254.24.244" }; // MD5加密函数 function md5Hex(src) { try { var md5 = Packages.java.security.MessageDigest.getInstance("MD5"); var bytes = md5.digest(src.getBytes("UTF-8")); var hexString = new Packages.java.lang.StringBuilder(); for (var i = 0; i < bytes.length; i++) { var hex = Packages.java.lang.Integer.toHexString(0xff & bytes[i]); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch(e) { println("MD5加密异常: " + e); return null; } } // 生成token function generateToken(msg) { try { var md5Result = md5Hex(msg); var token = getSm2Encode(md5Result); return token; } catch(e) { println("生成Token异常: " + e); return null; } } // 上报办件服务数据到北京政务服务网 - 调用HTTP接口 function uploadServiceData(serviceData) { try { println("========== 开始调用上报接口 =========="); var url = "https://v5qy.te.baibaodun.com.cn/sm2/uploadServiceData"; // 构造表单参数 var params = new Packages.java.util.HashMap(); params.put("userId", serviceData.userId || ""); params.put("taskCode", serviceData.taskCode || ""); params.put("taskName", serviceData.taskName || ""); params.put("projectNo", serviceData.projectNo || ""); params.put("orgcode", serviceData.orgcode || ""); params.put("orgName", serviceData.orgName || ""); params.put("userName", serviceData.userName || ""); params.put("certKey", serviceData.certKey || ""); params.put("telPhone", serviceData.telPhone || ""); params.put("taskguid", serviceData.taskguid || ""); params.put("userProp", serviceData.userProp || "1"); params.put("userPageType", serviceData.userPageType || "111"); params.put("serviceName", serviceData.serviceName || "申报"); params.put("isService", serviceData.isService || "1"); params.put("proStatus", serviceData.proStatus || "1"); params.put("bj_zt", serviceData.bj_zt || "1"); params.put("projectType", String(serviceData.projectType || 2)); params.put("dataSource", serviceData.dataSource || "111"); params.put("administrative_div", serviceData.administrative_div || "00"); params.put("applydate", serviceData.applydate || ""); params.put("serviceTime", serviceData.serviceTime || ""); params.put("serviceDate", serviceData.serviceDate || ""); params.put("source", serviceData.source || "BAFW"); params.put("sourceTJ", serviceData.sourceTJ || "BAFW"); println("请求参数: userId=" + params.get("userId") + ", projectNo=" + params.get("projectNo") + ", userName=" + params.get("userName")); // 使用表单方式发送请求 var response = Packages.cn.hutool.http.HttpUtil.post(url, params, 30000); println("响应结果: " + response); var result = Packages.com.alibaba.fastjson.JSON.parseObject(response); var code = result.getString("code"); var success = result.getBoolean("success"); // 判断成功:code为"200" 或 success为true if ("200".equals(code) || (success != null && success.booleanValue())) { println("========== 上报成功 =========="); return { success: true, serviceCode: result.getString("serviceCode"), serviceNumber: result.getInteger("serviceNumber"), message: "上报成功" }; } else { println("========== 上报失败 =========="); return { success: false, message: result.getString("message"), code: result.getString("code") }; } } catch(e) { println("调用上报接口异常: " + e); if (e.javaException) { e.javaException.printStackTrace(); } return {success: false, message: "调用上报接口异常: " + e}; } }]]> 14