回调验签

  • 报文一共有5个字段:signatureeventIdtimestampnoncecontent
  • plaintext = eventId + timestamp + nonce + content
  • content为最后的推送业务数据报文(JSON格式)
  • nonce为随机数
public boolean verifySignatureSHA256WithRSA(String plaintext, String signature, String publicKey) {
    return verifySignatureSHA256WithRSA(plaintext.getBytes(StandardCharsets.UTF_8), signature,publicKey);
}

public boolean verifySignatureSHA256WithRSA(byte[] plaintextByte, String inputSignature,String pubKey) {
    try {
        X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(Base64
                .getDecoder().decode(pubKey));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);
        byte[] signed = Base64.getDecoder().decode(inputSignature);
        Signature signature = Signature
                .getInstance("SHA256WithRSA");
        signature.initVerify(publicKey);
        signature.update(plaintextByte);
        return signature.verify(signed);
    } catch (Exception e) {

    }
}

results matching ""

    No results matching ""