1

我有一个 .p12 文件,我用指定的提供程序将它加载到密钥库,现在我得到一个编码证书并转换为 base64 编码,之后我通过网络将它传递到服务器端,在服务器端我在 base64 中得到它格式,解码为正常,并解析为证书类型,但是,当我使用它来检查签名时,它会给出异常

javaenter code here.lang.ClassCastException: "myprovider".JCEECPublicKey cannot be cast to java.security.interfaces.RSAPublicKey

引发此异常的代码:

Certificate cert = null;
            try {
                cert = CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(cert_array));
            } catch (Exception e) {
                e.printStackTrace();
            }

if (cert != null) {

                boolean ret = false;
                try {
                    //load with specific crypto provider
                    Signature sgn = Signature.getInstance("ECGOST34310", "GAMMA");
                    sgn.initVerify(cert.getPublicKey());
                    sgn.update(textBlob);
                    ret = sgn.verify(sign);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                //signature check
                if (ret) {
                    System.out.println("verify sign OK");
                } else {
                    System.out.println("verify sign FALSE");
                }
            }

客户端代码,生成证书:

KeyStore store = loadKeyStorep12(fileName, pass);
            Enumeration en = store.aliases();
            while (en.hasMoreElements()) {
                StoreObjectParam profParam = (StoreObjectParam) en.nextElement();
                System.out.println(profParam);
                aliasKey = profParam.getSn();
            }
            PrivateKey prvKey = (PrivateKey) store.getKey(aliasKey, pass.toCharArray());
            if (prvKey != null) {
                byte[] textBlob = loadDocument(filePath);
                sign = SetSign(textBlob, prvKey);
                Certificate cert = store.getCertificate(aliasKey);
                certificate = cert.getEncoded();

将此证书发送到服务器端后

4

0 回答 0