Retrieving a Certificate from a Key Store - Java Security

Java examples for Security:Certificate

Description

Retrieving a Certificate from a Key Store

Demo Code

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

public class Main {
  public static void main(String[] args) throws Exception {
    try {//www  .j a  v  a  2s  .  com
      // Load the keystore in the user's home directory
      FileInputStream is = new FileInputStream(System.getProperty("user.home")
          + File.separatorChar + ".keystore");

      KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
      keystore.load(is, "my-keystore-password".toCharArray());

      // Get certificate
      java.security.cert.Certificate cert = keystore.getCertificate("myalias");
    } catch (KeyStoreException e) {
    } catch (java.security.cert.CertificateException e) {
    } catch (NoSuchAlgorithmException e) {
    } catch (java.io.IOException e) {
    }
  }

}

Related Tutorials