跳到主要内容

附录2

签名生成

httpsRequest.setReqStat("S");
httpsRequest.setErrDesc("");

String jsonStr = gson.toJson(httpsRequest);
String message = 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 = Base64.getEncoder().encodeToString(signature);