SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(PublicKeyFactory.createKey(keyPair.getPublic() .getEncoded())); X509ExtensionUtils extensionUtils = new BcX509ExtensionUtils();
extractBytes(encKey, encKeySize / 2, 0, bX); extractBytes(encKey, encKeySize / 2, offset, bY);
@Override public byte[] getEncoded() { try { return SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(parameters).getEncoded(); } catch (IOException e) { return null; } } }
/** * Convert public key parameter to subject public key info. * * @param publicKey the public key to convert. * @return a subject public key info. */ public static SubjectPublicKeyInfo getSubjectPublicKeyInfo(PublicKeyParameters publicKey) { try { if (publicKey instanceof BcPublicKeyParameters) { return ((BcPublicKeyParameters) publicKey).getSubjectPublicKeyInfo(); } else { return SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(getAsymmetricKeyParameter(publicKey)); } } catch (IOException e) { // Very unlikely throw new IllegalArgumentException("Invalid public key, unable to get subject info."); } }
public static PublicKey getRSAPublicKey(byte[] encodedPubKey) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { RSAPublicKey pubKey8 = RSAPublicKey.getInstance(encodedPubKey); SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(new RSAKeyParameters(false, pubKey8.getModulus(), pubKey8.getPublicExponent())); X509EncodedKeySpec spec = new X509EncodedKeySpec(info.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(spec); }
public static PublicKey getRSAPublicKey(byte[] encodedPubKey) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { RSAPublicKey pubKey8 = RSAPublicKey.getInstance(encodedPubKey); SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(new RSAKeyParameters(false, pubKey8.getModulus(), pubKey8.getPublicExponent())); X509EncodedKeySpec spec = new X509EncodedKeySpec(info.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(spec); }
@Override public PublicKey toKey(PublicKeyParameters key) { try { // Optimization if (key instanceof BcAsymmetricKeyParameters) { String keyType = checkKeyType((BcAsymmetricKeyParameters) key); if (keyType != null) { throw new IllegalArgumentException(String.format(CLASS_ERROR, keyType, PUBLIC, key.getClass().getName())); } return generatePublic( SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo( ((BcAsymmetricKeyParameters) key).getParameters())); } // Fallback return generatePublic(SubjectPublicKeyInfo.getInstance(key.getEncoded())); } catch (IOException e) { throw new IllegalArgumentException("Invalid public key parameters: " + key.getClass().getName()); } }
private PKCS10CertificationRequest generateCertificationRequest(String dn, KeyPair kp) throws Exception{ X500Name subject=new X500Name(dn); PublicKey pubKey=kp.getPublic(); PrivateKey privKey=kp.getPrivate(); AsymmetricKeyParameter pubkeyParam = PublicKeyFactory.createKey(pubKey.getEncoded()); SubjectPublicKeyInfo publicKeyInfo=SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(pubkeyParam); PKCS10CertificationRequestBuilder builder=new PKCS10CertificationRequestBuilder(subject, publicKeyInfo); AlgorithmIdentifier signatureAi = new AlgorithmIdentifier(OIWObjectIdentifiers.sha1WithRSA); BcRSAContentSignerBuilder signerBuilder=new BcRSAContentSignerBuilder( signatureAi, AlgorithmIdentifier.getInstance(OIWObjectIdentifiers.idSHA1)); AsymmetricKeyParameter pkParam = PrivateKeyFactory.createKey(privKey.getEncoded()); ContentSigner signer=signerBuilder.build(pkParam); return builder.build(signer); } }
/** * @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); } } }
.createSubjectPublicKeyInfo( keyPair.getPublic())); AlgorithmIdentifier sigAlgId
/** * @return parameters converted to BC subject public key info structure. * @throws IOException on error. */ public SubjectPublicKeyInfo getSubjectPublicKeyInfo() throws IOException { if (this.parameters instanceof RSAKeyParameters) { RSAKeyParameters params = (RSAKeyParameters) this.parameters; return new SubjectPublicKeyInfo( new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new RSAPublicKey(params.getModulus(), params.getExponent())); } else if (this.parameters instanceof DSAPublicKeyParameters) { DSAPublicKeyParameters params = (DSAPublicKeyParameters) this.parameters; DSAParameters dsaParams = params.getParameters(); DSAParameter algParams = null; if (dsaParams != null) { algParams = new DSAParameter(dsaParams.getP(), dsaParams.getQ(), dsaParams.getG()); } return new SubjectPublicKeyInfo( new AlgorithmIdentifier(X9ObjectIdentifiers.id_dsa, algParams), new ASN1Integer(params.getY())); } else { // Fallback to Bouncy Castle, not sure it will do anything useful however. return SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(this.parameters); } }