private static PrivateKey generatePrivateKey(final PrivateKeyInfo privateKeyInfo) throws IOException, InvalidKeySpecException { final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()); return tryGeneratePrivateKey(spec, PRIVATE_KEY_ALGS); }
public static byte[] signData(byte[] data) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // This is more or less code that has been just modified long enough that it works // Don't take it as good example how to get a Privatekey StringReader keyreader = new StringReader(SimpleSigner.certchain[0] + SimpleSigner.pemkey); PEMParser pemparser = new PEMParser(keyreader); X509CertificateHolder cert = (X509CertificateHolder) pemparser.readObject(); PrivateKeyInfo keyInfo = (PrivateKeyInfo) pemparser.readObject(); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyInfo.getEncoded()); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey key = kf.generatePrivate(keySpec); // The actual signing Cipher signer; signer = Cipher.getInstance("RSA/ECB/PKCS1PADDING"); signer.init(Cipher.ENCRYPT_MODE, key); byte[] signed_bytes = signer.doFinal(data); return signed_bytes; } }
public static byte[] getEncodedPrivateKeyInfo(PrivateKeyInfo info) { try { return info.getEncoded(ASN1Encoding.DER); } catch (Exception e) { return null; } } }
public static byte[] getEncodedPrivateKeyInfo(PrivateKeyInfo info) { try { return info.getEncoded(ASN1Encoding.DER); } catch (Exception e) { return null; } } }
private static PrivateKey generatePrivateKey(PrivateKeyInfo privateKeyInfo) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException { final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()); final KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(spec); }
public byte[] getEncoded() { try { PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(params, attributes); return pki.getEncoded(); } catch (IOException e) { return null; } }
@Override public byte[] getEncoded() { try { return PrivateKeyInfoFactory.createPrivateKeyInfo(this.parameters).getEncoded(); } catch (IOException e) { return null; } } }
public byte[] getEncoded() { try { PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(keyParams, attributes); return pki.getEncoded(); } catch (IOException e) { return null; } }
@Override public byte[] getEncoded() { try { return PrivateKeyInfoFactory.createPrivateKeyInfo(parameters).getEncoded(); } catch (IOException e) { return null; } } }
public byte[] getEncoded() { try { PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(keyParams, attributes); return pki.getEncoded(); } catch (IOException e) { return null; } }
public byte[] getEncoded() { PrivateKeyInfo pki; try { pki = PrivateKeyInfoFactory.createPrivateKeyInfo(keyParams, attributes); return pki.getEncoded(); } catch (IOException e) { return null; } }
public static PrivateKey loadPrivateKey(String keyPath) throws IOException, GeneralSecurityException { PEMKeyPair keyPair = loadPEM(keyPath); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded()); return KeyFactory.getInstance("RSA").generatePrivate(keySpec); }
public static PrivateKey loadPrivateKey(String keyPath) throws IOException, GeneralSecurityException { PEMKeyPair keyPair = loadPEM(keyPath); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded()); return KeyFactory.getInstance("RSA").generatePrivate(keySpec); }
public static PrivateKey loadPrivateKey(String keyPath) throws IOException, GeneralSecurityException { PEMKeyPair keyPair = loadPEM(keyPath); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded()); return KeyFactory.getInstance("RSA").generatePrivate(keySpec); }
private static KeyPair loadPrivateKey(final String certPath) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { Path path = Paths.get(certPath, "key.pem"); BufferedReader reader = Files.newBufferedReader(path, Charset.defaultCharset()); PEMParser parser = new PEMParser(reader); Object object = parser.readObject(); PEMKeyPair keyPair = (PEMKeyPair) object; byte[] privateEncoded = keyPair.getPrivateKeyInfo().getEncoded(); byte[] publicEncoded = keyPair.getPublicKeyInfo().getEncoded(); KeyFactory factory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicEncoded); PublicKey publicKey = factory.generatePublic(publicKeySpec); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateEncoded); PrivateKey privateKey = factory.generatePrivate(privateKeySpec); return new KeyPair(publicKey, privateKey); }
protected static void printKeyAndCert(String prefix, KeyAndCert keyAndCert) throws CertificateEncodingException, IOException { printCert(prefix, keyAndCert.getCert()); System.out.println("-----BEGIN PRIVATE KEY-----"); System.out.println(Base64.encodeToString(keyAndCert.getKey().getEncoded(), true)); System.out.println("-----END PRIVATE KEY-----"); }
public PKCS8EncodedKeySpec readPrivateKey(FileInputStream fis, Optional<String> keyPassword) throws IOException { PEMParser keyReader = new PEMParser(new InputStreamReader(fis)); PEMDecryptorProvider decryptorProvider = new JcePEMDecryptorProviderBuilder().build(keyPassword.get().toCharArray()); Object keyPair = keyReader.readObject(); keyReader.close(); PrivateKeyInfo keyInfo; if (keyPair instanceof PEMEncryptedKeyPair) { PEMKeyPair decryptedKeyPair = ((PEMEncryptedKeyPair) keyPair).decryptKeyPair(decryptorProvider); keyInfo = decryptedKeyPair.getPrivateKeyInfo(); } else { keyInfo = ((PEMKeyPair) keyPair).getPrivateKeyInfo(); } return new PKCS8EncodedKeySpec(keyInfo.getEncoded()); } }
@Override public PrivateKey generatePrivate(PrivateKeyInfo privateKeyInfo) throws IOException { AsymmetricKeyFactory factory = getKeyFactory(privateKeyInfo); if (factory instanceof AsymmetricKeyInfoConverter) { return ((AsymmetricKeyInfoConverter) factory).generatePrivate(privateKeyInfo); } return factory.toKey(fromPKCS8(privateKeyInfo.getEncoded())); }
@Override public PrivateKey generatePrivate(PrivateKeyInfo privateKeyInfo) throws IOException { AsymmetricKeyFactory factory = getKeyFactory(privateKeyInfo); if (factory instanceof AsymmetricKeyInfoConverter) { return ((AsymmetricKeyInfoConverter) factory).generatePrivate(privateKeyInfo); } return factory.toKey(fromPKCS8(privateKeyInfo.getEncoded())); }
/** * @return Encoded PrivateKeyInfo structure in the case of a private key, otherwise an encoded SubjectPublicKeyInfo * structure. */ @Override public byte[] getEncoded() { try { if (delegate.isPrivate()) { return PrivateKeyInfoFactory.createPrivateKeyInfo(delegate).getEncoded(); } return SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(delegate).getEncoded(); } catch (IOException e) { throw new EncodingException("Key encoding error", e); } } }