跳到主要内容

附录1

签名生成

HTTP

String mti = "001X";
String jweString = "eyJlcGsiOnsia3R5Ijo...";

String message = mti + jweString;

String path = KEYPAIR_DIR + "ec_private_key_521.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("EC");
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

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

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

预授权付款

String sourceSystem = "mxx";
String jsonStr = "{"
+ "\"MsgVer\":\"1.0\","
+ "\"PmtType\":\"01\","
+ "\"CallerDeviceType\":\"\","
+ "\"Email\":\"\","
+ "\"DeviceSN\":\"PPXXX722XXX05XXX\","
+ "\"CallerDeviceVer\":\"\","
+ "\"TxnID\":\"20230615155030POS\","
+ "\"LocalTxnDTTime\":\"\","
+ "\"AmtTxn\":\"000000000100\","
+ "\"CrcyTxn\":\"458\","
+ "\"Description\":\"Description\","
+ "\"OptInPrintReceipt\":\"OptInPrintReceipt\","
+ "\"OptInSendReceipt\":\"OptInSendEReceipt\","
+ "\"sourceSystem\":\"mxx\","
+ "\"sequenceNo\":\"1\""
+ "}";
String message = sourceSystem + "\n" + jsonStr;

String path = KEYPAIR_DIR + "ec_private_key_521.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));