开发者社区> 问答> 正文

RSA签名报错

这是签名的方法


public static String Sign2(String data, String BEISEN_PRIKEY) throws Exception {
        // 密钥格式为PKCS1 需要转换为PKCS8才可以使用
        byte[] encodeByte = Base64.decodeBase64(BEISEN_PRIKEY);
        AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.pkcs8ShroudedKeyBag);    //PKCSObjectIdentifiers.pkcs8ShroudedKeyBag
        ASN1Object asn1Object = ASN1Object.fromByteArray(encodeByte);
        PrivateKeyInfo privKeyInfo = new PrivateKeyInfo(algorithmIdentifier, asn1Object);
        byte[] pkcs8Bytes = privKeyInfo.getEncoded();

        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pkcs8Bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA","BC");
        PrivateKey key = keyFactory.generatePrivate(keySpec);
        Signature signature = Signature.getInstance(SIGN_ALGORITHMS);
        signature.initSign(key);
        signature.update(data.getBytes(encoding));
        return new String(Base64.encodeBase64(signature.sign()));
    }





然后控制台报了这个错


java.security.spec.InvalidKeySpecException: java.lang.ClassCastException: org.bouncycastle.asn1.DERSequence cannot be cast to org.bouncycastle.asn1.DERInteger


这个要怎么改的

展开
收起
游客eqgjgegwkey5u 2020-10-20 18:01:23 906 0
1 条回答
写回答
取消 提交回答
  • 下面是例子:

    uJikVPTRj2m7lg3SBFxQ3HEtap8y3MzKtUW4vhLelnvhhrBdUVusNYog9cIPODMhVRtyUev7Kqqr7E5z0zn8vjhfDLSkT0VYli3ZmDRr/JZZ343TVaqObxG6ZEZHVXzYBZ+pb0aHzR39nk002LGtFM/Qet4vOACqaASAHwvZjv8= AQAB

    6IgQk8tQY1/AmeYJQT4vIuzVrfAcI8PR2ELfMD05bBrqZ6PjQWdp/gIlYsLq/VL9loxREDd/jY/ThprDymmraQ==

    yzoSAq7rUlNxr/Pmr8/f9VpaiBNCXcjdmmFut7Rv2v4HYMNOAXOQG1NQCdi4f2VcriZ2a9CbcvlvL/t7nAiiJw== nd9bWeyMzJtC+XnryKb+e7jOLoS5pjKZ8zkbrPW0WchAY1MqxMoAdVthpJbv0kQZNbBYjSkQ6by/gUDsMy3hGQ== rqU41RV9JUKQcKUpme0Kic8b/dSPNAlqZ0FT5IsrlQa5UG6Gmri1HsZ6Afw8kT6KZx6mpgCfli7FXna/w1eoAQ== 1MAM8T2f/Xjfbz/ZVK6p/smx5RUmjBxdoFkGVMjEc0iY3UwmEnwOokRiLLDWgXlmqHS08Cb02BRnTQhK5sNLbw== f+TRxkKwhmo9G2OL5JWJOJH5Y2vVM4F32EvsCVTNfvlpcYXzwZCAKb//wgWnwco7B9oyRGv44sDIyLo6KVh9sKc4iERJ5q+ZaTHoSU4TX53/mBiA778rhPXVub22IdBDCJ4VOR4WuUT6tQeXXnPIMw57j3Mroj4auNvYc6kHdyE=
    2021-02-09 16:29:20
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多