Refine search
KeyFactory fact = KeyFactory.getInstance("RSA"); if (type.equals("RSA PRIVATE KEY")) { ASN1Sequence seq = ASN1Sequence.getInstance(content); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec( key.getModulus(), key.getPublicExponent(), key.getCoefficient() ); publicKey = fact.generatePublic(pubSpec); privateKey = fact.generatePrivate(privSpec); } else if (type.equals("PUBLIC KEY")) { KeySpec keySpec = new X509EncodedKeySpec(content); ASN1Sequence seq = ASN1Sequence.getInstance(content); org.bouncycastle.asn1.pkcs.RSAPublicKey key = org.bouncycastle.asn1.pkcs.RSAPublicKey.getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); publicKey = fact.generatePublic(pubSpec); } else {
// key pair is in 'kp' KeyFactory kf = KeyFactory.getInstance("RSA"); RSAPrivateCrtKeySpec ks = kf.getKeySpec( kp.getPrivate(), RSAPrivateCrtKeySpec.class); System.out.println("<RSAKeyValue>"); System.out.println(" <Modulus>" + ks.getModulus() + "</Modulus>"); System.out.println(" <Exponent>" + ks.getPublicExponent() + "</Exponent>"); System.out.println(" <P>" + ks.getPrimeP() + "</P>"); System.out.println(" <Q>" + ks.getPrimeQ() + "</Q>"); System.out.println(" <DP>" + ks.getPrimeExponentP() + "</DP>"); System.out.println(" <DQ>" + ks.getPrimeExponentQ() + "</DQ>"); System.out.println(" <InverseQ>" + ks.getCrtCoefficient() + "</InverseQ>"); System.out.println(" <D>" + ks.getPrivateExponent() + "</D>"); System.out.println("</RSAKeyValue>");
public static PrivateKey getPrivateKey(String priKeyData) throws Exception { /* * n:512 e:512 d:512 p:256 q:256 dmp1:256 dmq1:256 iqmp:256 */ BigInteger modulus = new BigInteger(priKeyData.substring(8, 512 + 8), 16); BigInteger publicExponent = new BigInteger(priKeyData.substring( 512 + 8, 512 + 8 + 512), 16); BigInteger privateExponent = new BigInteger(priKeyData.substring( 512 + 8 + 512, 512 + 8 + 512 + 512), 16); BigInteger primeP = new BigInteger(priKeyData.substring( 512 + 8 + 512 + 512, 512 + 8 + 512 + 512 + 256), 16); BigInteger primeQ = new BigInteger(priKeyData.substring(512 + 8 + 512 + 512 + 256, 512 + 8 + 512 + 512 + 256 + 256), 16); BigInteger primeExponentP = new BigInteger( priKeyData.substring(512 + 8 + 512 + 512 + 256 + 256, 512 + 8 + 512 + 512 + 256 + 256 + 256), 16); BigInteger primeExponentQ = new BigInteger(priKeyData.substring(512 + 8 + 512 + 512 + 256 + 256 + 256, 512 + 8 + 512 + 512 + 256 + 256 + 256 + 256), 16); BigInteger crtCoefficient = new BigInteger(priKeyData.substring(512 + 8 + 512 + 512 + 256 + 256 + 256 + 256, 512 + 8 + 512 + 512 + 256 + 256 + 256 + 256 + 256), 16); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPrivateCrtKeySpec rsaPrivateKeySpec = new RSAPrivateCrtKeySpec( modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient); return keyFactory.generatePrivate(rsaPrivateKeySpec); }
BigInteger q = getMPInt(); BigInteger p = getMPInt(); BigInteger dP = d.remainder(p.subtract(BigInteger.valueOf(1))); BigInteger dQ = d.remainder(q.subtract(BigInteger.valueOf(1))); KeyFactory keyFactory = SecurityUtils.getKeyFactory(KeyUtils.RSA_ALGORITHM); pub = keyFactory.generatePublic(new RSAPublicKeySpec(n, e)); prv = keyFactory.generatePrivate(new RSAPrivateCrtKeySpec(n, e, d, p, q, dP, dQ, qInv)); } else if (KeyPairProvider.SSH_DSS.equals(keyAlg)) { BigInteger p = getMPInt(); BigInteger x = getMPInt(); KeyFactory keyFactory = SecurityUtils.getKeyFactory(KeyUtils.DSS_ALGORITHM); pub = keyFactory.generatePublic(new DSAPublicKeySpec(y, p, q, g)); prv = keyFactory.generatePrivate(new DSAPrivateKeySpec(x, p, q, g)); } else if (KeyPairProvider.SSH_ED25519.equals(keyAlg)) {
/** * construct a private key from an RSAPrivateCrtKeySpec * * @param spec the spec to be used in construction. */ JCERSAPrivateCrtKey( RSAPrivateCrtKeySpec spec) { this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); this.privateExponent = spec.getPrivateExponent(); this.primeP = spec.getPrimeP(); this.primeQ = spec.getPrimeQ(); this.primeExponentP = spec.getPrimeExponentP(); this.primeExponentQ = spec.getPrimeExponentQ(); this.crtCoefficient = spec.getCrtCoefficient(); }
@Override KeyPair generateKeyPair(TypesReader typesReader) throws GeneralSecurityException, IOException { BigInteger n = typesReader.readMPINT(); BigInteger e = typesReader.readMPINT(); BigInteger d = typesReader.readMPINT(); BigInteger c = typesReader.readMPINT(); BigInteger p = typesReader.readMPINT(); RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(n, e); RSAPrivateKeySpec privateKeySpec; if (null == p || null == c) { privateKeySpec = new RSAPrivateKeySpec(n, d); } else { BigInteger q = c.modInverse(p); BigInteger pE = d.mod(p.subtract(BigInteger.ONE)); BigInteger qE = d.mod(q.subtract(BigInteger.ONE)); privateKeySpec = new RSAPrivateCrtKeySpec(n, e, d, p, q, pE, qE, c); } KeyFactory factory = KeyFactory.getInstance("RSA"); return new KeyPair(factory.generatePublic(publicKeySpec), factory.generatePrivate(privateKeySpec)); } });
if (!BigInteger.ZERO.equals(version)) { throw new StreamCorruptedException("Multi-primes N/A"); PublicKey pubKey = kf.generatePublic(new RSAPublicKeySpec(modulus, publicExp)); BigInteger primeExponentQ = parser.readObject().asInteger(); BigInteger crtCoef = parser.readObject().asInteger(); RSAPrivateKeySpec prvSpec = new RSAPrivateCrtKeySpec( modulus, publicExp, privateExp, primeP, primeQ, primeExponentP, primeExponentQ, crtCoef); PrivateKey prvKey = kf.generatePrivate(prvSpec); return new KeyPair(pubKey, prvKey);
KeyFactory factory = KeyFactory.getInstance("RSA"); return factory.generatePrivate(new PKCS8EncodedKeySpec(pkcs8EncodedKey)); BigInteger crtCoef = seq[8].getBigInteger(); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); KeyFactory factory = KeyFactory.getInstance("RSA"); return factory.generatePrivate(keySpec);
BigInteger crtCoefficient = ((ASN1Integer) seq.getObjectAt(8)).getValue(); if (!version.equals(VERSION)) { throw new CryptoException(MessageFormat.format( res.getString("OpenSslVersionIncorrect.exception.message"), "" + VERSION.intValue(), "" + version.intValue())); RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponenetQ, crtCoefficient); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(rsaPrivateCrtKeySpec); } else if (seq.size() == 6) { // DSA private key generatorG); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); return keyFactory.generatePrivate(dsaPrivateKeySpec); } else if (seq.size() >= 2) { // EC private key (RFC 5915)
@Override public Collection<KeyPair> loadKeyPairs(String resourceKey, PuttyKeyReader pubReader, PuttyKeyReader prvReader) throws IOException, GeneralSecurityException { pubReader.skip(); // skip version KeyFactory kf = SecurityUtils.getKeyFactory(KeyUtils.RSA_ALGORITHM); BigInteger publicExp = pubReader.readInt(); BigInteger modulus = pubReader.readInt(); PublicKey pubKey = kf.generatePublic(new RSAPublicKeySpec(modulus, publicExp)); BigInteger privateExp = prvReader.readInt(); BigInteger primeP = prvReader.readInt(); BigInteger primeQ = prvReader.readInt(); BigInteger crtCoef = prvReader.readInt(); BigInteger primeExponentP = privateExp.mod(primeP.subtract(BigInteger.ONE)); BigInteger primeExponentQ = privateExp.mod(primeQ.subtract(BigInteger.ONE)); RSAPrivateKeySpec prvSpec = new RSAPrivateCrtKeySpec( modulus, publicExp, privateExp, primeP, primeQ, primeExponentP, primeExponentQ, crtCoef); PrivateKey prvKey = kf.generatePrivate(prvSpec); return Collections.singletonList(new KeyPair(pubKey, prvKey)); } }
static RSAPrivateCrtKeySpec decodeRSAPrivateKey(byte[] bytes) { List<Object> seq = createASN1Sequence(bytes); checkArgument(seq.size() >= 9, "not enough elements (%s) for a private key", seq.size(), seq); int version = bigIntAt(seq, 0).intValue(); checkArgument(version == 0 || version == 1, "wrong version %s for RSA private key", version); return new RSAPrivateCrtKeySpec(bigIntAt(seq, 1), bigIntAt(seq, 2), bigIntAt(seq, 3), bigIntAt(seq, 4), bigIntAt( seq, 5), bigIntAt(seq, 6), bigIntAt(seq, 7), bigIntAt(seq, 8)); }
/** * @return true if the keypairs match */ public static boolean privateKeyMatchesPublicKey(RSAPrivateCrtKeySpec privateKey, RSAPublicKeySpec publicKey) { return privateKey.getPublicExponent().equals(publicKey.getPublicExponent()) && privateKey.getModulus().equals(publicKey.getModulus()); }
return new RSAPublicKeySpec(k.getModulus(), k.getPublicExponent()); return new RSAPrivateCrtKeySpec( k.getModulus(), k.getPublicExponent(), k.getPrivateExponent(),
BigInteger firstCrtCoeff = asBigInteger(json, PARAM_FIRST_CRT_COEFF, "RSA first CRT coefficient"); try { return kf.generatePrivate(new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, .orElseGet(() -> { try { return kf.generatePrivate(new RSAPrivateKeySpec(modulus, privateExponent)); } catch (InvalidKeySpecException e) { throw new JwtException("Failed to generate private key based on modulus and private exponent");
BigInteger crtCoef = parser.read().getInteger(); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec( modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef);
private void checkPrivateKey(final RSAPrivateCrtKeySpec spec) { final List<String> missing = new ArrayList<>(); if (spec.getPrivateExponent() == null) missing.add("d"); if (spec.getPrimeP() == null) missing.add("p"); if (spec.getPrimeQ() == null) missing.add("q"); if (spec.getPrimeExponentP() == null) missing.add("dp"); if (spec.getPrimeExponentQ() == null) missing.add("dq"); if (spec.getCrtCoefficient() == null) missing.add("qi"); /** * We want them to supply either all or none of the private key data */ if (missing.size() == 6) return; // they've supplied none - good if (missing.size() == 0) return; // they've supplied all - good /** * They supplied just some. This doesn't work and isn't likely what they want */ throw new InvalidJwkKeySpecException("rsa", missing); }
/** * @return true if the keypair has the same fingerprint as supplied */ public static boolean privateKeyHasFingerprint(RSAPrivateCrtKeySpec privateKey, String fingerprint) { return fingerprint(privateKey.getPublicExponent(), privateKey.getModulus()).equals(fingerprint); }
BigInteger publicExponent = new BigInteger(String.valueOf(buffer.getInt())); BigInteger privateExponent = getBigInteger(buffer, l); RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient); KeyFactory factory = KeyFactory.getInstance("RSA"); return factory.generatePrivate(spec);
BigInteger q = getMPInt(); BigInteger p = getMPInt(); BigInteger dP = d.remainder(p.subtract(BigInteger.valueOf(1))); BigInteger dQ = d.remainder(q.subtract(BigInteger.valueOf(1))); KeyFactory keyFactory = SecurityUtils.getKeyFactory(KeyUtils.RSA_ALGORITHM); pub = keyFactory.generatePublic(new RSAPublicKeySpec(n, e)); prv = keyFactory.generatePrivate(new RSAPrivateCrtKeySpec(n, e, d, p, q, dP, dQ, qInv)); } else if (KeyPairProvider.SSH_DSS.equals(keyAlg)) { BigInteger p = getMPInt(); BigInteger x = getMPInt(); KeyFactory keyFactory = SecurityUtils.getKeyFactory(KeyUtils.DSS_ALGORITHM); pub = keyFactory.generatePublic(new DSAPublicKeySpec(y, p, q, g)); prv = keyFactory.generatePrivate(new DSAPrivateKeySpec(x, p, q, g)); } else if (KeyPairProvider.SSH_ED25519.equals(keyAlg)) {
/** * construct a private key from an RSAPrivateCrtKeySpec * * @param spec the spec to be used in construction. */ JCERSAPrivateCrtKey( RSAPrivateCrtKeySpec spec) { this.modulus = spec.getModulus(); this.publicExponent = spec.getPublicExponent(); this.privateExponent = spec.getPrivateExponent(); this.primeP = spec.getPrimeP(); this.primeQ = spec.getPrimeQ(); this.primeExponentP = spec.getPrimeExponentP(); this.primeExponentQ = spec.getPrimeExponentQ(); this.crtCoefficient = spec.getCrtCoefficient(); }