Skip to content

Instantly share code, notes, and snippets.

@babafeng
Last active February 3, 2017 06:35
Show Gist options
  • Select an option

  • Save babafeng/bb53ca7594c7e54ccbe5d6ebf995c199 to your computer and use it in GitHub Desktop.

Select an option

Save babafeng/bb53ca7594c7e54ccbe5d6ebf995c199 to your computer and use it in GitHub Desktop.
try {
AssetManager asset = getResources().getAssets();
CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
InputStream cerInputStream = asset.open("demons.cer");
Certificate certificate = certificateFactory.generateCertificate(cerInputStream);
RSAPublicKey publicKey = (RSAPublicKey) certificate.getPublicKey();
Log.d(TAG, " public: " + publicKey);
} catch (IOException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
}
try {
InputStream cerInputStream = new FileInputStream("C:\\holmes\\Java\\demons.cer");
CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
Certificate certificate = certificateFactory.generateCertificate(cerInputStream);
RSAPublicKey publicKeyx = (RSAPublicKey) certificate.getPublicKey();
System.out.println(publicKeyx);
} catch (IOException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
}
public class java_keystore {
public static void main(String args[]) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException {
// keystore pass
String store_pass = "demons";
InputStream inputStream = new FileInputStream("C:\\holmes\\Java\\demons.keystore");
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(inputStream, store_pass.toCharArray());
// alias
String alias = "demons";
// key pass
String key_pass = "demons";
// privateKey
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, key_pass.toCharArray());
// publicKey
PublicKey publicKey = keyStore.getCertificate(alias).getPublicKey();
System.out.println(privateKey);
System.out.println(publicKey);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment