java.io.IOException : Wrong version of key store. 使用 SpongyCaSTLe

标签 java android bouncycastle spongycastle

我正在使用 SpongycaSTLe 并希望以编程方式创建自签名证书。当我在 Android 中运行下面的代码时,我得到了 java.io.IOException: Wrong version of key store.

我不确定我做错了什么,我认为 SpongycaSTLe 是正确的版本?

无论如何这里是代码:

import org.spongycastle.jce.X509Principal;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.x509.X509V3CertificateGenerator;

import javax.net.ssl.*;
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.Date;

public class HttpsHello {
    private static String domainName = "localhost";
    static { Security.addProvider(new BouncyCastleProvider());  }

    public static void test() {

        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024);
            KeyPair KPair = keyPairGenerator.generateKeyPair();

            X509V3CertificateGenerator v3CertGen = new X509V3CertificateGenerator();

             int ran = new SecureRandom().nextInt();
            if (ran < 0) ran = ran *-1;

            BigInteger serialNumber = BigInteger.valueOf(ran);

            v3CertGen.setSerialNumber(serialNumber);
            v3CertGen.setIssuerDN(new X509Principal("CN=" + domainName + ", OU=None, O=None L=None, C=None"));
            v3CertGen.setNotBefore(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30));
            v3CertGen.setNotAfter(new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365*10)));
            v3CertGen.setSubjectDN(new X509Principal("CN=" + domainName + ", OU=None, O=None L=None, C=None"));


            v3CertGen.setPublicKey(KPair.getPublic());
            v3CertGen.setSignatureAlgorithm("MD5WithRSAEncryption");

            X509Certificate pkcert = v3CertGen.generateX509Certificate(KPair.getPrivate());
        //    FileOutputStream fos = new FileOutputStream("/path/to/testCert.cert");
          //  fos.write(pkcert.getEncoded());
           // fos.close();

            ByteArrayInputStream cert = new ByteArrayInputStream(pkcert.getEncoded());

            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());

            ks.load(cert,null);
            KeyManagerFactory kmf =
                    KeyManagerFactory.getInstance("SunX509");
            kmf.init(ks, null);
            SSLContext sc = SSLContext.getInstance("TLS");
            sc.init(kmf.getKeyManagers(), null, null);
            SSLServerSocketFactory ssf = sc.getServerSocketFactory();
            SSLServerSocket s
                    = (SSLServerSocket) ssf.createServerSocket(8888);
            System.out.println("Server started:");
            printServerSocketInfo(s);
            // Listening to the port
            SSLSocket c = (SSLSocket) s.accept();
            printSocketInfo(c);
            BufferedWriter w = new BufferedWriter(
                    new OutputStreamWriter(c.getOutputStream()));
            BufferedReader r = new BufferedReader(
                    new InputStreamReader(c.getInputStream()));
            String m = r.readLine();
            w.write("HTTP/1.0 200 OK");
            w.newLine();
            w.write("Content-Type: text/html");
            w.newLine();
            w.newLine();
            w.write("<html><body>Hello world!</body></html>");
            w.newLine();
            w.flush();
            w.close();
            r.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static void printSocketInfo(SSLSocket s) {
        System.out.println("Socket class: "+s.getClass());
        System.out.println("   Remote address = "
                +s.getInetAddress().toString());
        System.out.println("   Remote port = "+s.getPort());
        System.out.println("   Local socket address = "
                +s.getLocalSocketAddress().toString());
        System.out.println("   Local address = "
                +s.getLocalAddress().toString());
        System.out.println("   Local port = "+s.getLocalPort());
        System.out.println("   Need client authentication = "
                +s.getNeedClientAuth());
        SSLSession ss = s.getSession();
        System.out.println("   Cipher suite = "+ss.getCipherSuite());
        System.out.println("   Protocol = "+ss.getProtocol());
    }
    private static void printServerSocketInfo(SSLServerSocket s) {
        System.out.println("Server socket class: "+s.getClass());
        System.out.println("   Socker address = "
                +s.getInetAddress().toString());
        System.out.println("   Socker port = "
                +s.getLocalPort());
        System.out.println("   Need client authentication = "
                +s.getNeedClientAuth());
        System.out.println("   Want client authentication = "
                +s.getWantClientAuth());
        System.out.println("   Use client mode = "
                +s.getUseClientMode());
    }
}

最佳答案

我认为它在下面一行:

ks.load(cert,null);

'load' 用于加载 KeyStore 文件。您正在尝试加载证书。这两者的格式完全不同,这就是您收到错误的原因。

关于java.io.IOException : Wrong version of key store. 使用 SpongyCaSTLe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24355433/

相关文章:

java - 2008 SQL Server 数据库和 Android 应用程序之间的通信

使用 Thread 时 JavaFX 应用程序出现滞后

java - 使用 Mockito 进行单元测试时出现空指针

java - 使用 DEROctetString 与纯扩展

c# - 以 DER 格式导出 RSA 公钥并解密数据

java:如何将已编译的类文件的长列表包含到jar中

android - 是否有可用的 Android Facebook SDK 或 API?

android - 如何在 Android 主屏幕的角落滑动启动应用程序,或者它可能是任何屏幕,如惰性滑动

java - 将 AppCompat v7 添加到 gradle 会在编译时产生多重错误

java - 如何使用 JCA 读取 BouncyCaSTLe 私钥 PEM 文件?