Java RSA (SHA1withRSA)签名和验签

Posted FrankYou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java RSA (SHA1withRSA)签名和验签相关的知识,希望对你有一定的参考价值。

static {
    try {
        SIGNATURE = Signature.getInstance("SHA1withRSA", "BC");
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException e) {
        throw Throwables.propagate(e);
    }
}

public static String getSignature(Map<String, String> map, String pfxPath, String pfxPasswd) {
    try {
        SIGNATURE.initSign(X509Certs.getPrivateKey(pfxPath, pfxPasswd).getPrivateKey());
        SIGNATURE.update(DigestUtils.sha1Hex(buildReqParams(map, false)).getBytes(Charsets.UTF_8));
        return Base64.encodeBase64String(SIGNATURE.sign()).replaceAll("\r|\n", "");
    } catch (Throwable e) {
        throw Throwables.propagate(e);
    }
}

public static void verifySign(Map<String, String> map, String certPath) throws GatewayFrontException {
    try {
        byte[] sign = Base64.decodeBase64(map.get("signature").getBytes(Charsets.UTF_8));
        map.remove("signature");
        SIGNATURE.initVerify(X509Certs.getVerifyPublicKey(certPath));
        SIGNATURE.update(DigestUtils.sha1Hex(ExPayUtil.buildReqParams(map, false)).getBytes(Charsets.UTF_8));
        Preconditions.checkState(SIGNATURE.verify(sign), ExPayConstant.VERIFY_SIGNATURE_FAILUE);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

 

以上是关于Java RSA (SHA1withRSA)签名和验签的主要内容,如果未能解决你的问题,请参考以下文章

iOS小技能:RSA签名算法和加密算法的实现

iOS小技能:RSA签名算法和加密算法的实现

RSA 数字签名算法(Java版)

支付宝签名

iOS小技能:RSA签名验签加密解密的原理

Android 签名 SHA1WithRSA SHA256WithRSA MD5WithRSA