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(
/** * 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)); }
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(
public static RSAPrivateKey getInstance( ASN1TaggedObject obj, boolean explicit) { return getInstance(ASN1Sequence.getInstance(obj, explicit)); }
/** * construct an RSA key from a private key info object. */ BCRSAPrivateCrtKey( PrivateKeyInfo info) throws IOException { this(RSAPrivateKey.getInstance(info.parsePrivateKey())); }
/** * 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 static RSAPrivateKey getInstance( ASN1TaggedObject obj, boolean explicit) { return getInstance(ASN1Sequence.getInstance(obj, explicit)); }
@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); }
@Override public RSAPrivateKeySpec readPrivateKey(byte[] privateKeyDerBytes) throws IOException { RSAPrivateKey asn1PrivKey = RSAPrivateKey.getInstance(ASN1Sequence.fromByteArray(privateKeyDerBytes)); return new RSAPrivateKeySpec(asn1PrivKey.getModulus(), asn1PrivKey.getPrivateExponent()); }
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(),
.getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent());
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"); } }
static AsymmetricKeyParameter loadPrivateKeyResource(String resource) throws IOException { PemObject pem = loadPemResource(resource); if (pem.getType().endsWith("RSA PRIVATE KEY")) { RSAPrivateKey rsa = RSAPrivateKey.getInstance(pem.getContent()); return new RSAPrivateCrtKeyParameters(rsa.getModulus(), rsa.getPublicExponent(), rsa.getPrivateExponent(), rsa.getPrime1(), rsa.getPrime2(), rsa.getExponent1(), rsa.getExponent2(), rsa.getCoefficient()); } if (pem.getType().endsWith("PRIVATE KEY")) { return PrivateKeyFactory.createKey(pem.getContent()); } throw new IllegalArgumentException("'resource' doesn't specify a valid private key"); }
static AsymmetricKeyParameter loadPrivateKeyResource(String resource) throws IOException { PemObject pem = loadPemResource(resource); if (pem.getType().endsWith("RSA PRIVATE KEY")) { RSAPrivateKey rsa = RSAPrivateKey.getInstance(pem.getContent()); return new RSAPrivateCrtKeyParameters(rsa.getModulus(), rsa.getPublicExponent(), rsa.getPrivateExponent(), rsa.getPrime1(), rsa.getPrime2(), rsa.getExponent1(), rsa.getExponent2(), rsa.getCoefficient()); } if (pem.getType().endsWith("PRIVATE KEY")) { return PrivateKeyFactory.createKey(pem.getContent()); } throw new IllegalArgumentException( "'resource' doesn't specify a valid private key"); }
static AsymmetricKeyParameter loadPrivateKeyResource(String resource) throws IOException { PemObject pem = loadPemResource(resource); if (pem.getType().endsWith("RSA PRIVATE KEY")) { RSAPrivateKey rsa = RSAPrivateKey.getInstance(pem.getContent()); return new RSAPrivateCrtKeyParameters(rsa.getModulus(), rsa.getPublicExponent(), rsa.getPrivateExponent(), rsa.getPrime1(), rsa.getPrime2(), rsa.getExponent1(), rsa.getExponent2(), rsa.getCoefficient()); } if (pem.getType().endsWith("PRIVATE KEY")) { return PrivateKeyFactory.createKey(pem.getContent()); } throw new IllegalArgumentException( "'resource' doesn't specify a valid private key"); }
RSAPrivateKey.getInstance(((PKCS8EncodedKeySpec)keySpec).getEncoded()));
RSAPrivateKey rsaPrivateKey = RSAPrivateKey.getInstance(sequence);
RSAPrivateKey keyStructure = RSAPrivateKey.getInstance(keyInfo.parsePrivateKey());
|| algOID.equals(X509ObjectIdentifiers.id_ea_rsa)) RSAPrivateKey keyStructure = RSAPrivateKey.getInstance(keyInfo.parsePrivateKey());