List of usage examples for org.bouncycastle.crypto.util PrivateKeyInfoFactory createPrivateKeyInfo
public static PrivateKeyInfo createPrivateKeyInfo(AsymmetricKeyParameter privateKey) throws IOException
From source file:org.albertschmitt.crypto.RSAService.java
License:Open Source License
/** * Write the RSAPrivateKey to a stream in DER format. * * @param outstream/*from w w w. ja va2s. c o m*/ * The stream the DER key is to be written to. * @param key * The RSAPrivatKey. * @throws IOException */ public void writeDERKey(OutputStream outstream, RSAPrivateKey key) throws IOException { AsymmetricKeyParameter keyParam = key.getKey(); PrivateKeyInfo pki = PrivateKeyInfoFactory.createPrivateKeyInfo(keyParam); byte[] keybytes = pki.getEncoded(); outstream.write(keybytes); outstream.close(); }
From source file:org.albertschmitt.crypto.RSAService.java
License:Open Source License
/** * Generate a Public / Private RSA key pair and write them to the designated Output Streams. * * @param os_private//from ww w . j a va 2s . c om * The stream to which the RSA Private Key will be written. * @param os_public * The stream to which the RSA Public Key will be written. * @throws java.io.IOException */ public void generateKey(OutputStream os_private, OutputStream os_public) throws IOException { BigInteger publicExponent = new BigInteger("10001", 16); SecureRandom secure = new SecureRandom(); RSAKeyGenerationParameters kparams = new RSAKeyGenerationParameters(publicExponent, secure, keysize.getKeySize(), 80); RSAKeyPairGenerator kpg = new RSAKeyPairGenerator(); kpg.init(kparams); AsymmetricCipherKeyPair keyPair = kpg.generateKeyPair(); // Write private key. PrivateKeyInfo pkiPrivate = PrivateKeyInfoFactory.createPrivateKeyInfo(keyPair.getPrivate()); writePEMKey(os_private, pkiPrivate); // Write public key. SubjectPublicKeyInfo pkiPublic = SubjectPublicKeyInfoFactory .createSubjectPublicKeyInfo(keyPair.getPublic()); writePEMKey(os_public, pkiPublic); }
From source file:org.cryptacular.adapter.AbstractWrappedKey.java
License:Open Source License
/** * @return Encoded PrivateKeyInfo structure in the case of a private key, * otherwise an encoded SubjectPublicKeyInfo structure. *//*from w w w . ja v a2s . c om*/ @Override public byte[] getEncoded() { try { if (delegate.isPrivate()) { return PrivateKeyInfoFactory.createPrivateKeyInfo(delegate).getEncoded(); } return SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(delegate).getEncoded(); } catch (IOException e) { throw new RuntimeException("Key encoding error.", e); } }
From source file:org.cryptoworkshop.ximix.node.crypto.key.ECKeyManager.java
License:Apache License
public synchronized byte[] getEncoded(char[] password) throws IOException, GeneralSecurityException { KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC"); try {/*w w w. j av a2 s . c o m*/ OutputEncryptor encOut = new JcePKCSPBEOutputEncryptorBuilder(NISTObjectIdentifiers.id_aes256_CBC) .setProvider("BC").build(password); JcaX509ExtensionUtils extUtils = new JcaX509ExtensionUtils(); PKCS12PfxPduBuilder builder = new PKCS12PfxPduBuilder(); for (String keyID : sharedPrivateKeyMap.getIDs()) { ECDomainParameters domainParams = paramsMap.get(keyID); PrivateKey privKey = fact .generatePrivate(new PKCS8EncodedKeySpec(PrivateKeyInfoFactory .createPrivateKeyInfo(new ECPrivateKeyParameters( sharedPrivateKeyMap.getShare(keyID).getValue(), domainParams)) .getEncoded())); SubjectPublicKeyInfo pubKey = this.fetchPublicKey(keyID); // TODO: perhaps add CA cert and trust anchor to key store if available PKCS12SafeBagBuilder eeCertBagBuilder = new PKCS12SafeBagBuilder( createCertificate(keyID, sharedPrivateKeyMap.getShare(keyID).getSequenceNo(), (PrivateKey) nodeContext.getNodeCAStore().getKey("nodeCA", new char[0]))); eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID)); SubjectKeyIdentifier pubKeyId = extUtils.createSubjectKeyIdentifier(pubKey); eeCertBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId); PKCS12SafeBagBuilder keyBagBuilder = new JcaPKCS12SafeBagBuilder(privKey, encOut); keyBagBuilder.addBagAttribute(PKCS12SafeBag.friendlyNameAttribute, new DERBMPString(keyID)); keyBagBuilder.addBagAttribute(PKCS12SafeBag.localKeyIdAttribute, pubKeyId); builder.addEncryptedData( new JcePKCSPBEOutputEncryptorBuilder(PKCSObjectIdentifiers.pbeWithSHAAnd128BitRC2_CBC) .setProvider("BC").build(password), new PKCS12SafeBag[] { eeCertBagBuilder.build() }); builder.addData(keyBagBuilder.build()); } PKCS12PfxPdu pfx = builder.build(new JcePKCS12MacCalculatorBuilder(NISTObjectIdentifiers.id_sha256), password); return pfx.getEncoded(ASN1Encoding.DL); } catch (PKCSException e) { throw new GeneralSecurityException("Unable to create key store: " + e.getMessage(), e); } catch (OperatorCreationException e) { throw new GeneralSecurityException("Unable to create operator: " + e.getMessage(), e); } }
From source file:org.xwiki.crypto.internal.asymmetric.BcPrivateKeyParameters.java
License:Open Source License
@Override public byte[] getEncoded() { try {/* www . java 2 s . co m*/ return PrivateKeyInfoFactory.createPrivateKeyInfo(parameters).getEncoded(); } catch (IOException e) { return null; } }
From source file:org.xwiki.crypto.internal.asymmetric.keyfactory.AbstractBcKeyFactory.java
License:Open Source License
@Override public PrivateKey toKey(PrivateKeyParameters key) { try {// w ww . j a v a 2s. c o m // Optimization if (key instanceof BcAsymmetricKeyParameters) { String keyType = checkKeyType((BcAsymmetricKeyParameters) key); if (keyType != null) { throw new IllegalArgumentException( String.format(CLASS_ERROR, keyType, PRIVATE, key.getClass().getName())); } return generatePrivate(PrivateKeyInfoFactory .createPrivateKeyInfo(((BcAsymmetricKeyParameters) key).getParameters())); } // Fallback return generatePrivate(PrivateKeyInfo.getInstance(key.getEncoded())); } catch (IOException e) { throw new IllegalArgumentException("Invalid private key parameters: " + key.getClass().getName()); } }
From source file:uk.ac.cam.gpe21.droidssl.mitm.crypto.key.KeyUtils.java
License:Apache License
public static PrivateKey readPrivateKey(Reader reader) throws IOException { try (PEMParser parser = new PEMParser(reader)) { Object object = parser.readObject(); if (!(object instanceof PEMKeyPair)) throw new IOException("File does not contain a key"); PEMKeyPair pair = (PEMKeyPair) object; // TODO merge messy conversion logic with that below */ AsymmetricKeyParameter privateKey = PrivateKeyFactory.createKey(pair.getPrivateKeyInfo()); PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(privateKey); KeyFactory keyFactory = new DefaultJcaJceHelper().createKeyFactory("RSA"); // TODO should we really assume RSA? return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded())); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new IOException(ex); }//from www. ja v a2 s .c o m }
From source file:uk.ac.cam.gpe21.droidssl.mitm.crypto.key.KeyUtils.java
License:Apache License
public static KeyPair convertToJca(AsymmetricCipherKeyPair keyPair) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { SubjectPublicKeyInfo publicKey = SubjectPublicKeyInfoFactory .createSubjectPublicKeyInfo(keyPair.getPublic()); PrivateKeyInfo privateKey = PrivateKeyInfoFactory.createPrivateKeyInfo(keyPair.getPrivate()); KeyFactory keyFactory = new DefaultJcaJceHelper().createKeyFactory("RSA"); // TODO should we really assume RSA? return new KeyPair(keyFactory.generatePublic(new X509EncodedKeySpec(publicKey.getEncoded())), keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKey.getEncoded()))); }