private static SubjectKeyIdentifier createSubjectKeyIdentifier(Key key) throws IOException { ByteArrayInputStream bIn = new ByteArrayInputStream(key.getEncoded()); ASN1InputStream is = null; try { is = new ASN1InputStream(bIn); ASN1Sequence seq = (ASN1Sequence) is.readObject(); SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(seq); return new BcX509ExtensionUtils().createSubjectKeyIdentifier(info); } finally { IOUtils.closeQuietly(is); } }
/** * 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)); }
/** * Create subjectKeyIdentifier * The Subject Key Identifier extension identifies the public key certified by this certificate. * This extension provides a way of distinguishing public keys if more than one is available for * a given subject name. * i.e. * Identifier: Subject Key Identifier - 2.5.29.14 * Critical: no * Key Identifier: * 3B:46:83:85:27:BC:F5:9D:8E:63:E3:BE:79:EF:AF:79: * 9C:37:85:84 * * */ protected SubjectKeyIdentifier createSubjectKeyIdentifier(PublicKey publicKey) throws IOException { try (ByteArrayInputStream bais = new ByteArrayInputStream(publicKey.getEncoded()); ASN1InputStream ais = new ASN1InputStream(bais)) { ASN1Sequence asn1Sequence = (ASN1Sequence) ais.readObject(); SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo(asn1Sequence); return new BcX509ExtensionUtils().createSubjectKeyIdentifier(subjectPublicKeyInfo); } }
public static byte[] getEncodedSubjectPublicKeyInfo(AlgorithmIdentifier algId, ASN1Encodable keyData) { try { return getEncodedSubjectPublicKeyInfo(new SubjectPublicKeyInfo(algId, keyData)); } catch (Exception e) { return null; } }
public static byte[] getEncodedSubjectPublicKeyInfo(AlgorithmIdentifier algId, byte[] keyData) { try { return getEncodedSubjectPublicKeyInfo(new SubjectPublicKeyInfo(algId, keyData)); } catch (Exception e) { return null; } }
public static byte[] getEncodedSubjectPublicKeyInfo(AlgorithmIdentifier algId, byte[] keyData) { try { return getEncodedSubjectPublicKeyInfo(new SubjectPublicKeyInfo(algId, keyData)); } catch (Exception e) { return null; } }
public static byte[] getEncodedSubjectPublicKeyInfo(AlgorithmIdentifier algId, ASN1Encodable keyData) { try { return getEncodedSubjectPublicKeyInfo(new SubjectPublicKeyInfo(algId, keyData)); } catch (Exception e) { return null; } }
public static SubjectPublicKeyInfo getInstance( Object obj) { if (obj instanceof SubjectPublicKeyInfo) { return (SubjectPublicKeyInfo)obj; } else if (obj != null) { return new SubjectPublicKeyInfo(ASN1Sequence.getInstance(obj)); } return null; }
private SubjectPublicKeyInfo getPublicKeyInfo() throws IOException { final ByteArrayInputStream bIn = new ByteArrayInputStream(keyPair.getPublic().getEncoded()); final ASN1InputStream dIn = new ASN1InputStream(bIn); return new SubjectPublicKeyInfo((ASN1Sequence) dIn.readObject()); }
public static SubjectPublicKeyInfo createSubjectPublicKeyInfo(PublicKey publicKey) throws IOException { requireNonNull("publicKey", publicKey); if (publicKey instanceof java.security.interfaces.RSAPublicKey) { java.security.interfaces.RSAPublicKey rsaPubKey = (java.security.interfaces.RSAPublicKey) publicKey; return new SubjectPublicKeyInfo(ALGID_RSA, new RSAPublicKey(rsaPubKey.getModulus(), rsaPubKey.getPublicExponent())); } else { throw new IllegalArgumentException("unsupported public key " + publicKey); } }
protected static MyKeypair generateRsaKeypair() throws Exception { KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA"); kpGen.initialize(2048); KeyPair kp = kpGen.generateKeyPair(); RSAPublicKey pubKey = (RSAPublicKey) kp.getPublic(); SubjectPublicKeyInfo subjectPublicKeyInfo = new SubjectPublicKeyInfo( new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new org.bouncycastle.asn1.pkcs.RSAPublicKey(pubKey.getModulus(), pubKey.getPublicExponent())); return new MyKeypair(kp.getPrivate(), subjectPublicKeyInfo); }
@SuppressWarnings("resource") static AuthorityKeyIdentifier createAuthorityKeyId( PublicKey pub) throws IOException { ByteArrayInputStream bIn = new ByteArrayInputStream(pub.getEncoded()); SubjectPublicKeyInfo info = new SubjectPublicKeyInfo( (ASN1Sequence)new ASN1InputStream(bIn).readObject()); return new AuthorityKeyIdentifier(info); }
@SuppressWarnings("resource") static SubjectKeyIdentifier createSubjectKeyId( PublicKey pub) throws IOException { ByteArrayInputStream bIn = new ByteArrayInputStream(pub.getEncoded()); SubjectPublicKeyInfo info = new SubjectPublicKeyInfo( (ASN1Sequence)new ASN1InputStream(bIn).readObject()); return new SubjectKeyIdentifier(info); }
private KeyPairWithSubjectPublicKeyInfo genRSAKeypair(final int keysize, final BigInteger publicExponent, final SecureRandom random) throws Exception { KeyPair kp = KeyUtil.generateRSAKeypair(keysize, publicExponent, random); java.security.interfaces.RSAPublicKey rsaPubKey = (java.security.interfaces.RSAPublicKey) kp.getPublic(); SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo( new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(rsaPubKey.getModulus(), rsaPubKey.getPublicExponent())); return new KeyPairWithSubjectPublicKeyInfo(kp, spki); }
@Override public AuthorityKeyIdentifier buildAuthorityKeyIdentifier( PublicKey publicKey ) { try { ByteArrayInputStream octets = new ByteArrayInputStream( publicKey.getEncoded() ); SubjectPublicKeyInfo apki = new SubjectPublicKeyInfo( ( ASN1Sequence ) new ASN1InputStream( octets ).readObject() ); return new AuthorityKeyIdentifier( apki ); } catch ( IOException ex ) { throw new CryptoFailure( "Unable to build AuthorityKeyIdentifier", ex ); } }
private KeyPairWithSubjectPublicKeyInfo genRSAKeypair(int keysize, BigInteger publicExponent, SecureRandom random) throws Exception { KeyPair kp = KeyUtil.generateRSAKeypair(keysize, publicExponent, random); java.security.interfaces.RSAPublicKey rsaPubKey = (java.security.interfaces.RSAPublicKey) kp.getPublic(); SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo( new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(rsaPubKey.getModulus(), rsaPubKey.getPublicExponent())); return new KeyPairWithSubjectPublicKeyInfo(kp, spki); }
@Override public SubjectKeyIdentifier buildSubjectKeyIdentifier( PublicKey publicKey ) { try { ByteArrayInputStream octets = new ByteArrayInputStream( publicKey.getEncoded() ); SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo( ( ASN1Sequence ) new ASN1InputStream( octets ).readObject() ); return new SubjectKeyIdentifier( spki ); } catch ( IOException ex ) { throw new CryptoFailure( "Unable to build SubjectKeyIdentifier", ex ); } }
@Override public byte[] getEncoded() { ASN1OctetString p = ASN1OctetString.getInstance( new X9ECPoint(getQ(), withCompression).toASN1Primitive()); // stored curve is null if ImplicitlyCa SubjectPublicKeyInfo info = new SubjectPublicKeyInfo( new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, ID_SM2_PUBKEY_PARAM), p.getOctets()); return KeyUtil.getEncodedSubjectPublicKeyInfo(info); }
public byte[] getEncoded() { ASN1Encodable params = ECUtils.getDomainParametersFromName(ecSpec, withCompression); ASN1OctetString p = ASN1OctetString.getInstance(new X9ECPoint(ecPublicKey.getQ(), withCompression).toASN1Primitive()); // stored curve is null if ImplicitlyCa SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params), p.getOctets()); return KeyUtil.getEncodedSubjectPublicKeyInfo(info); }
public byte[] getEncoded() { try { SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(OIWObjectIdentifiers.elGamalAlgorithm, new ElGamalParameter(elSpec.getP(), elSpec.getG())), new ASN1Integer(y)); return info.getEncoded(ASN1Encoding.DER); } catch (IOException e) { return null; } }