throw new IllegalArgumentException("Invalid RSA Private Key ASN1 sequence."); org.bouncycastle.asn1.pkcs.RSAPrivateKey key = org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec( key.getModulus(), key.getPublicExponent(), key.getPrivateExponent(), key.getPrime1(), key.getPrime2(), key.getExponent1(), key.getExponent2(), key.getCoefficient() ); publicKey = fact.generatePublic(pubSpec);
/** * 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)); }
@Override public RSAPrivateKeySpec readPrivateKey(byte[] privateKeyDerBytes) throws IOException { RSAPrivateKey asn1PrivKey = RSAPrivateKey.getInstance(ASN1Sequence.fromByteArray(privateKeyDerBytes)); return new RSAPrivateKeySpec(asn1PrivKey.getModulus(), asn1PrivKey.getPrivateExponent()); }
public static RSAPrivateKey getInstance( ASN1TaggedObject obj, boolean explicit) { return getInstance(ASN1Sequence.getInstance(obj, explicit)); }
BCRSAPrivateKey(org.bouncycastle.asn1.pkcs.RSAPrivateKey key) { this.modulus = key.getModulus(); this.privateExponent = key.getPrivateExponent(); }
public static RSAPrivateKey getInstance( Object obj) { if (obj instanceof RSAPrivateKey) { return (RSAPrivateKey)obj; } if (obj != null) { return new RSAPrivateKey(ASN1Sequence.getInstance(obj)); } return null; }
public PrivateKey generatePrivate(PrivateKeyInfo keyInfo) throws IOException { ASN1ObjectIdentifier algOid = keyInfo.getPrivateKeyAlgorithm().getAlgorithm(); if (RSAUtil.isRsaOid(algOid)) { RSAPrivateKey rsaPrivKey = RSAPrivateKey.getInstance(keyInfo.parsePrivateKey()); if (rsaPrivKey.getCoefficient().intValue() == 0) { return new BCRSAPrivateKey(rsaPrivKey); } else { return new BCRSAPrivateCrtKey(keyInfo); } } else { throw new IOException("algorithm identifier " + algOid + " in key not recognised"); } }
@SneakyThrows public static RSAPrivateKey privateKeyPkcs1(final String pkcs1) { final String withoutComment = dropComment(pkcs1, COMMENT_MARK); final byte[] raw = CodecUtils.decodeBase64(withoutComment); if (log.isDebugEnabled()) { log.debug("withoutComment: {}", withoutComment); log.debug("raw: {}", Hex.encodeHexString(raw)); } final ASN1Primitive asn1Primitive = ASN1Sequence.fromByteArray(raw); // final RSAPrivateKeyStructure asn1 = new RSAPrivateKeyStructure((ASN1Sequence) asn1Primitive); final org.bouncycastle.asn1.pkcs.RSAPrivateKey asn1 = org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(asn1Primitive); final RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(asn1.getModulus(), asn1.getPrivateExponent()); return (RSAPrivateKey) KeyFactory.getInstance(ALGO_RSA).generatePrivate(rsaPrivateKeySpec); }
/** * construct an RSA key from a private key info object. */ BCRSAPrivateCrtKey( PrivateKeyInfo info) throws IOException { this(RSAPrivateKey.getInstance(info.parsePrivateKey())); }
public static RSAPrivateKey getInstance( Object obj) { if (obj instanceof RSAPrivateKey) { return (RSAPrivateKey)obj; } if (obj != null) { return new RSAPrivateKey(ASN1Sequence.getInstance(obj)); } return null; }
throw new IllegalArgumentException("Invalid RSA Private Key ASN1 sequence."); org.bouncycastle.asn1.pkcs.RSAPrivateKey key = org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec( key.getModulus(), key.getPublicExponent(), key.getPrivateExponent(), key.getPrime1(), key.getPrime2(), key.getExponent1(), key.getExponent2(), key.getCoefficient() ); publicKey = fact.generatePublic(pubSpec);
/** * construct an RSA key from a private key info object. */ JCERSAPrivateCrtKey( PrivateKeyInfo info) throws IOException { this(org.bouncycastle.asn1.pkcs.RSAPrivateKey.getInstance(info.parsePrivateKey())); }
public byte[] getEncoded() { return KeyUtil.getEncodedPrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new org.bouncycastle.asn1.pkcs.RSAPrivateKey(getModulus(), ZERO, getPrivateExponent(), ZERO, ZERO, ZERO, ZERO, ZERO)); }
/** * construct an RSA key from a ASN.1 RSA private key object. */ BCRSAPrivateCrtKey( RSAPrivateKey key) { this.modulus = key.getModulus(); this.publicExponent = key.getPublicExponent(); this.privateExponent = key.getPrivateExponent(); this.primeP = key.getPrime1(); this.primeQ = key.getPrime2(); this.primeExponentP = key.getExponent1(); this.primeExponentQ = key.getExponent2(); this.crtCoefficient = key.getCoefficient(); }
public static RSAPrivateKey getInstance( ASN1TaggedObject obj, boolean explicit) { return getInstance(ASN1Sequence.getInstance(obj, explicit)); }
public byte[] getEncoded() { return KeyUtil.getEncodedPrivateKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, DERNull.INSTANCE), new org.bouncycastle.asn1.pkcs.RSAPrivateKey(getModulus(), ZERO, getPrivateExponent(), ZERO, ZERO, ZERO, ZERO, ZERO)); }
/** * construct an RSA key from a ASN.1 RSA private key object. */ JCERSAPrivateCrtKey( RSAPrivateKey key) { this.modulus = key.getModulus(); this.publicExponent = key.getPublicExponent(); this.privateExponent = key.getPrivateExponent(); this.primeP = key.getPrime1(); this.primeQ = key.getPrime2(); this.primeExponentP = key.getExponent1(); this.primeExponentQ = key.getExponent2(); this.crtCoefficient = key.getCoefficient(); }
RSAPrivateKey.getInstance(((PKCS8EncodedKeySpec)keySpec).getEncoded()));
RSAPrivateKey privateKey = new RSAPrivateKey( rsaPrivateKey.getModulus(), rsaPrivateKey.getPublicExponent(), rsaPrivateKey.getPrivateExponent(), rsaPrivateKey.getPrimeP(),
private AsymmetricBlockCipher getAsymmetricCipher( SecurityAlgorithm algorithm, RSAPrivateKey privateKey) throws ServiceResultException { CipherParameters params = new RSAPrivateCrtKeyParameters( privateKey.getModulus(), privateKey.getPublicExponent(), privateKey.getPrivateExponent(), privateKey.getPrime1(), privateKey.getPrime2(), privateKey.getExponent1(), privateKey.getExponent2(), privateKey.getCoefficient()); return getAsymmetricCipher(false, algorithm, params); }