Example usage for java.security KeyStore getInstance

List of usage examples for java.security KeyStore getInstance

Introduction

In this page you can find the example usage for java.security KeyStore getInstance.

Prototype

public static KeyStore getInstance(String type) throws KeyStoreException 

Source Link

Document

Returns a keystore object of the specified type.

Usage

From source file:com.linkage.crm.csb.sign.CtSignature.java

/**
 * @param originalText String /*from  ww w.  j  av a2  s. co  m*/
 * @param pwd String 
 * @param alias String 
 * @param priKeyFile 
 * @return String 
 */
public static String signature(String originalText, String pwd, String alias, String priKeyFile) {
    try {
        KeyStore ks = KeyStore.getInstance("JKS");
        FileInputStream ksfis = new FileInputStream(priKeyFile);
        BufferedInputStream ksbufin = new BufferedInputStream(ksfis);
        char[] kpass = pwd.toCharArray();
        ks.load(ksbufin, kpass);
        PrivateKey priKey = (PrivateKey) ks.getKey(alias, kpass);
        Signature rsa = Signature.getInstance("SHA1withDSA");
        rsa.initSign(priKey);
        rsa.update(originalText.getBytes());
        byte[] signedText = rsa.sign();
        return HexUtils.toHexString(signedText);
    } catch (Exception ex) {
        logger.error("errors appeared while trying to signature", ex);
        return null;
    }
}

From source file:com.appfirst.communication.AFHttpClient.java

public DefaultHttpClient getAFHttpClient() {
    try {/* w w  w .j a  va2s  .co  m*/
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        try {
            trustStore.load(null, null);
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        SSLSocketFactory sf = new AFSSLSocketFactory(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (NoSuchAlgorithmException nsae) {
        Log.e(TAG, nsae.getMessage());
        return new DefaultHttpClient();
    } catch (KeyManagementException kme) {
        Log.e(TAG, kme.getMessage());
        return new DefaultHttpClient();
    } catch (KeyStoreException kse) {
        Log.e(TAG, kse.getMessage());
        return new DefaultHttpClient();
    } catch (UnrecoverableKeyException uke) {
        Log.e(TAG, uke.getMessage());
        return new DefaultHttpClient();
    }
}

From source file:com.michael.openexercise.mc_network.volleydemo.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//w  w  w .j  a  v a 2  s  .  c o  m

        // Client should authenticate itself with the valid certificate to Server.
        InputStream clientStream = VolleySampleApplication.getContext().getResources()
                .openRawResource(R.raw.production_test_client);
        char[] password = "XXXXXXXXXXXXX".toCharArray();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(clientStream, password);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);

        // Client should also add the CA certificate obtained from server and create TrustManager from it for the client to validate the
        // identity of the server.
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream instream = null;
        instream = VolleySampleApplication.getContext().getResources()
                .openRawResource(R.raw.production_test_ca);

        try {
            trustStore.load(instream, "XXXXXXXX".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ignore) {
            }
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustStore);

        // Create an SSLContext that uses our TrustManager & Keystore
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);

        return context;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}

From source file:com.longluo.volleydemo.ssl.EasySSLSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from w ww .  ja v  a  2s.  c  o m

        // Client should authenticate itself with the valid certificate to
        // Server.
        InputStream clientStream = VolleySampleApplication.getContext().getResources()
                .openRawResource(R.raw.production_test_client);
        char[] password = "XXXXXXXXXXXXX".toCharArray();

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(clientStream, password);

        KeyManagerFactory keyManagerFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyManagerFactory.init(keyStore, password);

        // Client should also add the CA certificate obtained from server
        // and create TrustManager from it for the client to validate the
        // identity of the server.
        KeyStore trustStore = KeyStore.getInstance("BKS");
        InputStream instream = null;
        instream = VolleySampleApplication.getContext().getResources()
                .openRawResource(R.raw.production_test_ca);

        try {
            trustStore.load(instream, "XXXXXXXX".toCharArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                instream.close();
            } catch (Exception ignore) {
            }
        }

        String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
        tmf.init(trustStore);

        // Create an SSLContext that uses our TrustManager & Keystore
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(keyManagerFactory.getKeyManagers(), tmf.getTrustManagers(), null);

        return context;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}

From source file:com.wso2telco.identity.application.authentication.endpoint.util.MutualSSLClient.java

