密钥大小非法

时间:2012-08-13 17:44:21

标签: java android ssl-certificate bouncycastle keystore

我已经安装了jce以允许更大的密钥,但是KeytoolUIU和Portecle都给出了错误,例如 java.IO.Exception:初始化密钥库的存储时出错:java.security.InvalidKeyException:Illegal Key Size 。关键是只有1024,所以我不知道它为什么抱怨。

这是我当前加载密钥文件和访问安全网站的代码。

package com.g4apps.secure.android.sslclient;

import java.io.InputStream;
import java.security.KeyStore;
import java.security.Security;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import android.content.Context;

/**
 * This example demonstrates how to create secure connections with a custom SSL
 * context.
 */
public class SSLclient {


    public final static String authenticate(Context context) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        String output=null;

        Security.addProvider(new BouncyCastleProvider());
        try {
            KeyStore trustStore  = KeyStore.getInstance("BKS");
            InputStream instream = context.getResources().getAssets().open("my.truststore");
            try {
                trustStore.load(instream, "dysan100".toCharArray());
            } finally {
                try { instream.close(); } catch (Exception ignore) {}
            }
            KeyStore keystore = KeyStore.getInstance("BKS");
            InputStream keystream = context.getResources().getAssets().open("my.keystore.bks");
            try {
                keystore.load(keystream, "dysan100".toCharArray());
            } finally {
                try { keystream.close(); } catch (Exception ignore) {}
            }

            SSLSocketFactory socketFactory = new SSLSocketFactory(keystore,"dysan100",trustStore);
            socketFactory.setHostnameVerifier(new AllowAllHostnameVerifier());
            Scheme sch = new Scheme("https", socketFactory, 443);
            httpclient.getConnectionManager().getSchemeRegistry().register(sch);

            HttpGet httpget = new HttpGet("https://192.168.1.123/test.php");

            System.out.println("executing request" + httpget.getRequestLine());

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
                output=EntityUtils.toString(entity);
                System.out.println(output);
                return output;

            }


        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }

        return null;
    }

}

目前,密钥库是这样构成的

my.truststore.bks有我的CA证书
my.keystore.bks假设有我的服务器证书,客户证书和客户私钥。

这与我在我的电脑版程序中设置的方式相同(尽管使用JKS商店)。

既然不让我这样设置我的商店,那还有另一种可能对我有用的方式吗?

1 个答案:

答案 0 :(得分:1)

我不知道为什么我无法创建bks密钥库,但我能够使用PKCS12密钥库。所以我有另一种选择。

        KeyStore keystore = KeyStore.getInstance("PKCS12");
        InputStream keystream = context.getResources().getAssets().open("client.p12");
        try {
            keystore.load(keystream, "dysan100".toCharArray());
        } finally {
            try { keystream.close(); } catch (Exception ignore) {}
        }