List of usage examples for java.security.cert Certificate getPublicKey
public abstract PublicKey getPublicKey();
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"; Key key = keystore.getKey(alias, "password".toCharArray()); if (key instanceof PrivateKey) { // Get certificate of public key Certificate cert = keystore.getCertificate(alias); // Get public key PublicKey publicKey = cert.getPublicKey(); // Return a key pair new KeyPair(publicKey, (PrivateKey) key); }//from w w w. jav a 2 s. c o m }
From source file:MainClass.java
public static void main(String args[]) throws Exception { Certificate[] certpath = new Certificate[args.length - 1]; CertificateFactory cf = CertificateFactory.getInstance("X.509"); int i;/* w ww. j a v a2s . co m*/ for (i = 0; i < args.length - 1; i++) { FileInputStream in = new FileInputStream(args[i]); certpath[i] = cf.generateCertificate(in); } FileInputStream in = new FileInputStream(args[i]); Certificate trust = cf.generateCertificate(in); boolean pass = false; String reason = ""; for (i = 0; i < certpath.length; i++) { try { PublicKey pbk; if (i == certpath.length - 1) { pbk = trust.getPublicKey(); } else { pbk = certpath[i + 1].getPublicKey(); } certpath[i].verify(pbk); pass = true; } catch (Exception e) { pass = false; reason += i + " " + e.toString(); break; } } System.out.println(pass); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { CertificateFactory cf = CertificateFactory.getInstance("X.509"); List mylist = new ArrayList(); for (int i = 0; i < args.length; i++) { FileInputStream in = new FileInputStream(args[i]); Certificate c = cf.generateCertificate(in); mylist.add(c);/*from w ww .j a v a2 s. c o m*/ } CertPath cp = cf.generateCertPath(mylist); List cplist = cp.getCertificates(); Object[] o = cplist.toArray(); for (int i = 0; i < o.length; i++) { X509Certificate c = (X509Certificate) o[i]; System.out.println(c.getSubjectDN()); byte[] pbk = c.getPublicKey().getEncoded(); for (int j = 0; j < pbk.length; j++) { System.out.print(pbk[j] + ","); } System.out.println("\nIssued by " + c.getIssuerDN()); } }
From source file:Main.java
/** * Returns SHA256 hash of the public key of a given certificate. * * @param cert the cert that should be used to retrieve the public key from. * @return SHA256 hash of the public key. *//*from w w w . j a v a2 s. c o m*/ public static byte[] getPublicKeySha256(Certificate cert) { try { byte[] publicKey = cert.getPublicKey().getEncoded(); MessageDigest digest = MessageDigest.getInstance("SHA-256"); return digest.digest(publicKey); } catch (NoSuchAlgorithmException ex) { // This exception should never happen since SHA-256 is known algorithm throw new RuntimeException(ex); } }
From source file:Main.java
public static String executeHttpsPost(String url, String data, InputStream key) { HttpsURLConnection localHttpsURLConnection = null; try {/*from w w w .j a v a 2 s .com*/ URL localURL = new URL(url); localHttpsURLConnection = (HttpsURLConnection) localURL.openConnection(); localHttpsURLConnection.setRequestMethod("POST"); localHttpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); localHttpsURLConnection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length)); localHttpsURLConnection.setRequestProperty("Content-Language", "en-US"); localHttpsURLConnection.setUseCaches(false); localHttpsURLConnection.setDoInput(true); localHttpsURLConnection.setDoOutput(true); localHttpsURLConnection.connect(); Certificate[] arrayOfCertificate = localHttpsURLConnection.getServerCertificates(); byte[] arrayOfByte1 = new byte[294]; DataInputStream localDataInputStream = new DataInputStream(key); localDataInputStream.readFully(arrayOfByte1); localDataInputStream.close(); Certificate localCertificate = arrayOfCertificate[0]; PublicKey localPublicKey = localCertificate.getPublicKey(); byte[] arrayOfByte2 = localPublicKey.getEncoded(); for (int i = 0; i < arrayOfByte2.length; i++) { if (arrayOfByte2[i] != arrayOfByte1[i]) throw new RuntimeException("Public key mismatch"); } DataOutputStream localDataOutputStream = new DataOutputStream( localHttpsURLConnection.getOutputStream()); localDataOutputStream.writeBytes(data); localDataOutputStream.flush(); localDataOutputStream.close(); InputStream localInputStream = localHttpsURLConnection.getInputStream(); BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream)); StringBuffer localStringBuffer = new StringBuffer(); String str1; while ((str1 = localBufferedReader.readLine()) != null) { localStringBuffer.append(str1); localStringBuffer.append('\r'); } localBufferedReader.close(); return localStringBuffer.toString(); } catch (Exception localException) { byte[] arrayOfByte1; localException.printStackTrace(); return null; } finally { if (localHttpsURLConnection != null) localHttpsURLConnection.disconnect(); } }
From source file:com.vmware.identity.rest.core.util.RequestSigner.java
/** * Verify a signed request using a hex-formatted string, the string to sign, and a * certificate's public key./*from ww w . j a va2 s. c om*/ * * @param signedRequestHex a hex-encoded string representing the signed request to verify. * @param stringToSign the string that will be signed with the public key for * verification purposes. * @param certificate the certificate containing the public key used for verification. * @return true if the signature was verified, false if not. * @throws DecoderException if there is an error decoding the hex string. * @throws InvalidKeyException if the public key is invalid. * @throws SignatureException if the signature algorithm is unable to process the input * data provided. */ public static boolean verify(String signedRequestHex, String stringToSign, Certificate certificate) throws DecoderException, InvalidKeyException, SignatureException { return verify(signedRequestHex, stringToSign, certificate.getPublicKey()); }
From source file:kr.co.exsoft.eframework.util.LicenseUtil.java
/** * //from w w w. j a va 2s . c o m * <pre> * 1. : ?? APPLICATION * 2. : * </pre> * @Method Name : decipherLicenseKey * @param licenseKey * @return String * @throws Exception */ public static String decipherLicenseKey(String licenseKey) throws Exception { String ret = null; if (StringUtils.isNotBlank(licenseKey)) { // ?? ? public key ? URL url = ClassLoader.getSystemResource("kr/co/exsoft/eframework/cert/exsoft.cer"); FileInputStream certfis = new FileInputStream(new File(url.getFile())); CertificateFactory cf = CertificateFactory.getInstance("X.509"); Certificate cert = cf.generateCertificate(certfis); PublicKey key = cert.getPublicKey(); // ?? . ret = unspell(licenseKey, key); } return ret; }
From source file:org.apache.ofbiz.base.util.KeyStoreUtil.java
public static String pemToPkHex(String certString) throws IOException, CertificateException { Certificate cert = pemToCert(certString); return StringUtil.toHexString(cert.getPublicKey().getEncoded()); }
From source file:com.qm.frame.infrastructure.util.ValidateLicense.java
/** * License/*from ww w .j a v a 2 s . co m*/ * * @param licenseColl LicenseColl * @param path String * @return boolean */ public static boolean validateLicense(LicenseColl licenseColl, String path) { // ?? boolean payedUser = true; List<String> macAddressList = CollectMacAddress.getMacAddress(); for (LicenseInfo licenseInfo : licenseColl.getLicenseInfoList()) { String productName = licenseInfo.getProductName(); String expirationDate = licenseInfo.getExpiration(); String signature = licenseInfo.getSignature(); boolean flag = false; String data = new StringBuffer().append(productName).append(expirationDate).toString(); java.security.cert.CertificateFactory cf; try { cf = java.security.cert.CertificateFactory.getInstance("X.509"); java.security.cert.Certificate cert = cf .generateCertificate(new FileInputStream(path + "/WEB-INF/qm.cer")); // PublicKey pubKey = cert.getPublicKey(); // ??Signature Signature sig = Signature.getInstance("SHA1withDSA"); sig.initVerify(pubKey); sig.update(InternationalizationUtil.getBytes(data)); // ?License boolean verifies = sig.verify(Base64.decode((InternationalizationUtil.getBytes(signature)))); if (verifies) { Date evalDate = DateTimeUtil.parseDate(expirationDate); if (evalDate.before(new Date())) { // ? payedUser = false; } else { flag = true; payedUser = true; System.out.println( "" + productName + " License" + expirationDate + "?"); } } } catch (Exception e) { log.error("", e); } if (!flag) { payedUser = false; System.out.println( "" + productName + " License??License?"); } } return payedUser; }
From source file:com.icanft.common.startup.ValidateLicense.java
/** * License//w w w .j ava 2 s .co m * * @param licenseColl LicenseColl * @param path String * @return boolean */ public static boolean validateLicense(LicenseColl licenseColl, String path) { // ?? boolean payedUser = true; List<String> macAddressList = CollectMacAddress.getMacAddress(); for (LicenseInfo licenseInfo : licenseColl.getLicenseInfoList()) { String productName = licenseInfo.getProductName(); String expirationDate = licenseInfo.getExpiration(); String signature = licenseInfo.getSignature(); boolean flag = false; String data = new StringBuffer().append(productName).append(expirationDate).toString(); java.security.cert.CertificateFactory cf; try { cf = java.security.cert.CertificateFactory.getInstance("X.509"); java.security.cert.Certificate cert = cf .generateCertificate(new FileInputStream(path + "/WEB-INF/fbrp.cer")); // PublicKey pubKey = cert.getPublicKey(); // ??Signature Signature sig = Signature.getInstance("SHA1withDSA"); sig.initVerify(pubKey); sig.update(InternationalizationUtil.getBytes(data)); // ?License boolean verifies = true; // sig.verify(Base64.decodeBase64(InternationalizationUtil // .getBytes(signature))); if (verifies) { Date evalDate = DateTimeUtil.parseDate(expirationDate); if (evalDate.before(new Date())) { // ? payedUser = false; } else { flag = true; payedUser = true; System.out.println( "" + productName + " License" + expirationDate + "?"); } } } catch (Exception e) { log.error("", e); } if (!flag) { payedUser = false; System.out.println( "" + productName + " License??License?"); } } return payedUser; }