RSA签名报错-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

RSA签名报错

游客eqgjgegwkey5u 2020-10-20 18:01:23 116

这是签名的方法


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


这个要怎么改的

rsa报错 rsa签名 签名报错 rsa签名报错
分享到
取消 提交回答
全部回答(1)
  • 游客2q7uranxketok
    2021-02-09 16:29:20

    下面是例子:

    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=
    0 0
云计算
使用钉钉扫一扫加入圈子
+ 订阅

时时分享云计算技术内容,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

最新问题