Code example for KeyStore

Methods: load, setCertificateEntry, store

0
                    mKnownServersStore.load(in, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
                } finally { 
                    in.close();
                } 
            } else { 
                mKnownServersStore.load(null, LOCAL_TRUSTSTORE_PASSWORD.toCharArray()); // necessary
                                                                                        // to 
                                                                                        // initialize 
                                                                                        // an 
                                                                                        // empty 
                                                                                        // KeyStore 
                                                                                        // instance 
            } 
        } 
        return mKnownServersStore;
    } 
 
    public static void addCertToKnownServersStore(Certificate cert, Context context) throws KeyStoreException,
            NoSuchAlgorithmException, CertificateException, IOException { 
        KeyStore knownServers = getKnownServersStore(context);
        knownServers.setCertificateEntry(Integer.toString(cert.hashCode()), cert);
        FileOutputStream fos = null;
        try { 
            fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE);
            knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
        } finally { 
            fos.close();
        } 
    }