List of usage examples for java.security.cert Certificate getEncoded
public abstract byte[] getEncoded() throws CertificateEncodingException;
From source file:Main.java
public static void main(String[] argv) throws Exception { FileInputStream is = new FileInputStream("your.keystore"); KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(is, "my-keystore-password".toCharArray()); String alias = "myalias"; Certificate cert = keystore.getCertificate(alias); File file = null;// w w w . java 2s. c o m byte[] buf = cert.getEncoded(); FileOutputStream os = new FileOutputStream(file); os.write(buf); os.close(); Writer wr = new OutputStreamWriter(os, Charset.forName("UTF-8")); wr.write(new sun.misc.BASE64Encoder().encode(buf)); wr.flush(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String keystoreFile = "keyStoreFile.bin"; String caAlias = "caAlias"; String certToSignAlias = "cert"; String newAlias = "newAlias"; char[] password = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; char[] caPassword = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; char[] certPassword = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' }; FileInputStream input = new FileInputStream(keystoreFile); KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(input, password);// ww w .j a v a 2 s. c o m input.close(); PrivateKey caPrivateKey = (PrivateKey) keyStore.getKey(caAlias, caPassword); java.security.cert.Certificate caCert = keyStore.getCertificate(caAlias); byte[] encoded = caCert.getEncoded(); X509CertImpl caCertImpl = new X509CertImpl(encoded); X509CertInfo caCertInfo = (X509CertInfo) caCertImpl.get(X509CertImpl.NAME + "." + X509CertImpl.INFO); X500Name issuer = (X500Name) caCertInfo.get(X509CertInfo.SUBJECT + "." + CertificateIssuerName.DN_NAME); java.security.cert.Certificate cert = keyStore.getCertificate(certToSignAlias); PrivateKey privateKey = (PrivateKey) keyStore.getKey(certToSignAlias, certPassword); encoded = cert.getEncoded(); X509CertImpl certImpl = new X509CertImpl(encoded); X509CertInfo certInfo = (X509CertInfo) certImpl.get(X509CertImpl.NAME + "." + X509CertImpl.INFO); Date firstDate = new Date(); Date lastDate = new Date(firstDate.getTime() + 365 * 24 * 60 * 60 * 1000L); CertificateValidity interval = new CertificateValidity(firstDate, lastDate); certInfo.set(X509CertInfo.VALIDITY, interval); certInfo.set(X509CertInfo.SERIAL_NUMBER, new CertificateSerialNumber((int) (firstDate.getTime() / 1000))); certInfo.set(X509CertInfo.ISSUER + "." + CertificateSubjectName.DN_NAME, issuer); AlgorithmId algorithm = new AlgorithmId(AlgorithmId.md5WithRSAEncryption_oid); certInfo.set(CertificateAlgorithmId.NAME + "." + CertificateAlgorithmId.ALGORITHM, algorithm); X509CertImpl newCert = new X509CertImpl(certInfo); newCert.sign(caPrivateKey, "MD5WithRSA"); keyStore.setKeyEntry(newAlias, privateKey, certPassword, new java.security.cert.Certificate[] { newCert }); FileOutputStream output = new FileOutputStream(keystoreFile); keyStore.store(output, password); output.close(); }
From source file:com.daon.identityx.utils.GenerateAndroidFacet.java
public static void main(String[] args) { String androidKeystoreLocation = System.getProperty("ANDROID_KEYSTORE_LOCATION", DEFAULT_ANDROID_KEYSTORE_LOCATION); String androidKeystorePassword = System.getProperty("ANDROID_KEYSTORE_PASSWORD", DEFAULT_ANDROID_KEYSTORE_PASSWORD); String androidKeystoreCert = System.getProperty("ANDROID_KEYSTORE_CERT_NAME", DEFAULT_ANDROID_KEYSTORE_CERT_NAME); String hashingAlgorithm = System.getProperty("HASHING_ALGORITHM", DEFAULT_HASHING_ALGORITHM); try {//w w w. j a va2 s . com KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); File filePath = new File(androidKeystoreLocation); if (!filePath.exists()) { System.err.println( "The filepath to the debug keystore could not be located at: " + androidKeystoreCert); System.exit(1); } else { System.out.println("Found the Android Studio keystore at: " + androidKeystoreLocation); } keyStore.load(new FileInputStream(filePath), androidKeystorePassword.toCharArray()); System.out.println("Keystore loaded - password and location were OK"); Certificate cert = keyStore.getCertificate(androidKeystoreCert); if (cert == null) { System.err.println( "Could not location the certification in the store with the name: " + androidKeystoreCert); System.exit(1); } else { System.out.println("Certificate found in the store with name: " + androidKeystoreCert); } byte[] certBytes = cert.getEncoded(); MessageDigest digest = MessageDigest.getInstance(hashingAlgorithm); System.out.println("Hashing algorithm: " + hashingAlgorithm + " found."); byte[] hashedCert = digest.digest(certBytes); String base64HashedCert = Base64.getEncoder().encodeToString(hashedCert); System.out.println("Base64 encoded SHA-1 hash of the certificate: " + base64HashedCert); String base64HashedCertRemoveTrailing = StringUtils.deleteAny(base64HashedCert, "="); System.out.println( "Add the following facet to the Facets file in order for the debug app to be trusted by the FIDO client"); System.out.println("\"android:apk-key-hash:" + base64HashedCertRemoveTrailing + "\""); } catch (Throwable ex) { ex.printStackTrace(); } }
From source file:com.aqnote.shared.cryptology.cert.tool.X509CertTool.java
public static String coverCert2String(Certificate cert) throws CertificateEncodingException { String certContent = Base64.encodeBase64String(cert.getEncoded()); String crtFile = BEGIN_CERT + lineSeparator + certContent + END_CERT; return crtFile; }
From source file:com.thoughtworks.go.security.RegistrationJSONizer.java
public static String toJson(Registration registration) { Map<String, Object> ret = new HashMap<>(); if (registration.isValid()) { ret.put("agentPrivateKey", serialize("RSA PRIVATE KEY", registration.getPrivateKey().getEncoded())); StringBuilder builder = new StringBuilder(); for (Certificate c : registration.getChain()) { try { builder.append(serialize("CERTIFICATE", c.getEncoded())); } catch (CertificateEncodingException e) { throw bomb(e); }/*from w w w .j a v a 2 s . c o m*/ } ret.put("agentCertificate", builder.toString()); } return GSON.toJson(ret); }
From source file:com.vmware.identity.rest.core.util.CertificateHelper.java
/** * Encodes an {@code Certificate} into a PEM-formatted encoding. * * @param certificate the certificate to encode. * @return the PEM-formatted encoding of the certificate. * @throws CertificateEncodingException if an encoding error occurs. *///w ww .j a v a 2 s. c om public static String convertToPEM(Certificate certificate) throws CertificateEncodingException { if (certificate == null) { return null; } return convertToPEM(certificate.getEncoded()); }
From source file:Main.java
/** * Returns the {@link Certificate} fingerprint as returned by <code>keytool</code>. * * @param certificate/*from w w w. j a va2s . com*/ * @param hashAlgorithm */ public static String getFingerprint(Certificate cert, String hashAlgorithm) { if (cert == null) { return null; } try { MessageDigest digest = MessageDigest.getInstance(hashAlgorithm); return toHexadecimalString(digest.digest(cert.getEncoded())); } catch (NoSuchAlgorithmException e) { // ignore } catch (CertificateEncodingException e) { // ignore } return null; }
From source file:org.apache.ofbiz.base.util.KeyStoreUtil.java
public static String certToString(Certificate cert) throws CertificateEncodingException { byte[] certBuf = cert.getEncoded(); StringBuilder buf = new StringBuilder(); buf.append("-----BEGIN CERTIFICATE-----\n"); buf.append(new String(Base64.encodeBase64Chunked(certBuf))); buf.append("\n-----END CERTIFICATE-----\n"); return buf.toString(); }
From source file:org.codice.ddf.security.certificate.generator.CertificateCommandTest.java
private static void validateSans(KeyStoreFile ksf, String alias, boolean withAdditionalSans) throws Exception { final KeyStore.Entry ke = ksf.getEntry(alias); assertThat(ke, instanceOf(KeyStore.PrivateKeyEntry.class)); final KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry) ke; final Certificate c = pke.getCertificate(); final X509CertificateHolder holder = new X509CertificateHolder(c.getEncoded()); final Extension csn = holder.getExtension(Extension.subjectAlternativeName); assertThat(csn.getParsedValue().toASN1Primitive().getEncoded(ASN1Encoding.DER), equalTo(expectedSanGeneralName(alias, withAdditionalSans))); }
From source file:vellumcert.Pems.java
public static String buildCertPem(Certificate cert) throws CertificateEncodingException { StringBuilder builder = new StringBuilder(); builder.append(DASHES);//from w w w . j av a 2s . com builder.append(BEGIN_CERT); builder.append(DASHES); builder.append('\n'); String text = Base64.encodeBase64String(cert.getEncoded()); for (int index = 0;; index += LENGTH) { if (index + LENGTH < text.length()) { builder.append(text.substring(index, index + LENGTH)); builder.append('\n'); } else { builder.append(text.substring(index)); builder.append('\n'); break; } } builder.append(DASHES); builder.append(END_CERT); builder.append(DASHES); builder.append('\n'); return builder.toString(); }