Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ...
- KeyStore loadKeyStore (String ksFile, String pwd) {
- try {
- KeyStore ks = KeyStore.getInstance( KeyStore.getDefaultType() );
- InputStream stream = new FileInputStream( ksFile );
- /** verify the integrity of a keystore */
- ks.load( stream, pwd.toCharArray() );
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- void storeKeyStore (KeyStore ks, String ksFile, String pwd) {
- try {
- OutputStream stream = new FileOutputStream(ksFile);
- ks.store( stream, pwd.toCharArray() );
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- void printKeyEntry (KeyStore ks, String alias, String pwd) {
- System.out.println( "Private key: " + ks.getKey(alias,pwd.toCharArray() );
- Certificate certs[] = ks.getCertificateChain(alias);
- for (int i = 0; i < certs.length; i++) {
- if (c intanceof X509Certificate) {
- X509Certificate c = (X509Certificate) certs[i];
- System.out.println(
- "Subject DN: " + c.getSubjectDN() + ", Issuer DN: " + c.getIssuerDN()
- );
- }
- }
- }
- void printCertificateEntry (KeyStore ks, String alias) {
- Certificate c = ks.getCertificate(alias);
- if (c instanceof X509Certificate) {
- X509Certificate x509 = (X509Certificate) c;
- System.out.println(
- "Subject DN: " + x509.getSubjectDN() "Issuer DN: " + x509.getIssuerDN()
- );
- }
- }
- public static void main (String[] args) {
- String ksFile = System.getProperty("user.home")
- + File.separator + ".keystore";
- KeyStore ks = loadKeyStore( ksFile, "secret password" );
- String alias = "edek";
- if ( ks.isKeyEntry(alias) ) {
- printKeyEntry(
- ks,alias, "my very secret password protecting private key"
- );
- } else if ( ks.isCertificateEntry(alias) ) {
- printCertificateEntry( ks,alias );
- } else {
- System.out.println(
- "There is no entry for the alias: \"" + alias + "\" in the keystore"
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement