计算证书的签名

public static void main(String[] args) {
        try {
            // 读取证书文件
            String str =  "";
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                    Base64.getDecoder().decode(str)
            );
            // 创建 CertificateFactory 对象
            CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

            // 反序列化证书
            X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(byteArrayInputStream);

            // 创建 MessageDigest 对象,选择摘要算法,例如 SHA-256
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] documentHash = certificate.getTBSCertificate();
            byte [] digests = digest.digest(documentHash);
            BigInteger bigInteger = new BigInteger(convert(byteArrayToHexString(digests)), 16);
            System.out.println(bigInteger);

            byteArrayInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String convert(String r_hex) {
        String pad_hex = "00" + "01";

        int l = (4096 / 8 - 3 - ("3031300d060960864801650304020105000420" + r_hex).length() / 2);
        for (int i = 0; i < l; i++) {
            pad_hex = pad_hex + "FF";
        }

        pad_hex = pad_hex + "00" + "3031300d060960864801650304020105000420" + r_hex;
        return pad_hex;
    }

    public static String byteArrayToHexString(byte[] byteArray) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : byteArray) {
            hexString.append(String.format("%02x", b));
        }
        return hexString.toString();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值