private static PrivateKey generatePrivateKey(final PrivateKeyInfo privateKeyInfo) throws IOException, InvalidKeySpecException { final PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()); return tryGeneratePrivateKey(spec, PRIVATE_KEY_ALGS); }
private ByteString pkcs1Bytes() { try { PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(keyPair.getPrivate().getEncoded()); return ByteString.of(privateKeyInfo.parsePrivateKey().toASN1Primitive().getEncoded()); } catch (IOException e) { throw new AssertionError(e); } }
/** * Returns a {@link PEMKeyPair} object with direct access to the public and private keys given a PKCS #8 private key. * * @param privateKeyInfo the PKCS #8 private key info * @return the PKCS #1 public and private key pair * @throws IOException if there is an error converting the key pair */ private static PEMKeyPair convertPrivateKeyFromPKCS8ToPKCS1(PrivateKeyInfo privateKeyInfo) throws IOException { // Parse the key wrapping to determine the internal key structure ASN1Encodable asn1PrivateKey = privateKeyInfo.parsePrivateKey(); // Convert the parsed key to an RSA private key RSAPrivateKey keyStruct = RSAPrivateKey.getInstance(asn1PrivateKey); // Create the RSA public key from the modulus and exponent RSAPublicKey pubSpec = new RSAPublicKey( keyStruct.getModulus(), keyStruct.getPublicExponent()); // Create an algorithm identifier for forming the key pair AlgorithmIdentifier algId = new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE); if (isVerbose()) { logger.info("Converted private key from PKCS #8 to PKCS #1 RSA private key"); } // Create the key pair container return new PEMKeyPair(new SubjectPublicKeyInfo(algId, pubSpec), new PrivateKeyInfo(algId, keyStruct)); }
/** Read a PKCS#8 format private key. */ private static PrivateKey readPrivateKey(InputStream input) throws IOException, GeneralSecurityException { try { byte[] buffer = new byte[4096]; int size = input.read(buffer); byte[] bytes = Arrays.copyOf(buffer, size); /* Check to see if this is in an EncryptedPrivateKeyInfo structure. */ PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(bytes); /* * Now it's in a PKCS#8 PrivateKeyInfo structure. Read its Algorithm * OID and use that to construct a KeyFactory. */ ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(spec.getEncoded())); PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject()); String algOid = pki.getPrivateKeyAlgorithm().getAlgorithm().getId(); return KeyFactory.getInstance(algOid).generatePrivate(spec); } finally { input.close(); } } /**
AlgorithmIdentifier keyAlg = privateKeyInfo.getPrivateKeyAlgorithm(); ASN1ObjectIdentifier keyAlgOid = keyAlg.getAlgorithm(); if (PKCSObjectIdentifiers.rsaEncryption.equals(keyAlgOid)) { algoName = "RSA"; } else if (X9ObjectIdentifiers.id_dsa.equals(keyAlgOid)) { algoName = "DSA"; } else if (X9ObjectIdentifiers.id_ecPublicKey.equals(keyAlgOid)) { algoName = "EC"; } else { KeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyInfo.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance(algoName, "BC"); return keyFactory.generatePrivate(keySpec);
PrivateKeyInfo info = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(key.getEncoded())); if (info.getPrivateKeyAlgorithm().getAlgorithm().equals(CryptoProObjectIdentifiers.gostR3410_2001)) X962Parameters params = X962Parameters.getInstance(info.getPrivateKeyAlgorithm().getParameters()); X9ECParameters curveParams; ASN1ObjectIdentifier oid = ASN1ObjectIdentifier.getInstance(params.getParameters()); info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), info.parsePrivateKey()); return keyFact.generatePrivate(new PKCS8EncodedKeySpec(info.getEncoded()));
pi = PrivateKeyInfo.getInstance(o); } catch (Exception e) { pi = null; final String algOid = pi.getAlgorithmId().getObjectId().getId(); if (RSA_ID.equals(pi.getAlgorithmId().getObjectId())) { algorithm = "RSA"; } else if (EC_ID.equals(pi.getAlgorithmId().getObjectId())) { algorithm = "EC"; } else if (DSA_ID.equals(pi.getAlgorithmId().getObjectId())) { algorithm = "DSA"; } else { spec = new PKCS8EncodedKeySpec(encoded); } catch (Exception e) { throw new CryptException("Invalid PKCS#8 private key format.", e);
private void populateFromPrivateKeyInfo(PrivateKeyInfo keyInfo) throws IOException { ASN1Encodable keyOcts = keyInfo.parsePrivateKey(); if (EdECObjectIdentifiers.id_Ed448.equals(keyInfo.getPrivateKeyAlgorithm().getAlgorithm())) { eddsaPrivateKey = new Ed448PrivateKeyParameters(ASN1OctetString.getInstance(keyOcts).getOctets(), 0); } else { eddsaPrivateKey = new Ed25519PrivateKeyParameters(ASN1OctetString.getInstance(keyOcts).getOctets(), 0); } }
public PrivateKey generatePrivate(PrivateKeyInfo keyInfo) throws IOException { ASN1ObjectIdentifier algOid = keyInfo.getPrivateKeyAlgorithm().getAlgorithm(); if (algOid.equals(UAObjectIdentifiers.dstu4145le) || algOid.equals(UAObjectIdentifiers.dstu4145be)) { return new BCDSTU4145PrivateKey(keyInfo); } else { throw new IOException("algorithm identifier " + algOid + " in key not recognised"); } }
public byte[] getEncoded() { RainbowPrivateKey privateKey = new RainbowPrivateKey(A1inv, b1, A2inv, b2, vi, layers); PrivateKeyInfo pki; try { AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PQCObjectIdentifiers.rainbow, DERNull.INSTANCE); pki = new PrivateKeyInfo(algorithmIdentifier, privateKey); } catch (IOException e) { return null; } try { byte[] encoded = pki.getEncoded(); return encoded; } catch (IOException e) { return null; } }
byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); pki = PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey)); if (PQCObjectIdentifiers.mcEliece.equals(pki.getPrivateKeyAlgorithm().getAlgorithm())) McEliecePrivateKey key = McEliecePrivateKey.getInstance(pki.parsePrivateKey());
private void init(PrivateKeyInfo keyInfo) throws IOException { this.attributes = keyInfo.getAttributes(); XMSSKeyParams keyParams = XMSSKeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters()); this.treeDigest = keyParams.getTreeDigest().getAlgorithm(); this.keyParams = (XMSSPrivateKeyParameters)PrivateKeyFactory.createKey(keyInfo); }
public PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof PKCS8EncodedKeySpec) { // get the DER-encoded Key according to PKCS#8 from the spec byte[] encKey = ((PKCS8EncodedKeySpec)keySpec).getEncoded(); try { return generatePrivate(PrivateKeyInfo.getInstance(ASN1Primitive.fromByteArray(encKey))); } catch (Exception e) { throw new InvalidKeySpecException(e.toString()); } } throw new InvalidKeySpecException("Unsupported key specification: " + keySpec.getClass() + "."); }
private ASN1ObjectIdentifier getAlgorithmId(ASN1Object keyInfo) { if (keyInfo instanceof PrivateKeyInfo) { return ((PrivateKeyInfo) keyInfo).getPrivateKeyAlgorithm().getAlgorithm(); } else if (keyInfo instanceof SubjectPublicKeyInfo) { return ((SubjectPublicKeyInfo) keyInfo).getAlgorithm().getAlgorithm(); } else { throw new IllegalArgumentException("Asymmetric key expected but received: " + keyInfo.getClass().getName()); } }
info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()))), new DEROctetString(keyBytes)); info = new PrivateKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(keyBytes)); return info.getEncoded(ASN1Encoding.DER);
protected PrivateKey engineGeneratePrivate( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof PKCS8EncodedKeySpec) { try { PrivateKeyInfo info = PrivateKeyInfo.getInstance(((PKCS8EncodedKeySpec)keySpec).getEncoded()); PrivateKey key = BouncyCastleProvider.getPrivateKey(info); if (key != null) { return key; } throw new InvalidKeySpecException("no factory found for OID: " + info.getPrivateKeyAlgorithm().getAlgorithm()); } catch (Exception e) { throw new InvalidKeySpecException(e.toString()); } } throw new InvalidKeySpecException("Unknown KeySpec type: " + keySpec.getClass().getName()); }
public BCDSAPrivateKey( PrivateKeyInfo info) throws IOException { DSAParameter params = DSAParameter.getInstance(info.getPrivateKeyAlgorithm().getParameters()); ASN1Integer derX = (ASN1Integer)info.parsePrivateKey(); this.x = derX.getValue(); this.dsaSpec = new DSAParameterSpec(params.getP(), params.getQ(), params.getG()); }
private void readObject( ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); byte[] enc = (byte[])in.readObject(); init(PrivateKeyInfo.getInstance(enc)); }
public byte[] getEncoded() { try { ASN1Set attrSet = ASN1Set.getInstance(attributes); PrivateKeyInfo privInfo = PrivateKeyInfoFactory.createPrivateKeyInfo(xdhPrivateKey, attrSet); if (hasPublicKey) { return privInfo.getEncoded(); } else { return new PrivateKeyInfo(privInfo.getPrivateKeyAlgorithm(), privInfo.parsePrivateKey(), attrSet).getEncoded(); } } catch (IOException e) { return null; } }
throws IOException AlgorithmIdentifier algId = keyInfo.getAlgorithmId(); if (algId.getObjectId().equals(PKCSObjectIdentifiers.rsaEncryption)) RSAPrivateKeyStructure keyStructure = new RSAPrivateKeyStructure((ASN1Sequence)keyInfo.getPrivateKey()); keyStructure.getCoefficient()); else if (algId.getObjectId().equals(PKCSObjectIdentifiers.dhKeyAgreement)) DHParameter params = new DHParameter((ASN1Sequence)keyInfo.getAlgorithmId().getParameters()); DERInteger derX = (DERInteger)keyInfo.getPrivateKey(); else if (algId.getObjectId().equals(OIWObjectIdentifiers.elGamalAlgorithm)) ElGamalParameter params = new ElGamalParameter((ASN1Sequence)keyInfo.getAlgorithmId().getParameters()); DERInteger derX = (DERInteger)keyInfo.getPrivateKey(); DERInteger derX = (DERInteger)keyInfo.getPrivateKey(); DEREncodable de = keyInfo.getAlgorithmId().getParameters(); X962Parameters params = new X962Parameters((DERObject)keyInfo.getAlgorithmId().getParameters()); ECDomainParameters dParams = null; ECPrivateKeyStructure ec = new ECPrivateKeyStructure((ASN1Sequence)keyInfo.getPrivateKey());