/**
 * load trust store with given .jks file
 *
 * @param trustStorePath/*from w ww . j  a v a  2 s .c  o  m*/
 * @param trustStorePassoword
 * @throws java.security.KeyStoreException
 * @throws java.io.IOException
 * @throws java.security.cert.CertificateException
 * @throws java.security.NoSuchAlgorithmException
 */
public static void loadTrustStore(String trustStorePath, String trustStorePassoword)
        throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
    trustStore = KeyStore.getInstance(TRUST_STORE_TYPE);
    trustStore.load(new FileInputStream(trustStorePath), trustStorePassoword.toCharArray());
}

From source file:com.bright.json.JSonRequestor.java

private static HttpClient getNewHttpClient() {
    try {//from  w  w w .  java  2  s.c o  m
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        MySSLSocketFactory sf = new MySSLSocketFactory(trustStore);
        sf.setHostnameVerifier(MySSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));

        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}

From source file:edu.cwru.apo.TrustAPOHttpClient.java

private SSLSocketFactory newSslSocketFactory() {
    try {/*ww w  .j  ava2  s  . com*/
        // Get an instance of the Bouncy Castle KeyStore format
        KeyStore trusted = KeyStore.getInstance("BKS");
        // Get the raw resource, which contains the keystore with
        // your trusted certificates (root and any intermediate certs)
        InputStream in = context.getResources().openRawResource(R.raw.keystore);
        try {
            // Initialize the keystore with the provided trusted certificates
            // Also provide the password of the keystore
            trusted.load(in, "mysecret".toCharArray());
        } finally {
            in.close();
        }
        // Pass the keystore to the SSLSocketFactory. The factory is responsible
        // for the verification of the server certificate.
        SSLSocketFactory sf = new SSLSocketFactory(trusted);
        // Hostname verification from certificate
        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}

From source file:kr.co.exsoft.eframework.util.LicenseUtil.java

/**
 * ??  ?.//from  www  .j a  va2s. c o m
 * 
 * @param licenseType
 * @param userCount
 * @return String
 */
public static String generateLicenseKey(String licenseType, int userCount) {

    String ksPass = "loveboat";
    String keyPass = "loveboat";
    String alias = "ab942e0f-9e4a-44b9-9f82-0a5f5d48ba12";
    String ret = null;

    try {
        // ??   
        URL url = ClassLoader.getSystemResource("kr/co/exsoft/eframework/cert/exsoft.pfx");
        FileInputStream certfis = new FileInputStream(new File(url.getFile()));

        // Private Key ?.
        BufferedInputStream ksbufin = new BufferedInputStream(certfis);

        KeyStore ks = KeyStore.getInstance("PKCS12");
        ks.load(ksbufin, ksPass.toCharArray());

        PrivateKey key = (PrivateKey) ks.getKey(alias, keyPass.toCharArray());

        // ??  ?.
        ret = spell("EDMsl|" + licenseType + "|" + userCount + "|", key);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return ret;
}

From source file:fi.helsinki.moodi.config.OodiConfig.java

private KeyStore oodiKeyStore(String keystoreLocation, char[] keystorePassword) throws Exception {
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    FileSystemResource keystoreFile = new FileSystemResource(new File(keystoreLocation));

    keyStore.load(keystoreFile.getInputStream(), keystorePassword);
    return keyStore;
}

From source file:com.phonty.improved.PhontyHttpClient.java

private SSLSocketFactory newSslSocketFactory() {
    try {//  www.j a  v a2 s  .co  m
        // Get an instance of the Bouncy Castle KeyStore format
        KeyStore trusted = KeyStore.getInstance("BKS");
        // Get the raw resource, which contains the keystore with
        // your trusted certificates (root and any intermediate certs)
        InputStream in = context.getResources().openRawResource(R.raw.keystore);
        try {
            // Initialize the keystore with the provided trusted certificates
            // Also provide the password of the keystore
            trusted.load(in, "pqoeponkjlcnvkjenenobnervoerovneokrnvoie".toCharArray());
        } finally {
            in.close();
        }
        // Pass the keystore to the SSLSocketFactory. The factory is responsible
        // for the verification of the server certificate.
        SSLSocketFactory sf = new SSLSocketFactory(trusted);
        // Hostname verification from certificate
        // http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html#d4e506
        sf.setHostnameVerifier(SSLSocketFactory.STRICT_HOSTNAME_VERIFIER);
        return sf;
    } catch (Exception e) {
        throw new AssertionError(e);
    }
}