跳到主要内容

附录 1

数字签名生成

httpsRequest.setMsgVer("1.0");
httpsRequest.setPmtType("01");
httpsRequest.setCallerDeviceType("");
httpsRequest.setEmail("");
httpsRequest.setDeviceSN("PPXXX722XXX05XXX");
httpsRequest.setCallerDeviceVer("");
httpsRequest.setTxnID("20230615155030POS");
httpsRequest.setLocalTxnDTTime("");
httpsRequest.setAmtTxn("000000000100");
httpsRequest.setCrcyTxn("458");
httpsRequest.setDescription("Description");
httpsRequest.setOptInPrintReceipt("OptInPrintReceipt");
httpsRequest.setOptInSendReceipt("OptInSendEReceipt");
httpsRequest.setSourceSystem("mxx");
httpsRequest.setSequenceNo("1");

String jsonStr = gson.toJson(httpsRequest);
String message = httpsRequest.getSourceSystem() + "\n" + jsonStr;

String path = KEYPAIR_DIR + "privatekey.pem";

FileReader fileReader = new FileReader(path);
PEMParser pemParser = new PEMParser(fileReader);

PEMKeyPair pemKeyPair = (PEMKeyPair) pemParser.readObject();
byte[] encodedPrivateKey = pemKeyPair.getPrivateKeyInfo().getEncoded();

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

Signature privateSignature = Signature.getInstance("SHA256withRSA");
privateSignature.initSign(privateKey);
privateSignature.update(message.getBytes(StandardCharsets.UTF_8));

byte[] signature = privateSignature.sign();
String signatureStr = URLEncoder.encode(Base64.getEncoder().encodeToString(signature));