如果我想从密钥库加载来自特定提供商的证书,如何获得相同的证书?

时间:2012-05-19 09:24:12

标签: java security certificate digital-signature pki

我有一个.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();

将此证书发送到服务器端

0 个答案:

没有答案