List of usage examples for org.bouncycastle.asn1.x509 SubjectPublicKeyInfo getPublicKey
public ASN1Primitive getPublicKey() throws IOException
From source file:ElGamalPublicPGKey.java
License:Open Source License
public ElGamalPublicPGKey(SubjectPublicKeyInfo info) { ElGamalParameter params = new ElGamalParameter((ASN1Sequence) info.getAlgorithmId().getParameters()); DERInteger derY = null;//from w w w . j ava 2s . c o m try { derY = (DERInteger) info.getPublicKey(); } catch (IOException e) { throw new IllegalArgumentException("invalid info structure in DSA public key"); } this.y = derY.getValue(); this.elSpec = new ElGamalParameterSpec(params.getP(), params.getG()); }
From source file:de.thiemann.ssl.report.model.CertificateV3.java
License:Open Source License
private PubKeyInfo transferPublicKeyInfo(byte[] encodedPublicKey) { PubKeyInfo info = new PubKeyInfo(); try {// w w w. java 2s. c o m SubjectPublicKeyInfo subPubKeyInfo = new SubjectPublicKeyInfo( (ASN1Sequence) ASN1Object.fromByteArray(encodedPublicKey)); String asn1PubKeyId = subPubKeyInfo.getAlgorithmId().getAlgorithm().getId(); if (asn1PubKeyId.equals(ASN1PublicKeyIds.RSA.getOid())) { DERSequence seq = (DERSequence) subPubKeyInfo.getPublicKey(); ASN1Integer iModulus = (ASN1Integer) seq.getObjectAt(0); BigInteger modulus = iModulus.getPositiveValue(); info.pubKeyAlgorithm = ASN1PublicKeyIds.RSA.name(); info.pubKeySize = modulus.bitLength(); } else if (asn1PubKeyId.equals(ASN1PublicKeyIds.DSA.getOid())) { info.pubKeyAlgorithm = ASN1PublicKeyIds.DSA.name(); } else if (asn1PubKeyId.equals(ASN1PublicKeyIds.Diffie_Hellman.getOid())) { info.pubKeyAlgorithm = ASN1PublicKeyIds.Diffie_Hellman.name(); } else if (asn1PubKeyId.equals(ASN1PublicKeyIds.KEA.getOid())) { info.pubKeyAlgorithm = ASN1PublicKeyIds.KEA.name(); } else if (asn1PubKeyId.equals(ASN1PublicKeyIds.ECDH.getOid())) { info.pubKeyAlgorithm = ASN1PublicKeyIds.ECDH.name(); } else info.pubKeyAlgorithm = "Unknown public key! OID: " + asn1PubKeyId; } catch (IOException e) { e.printStackTrace(); } return info; }
From source file:io.aos.crypto.spl05.X509EncodedKeySpecExample.java
License:Apache License
public static void main(String[] args) throws Exception { // create the keys KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(128, Utils.createFixedRandom()); KeyPair pair = generator.generateKeyPair(); // dump public key ASN1InputStream aIn = new ASN1InputStream(pair.getPublic().getEncoded()); SubjectPublicKeyInfo info = SubjectPublicKeyInfo.getInstance(aIn.readObject()); System.out.println(ASN1Dump.dumpAsString(info)); System.out.println(ASN1Dump.dumpAsString(info.getPublicKey())); // create from specification X509EncodedKeySpec x509Spec = new X509EncodedKeySpec(pair.getPublic().getEncoded()); KeyFactory keyFact = KeyFactory.getInstance("RSA", "BC"); PublicKey pubKey = keyFact.generatePublic(x509Spec); if (pubKey.equals(pair.getPublic())) { System.out.println("key recovery successful"); } else {/*from w w w.ja va 2 s .com*/ System.out.println("key recovery failed"); } }
From source file:org.jclouds.crypto.pem.PKCS1EncodedPublicKeySpec.java
License:Apache License
/** * Decode PKCS#1 encoded public key into RSAPublicKeySpec. * <p>//from w w w . j ava 2 s . c om * Keys here can be in two different formats. They can have the algorithm * encoded, or they can have only the modulus and the public exponent. * <p> * The latter is not a valid PEM encoded file, but it is a valid DER encoded * RSA key, so this method should also support it. * * @param keyBytes * Encoded PKCS#1 rsa key. */ private void decode(final byte[] keyBytes) throws IOException { RSAPublicKeyStructure pks = null; ASN1Sequence seq = (ASN1Sequence) ASN1Object.fromByteArray(keyBytes); try { // Try to parse the public key normally. If the algorithm is not // present in the encoded key, an IllegalArgumentException will be // raised. SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(seq); pks = new RSAPublicKeyStructure((ASN1Sequence) info.getPublicKey()); } catch (IllegalArgumentException ex) { // If the algorithm is not found in the encoded key, try to extract // just the modulus and the public exponent to build the public key. pks = new RSAPublicKeyStructure(seq); } keySpec = new RSAPublicKeySpec(pks.getModulus(), pks.getPublicExponent()); }
From source file:org.opensc.pkcs15.asn1.attr.RSAPublicKeyChoice.java
License:Apache License
public RSAPublicKeyChoice(SubjectPublicKeyInfo spki) { this.spki = spki; try {/* w w w . j a v a2 s. c om*/ RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence) spki.getPublicKey()); this.raw = new RSAPublicKeyStructure(pubKey.getModulus(), pubKey.getPublicExponent()); } catch (IOException e) { throw new IllegalArgumentException("invalid info structure in RSA public key"); } }