public void setSigningKey(String key) throws Exception { this.signingKey = key; key = key.trim(); key = key.replace("-----BEGIN RSA PRIVATE KEY-----\n", "") .replace("-----END RSA PRIVATE KEY-----", "").trim().replace("\n", ""); byte[] encoded = Base64Utils.decodeFromString(key); DerInputStream derInputStream = new DerInputStream(encoded); DerValue[] seq = derInputStream.getSequence(0); BigInteger modulus = seq[1].getBigInteger(); BigInteger publicExp = seq[2].getBigInteger(); BigInteger privateExp = seq[3].getBigInteger(); BigInteger prime1 = seq[4].getBigInteger(); BigInteger prime2 = seq[5].getBigInteger(); BigInteger exp1 = seq[6].getBigInteger(); BigInteger exp2 = seq[7].getBigInteger(); BigInteger crtCoef = seq[8].getBigInteger(); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); KeyFactory kf = KeyFactory.getInstance("RSA"); this.signer = new RSASSASigner(kf.generatePrivate(keySpec)); }
static PrivateKey getPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { byte[] pkcs1Key = DatatypeConverter.parseBase64Binary(PRIVATE_KEY.replaceAll("(-+BEGIN RSA PRIVATE KEY-+\\r?\\n|-+END RSA PRIVATE KEY-+\\r?\\n?)", "")); DerInputStream dis = new DerInputStream(pkcs1Key); DerValue[] disSequence = dis.getSequence(0); BigInteger modulus = disSequence[1].getBigInteger(); BigInteger publicExp = disSequence[2].getBigInteger(); BigInteger privateExp = disSequence[3].getBigInteger(); BigInteger prime1 = disSequence[4].getBigInteger(); BigInteger prime2 = disSequence[5].getBigInteger(); BigInteger exp1 = disSequence[6].getBigInteger(); BigInteger exp2 = disSequence[7].getBigInteger(); BigInteger crtCoef = disSequence[8].getBigInteger(); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePrivate(keySpec); }
public static KeyStore createKeyStore(InputStream certInputStream, InputStream keyInputStream, String clientKeyAlgo, char[] clientKeyPassphrase) throws IOException, CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, KeyStoreException { CertificateFactory certFactory = CertificateFactory.getInstance("X509"); X509Certificate cert = (X509Certificate) certFactory.generateCertificate(certInputStream); byte[] keyBytes = decodePem(keyInputStream); PrivateKey privateKey; KeyFactory keyFactory = KeyFactory.getInstance(clientKeyAlgo); try { // First let's try PKCS8 privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(keyBytes)); } catch (InvalidKeySpecException e) { // Otherwise try PKCS8 RSAPrivateCrtKeySpec keySpec = PKCS1Util.decodePKCS1(keyBytes); privateKey = keyFactory.generatePrivate(keySpec); } KeyStore keyStore = KeyStore.getInstance("JKS"); keyStore.load(null, clientKeyPassphrase); String alias = cert.getSubjectX500Principal().getName(); keyStore.setKeyEntry(alias, privateKey, clientKeyPassphrase, new Certificate[]{cert}); return keyStore; }
@Override protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec == null) { throw new InvalidKeySpecException("keySpec == null"); } if (keySpec instanceof RSAPrivateCrtKeySpec) { return new OpenSSLRSAPrivateCrtKey((RSAPrivateCrtKeySpec) keySpec); } else if (keySpec instanceof RSAPrivateKeySpec) { return new OpenSSLRSAPrivateKey((RSAPrivateKeySpec) keySpec); } else if (keySpec instanceof PKCS8EncodedKeySpec) { return OpenSSLKey.getPrivateKey((PKCS8EncodedKeySpec) keySpec, NativeConstants.EVP_PKEY_RSA); } throw new InvalidKeySpecException("Must use RSAPublicKeySpec or PKCS8EncodedKeySpec; was " + keySpec.getClass().getName()); }
@Override public void generateKeyPair() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException { // https://github.com/bcgit/bc-java/blob/53d17ef99e30c6bd49e6eec9235e3eefca6a222d/pkix/src/test/java/org/bouncycastle/cert/test/CertTest.java RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger( "b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16), new BigInteger("11", 16)); RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec(new BigInteger( "b4a7e46170574f16a97082b22be58b6a2a629798419be12872a4bdba626cfae9900f76abfb12139dce5de56564fab2b6543165a040c606887420e33d91ed7ed7", 16), new BigInteger("11", 16), new BigInteger( "9f66f6b05410cd503b2709e88115d55daced94d1a34d4e32bf824d0dde6028ae79c5f07b580f5dce240d7111f7ddb130a7945cd7d957d1920994da389f490c89", 16), new BigInteger( "c0a0758cdf14256f78d4708c86becdead1b50ad4ad6c5c703e2168fbf37884cb", 16), new BigInteger("f01734d7960ea60070f1b06f2bb81bfac48ff192ae18451d5e56c734a5aab8a5", 16), new BigInteger( "b54bb9edff22051d9ee60f9351a48591b6500a319429c069a3e335a1d6171391", 16), new BigInteger("d3d83daf2a0cecd3367ae6f8ae1aeb82e9ac2f816c6fc483533d8297dd7884cd", 16), new BigInteger( "b8f52fc6f38593dabb661d3f50f8897f8106eee68b1bce78a95b132b4e5b5d19", 16)); KeyFactory fact = KeyFactory.getInstance("RSA", "BC"); setPrivateKey(fact.generatePrivate(privKeySpec)); setPublicKey(fact.generatePublic(pubKeySpec)); }
@Override public Key toJcaKey() throws GeneralSecurityException { final BigInteger modulus = Encoding.base64urlDecodeUint(n); final BigInteger publicExponent = Encoding.base64urlDecodeUint(e); if (getUse() == KeyUse.sig || d == null) { return KeyFactory.getInstance("RSA") .generatePublic(new RSAPublicKeySpec(modulus, publicExponent)); } else { final BigInteger privateExponent = Encoding.base64urlDecodeUint(d); final BigInteger primeP = Encoding.base64urlDecodeUint(p); final BigInteger primeQ = Encoding.base64urlDecodeUint(q); final BigInteger primeExponentP = Encoding.base64urlDecodeUint(dp); final BigInteger primeExponentQ = Encoding.base64urlDecodeUint(dq); final BigInteger crtCoefficent = Encoding.base64urlDecodeUint(qi); return KeyFactory.getInstance("RSA") .generatePrivate(new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficent)); } }
private static RSAPrivateKey toJcaPrivateKey( org.bouncycastle.asn1.pkcs.RSAPrivateKey rsaPrivateKey) throws GeneralSecurityException { RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPublicExponent(), rsaPrivateKey.getPrivateExponent(), rsaPrivateKey.getPrime1(), rsaPrivateKey.getPrime2(), rsaPrivateKey.getExponent1(), rsaPrivateKey.getExponent2(), rsaPrivateKey.getCoefficient()); KeyFactory kf = KeyFactory.getInstance("RSA", "BC"); RSAPrivateKey privateKey = (RSAPrivateKey) kf.generatePrivate(spec); return privateKey; }
/** * Test #1 for <code>RSAPrivateCrtKeySpec</code> constructor * Assertion: Constructs <code>RSAPrivateCrtKeySpec</code> * object using valid parameters */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies constructor with valid parameters.", method = "RSAPrivateCrtKeySpec", args = {java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class} ) public final void testRSAPrivateCrtKeySpec01() { KeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE); assertTrue(ks instanceof RSAPrivateCrtKeySpec); }
/** * Test #2 for <code>RSAPrivateCrtKeySpec</code> constructor * Assertion: Constructs <code>RSAPrivateCrtKeySpec</code> * object using valid parameters */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies constructor with valid parameters.", method = "RSAPrivateCrtKeySpec", args = {java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class} ) public final void testRSAPrivateCrtKeySpec02() { KeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE); assertTrue(ks instanceof RSAPrivateKeySpec); }
/** * Test #3 for <code>RSAPrivateCrtKeySpec</code> constructor * Assertion: Constructs <code>RSAPrivateCrtKeySpec</code> * object using valid parameters */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Verifies null as parameters.", method = "RSAPrivateCrtKeySpec", args = {java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class} ) public final void testRSAPrivateCrtKeySpec03() { new RSAPrivateCrtKeySpec( null, null, null, null, null, null, null, null); }
/** * Test for <code>getCrtCoefficient()</code> method<br> * Assertion: returns crt coefficient */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getCrtCoefficient", args = {} ) public final void testGetCrtCoefficient() { RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L)); assertTrue(BigInteger.valueOf(5L).equals(ks.getCrtCoefficient())); }
/** * Test for <code>getPrimeExponentP()</code> method<br> * Assertion: returns prime exponent P */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getPrimeExponentP", args = {} ) public final void testGetPrimeExponentP() { RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE); assertTrue(BigInteger.valueOf(5L).equals(ks.getPrimeExponentP())); }
/** * Test for <code>getPrimeExponentQ()</code> method<br> * Assertion: returns prime exponent Q */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getPrimeExponentQ", args = {} ) public final void testGetPrimeExponentQ() { RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE); assertTrue(BigInteger.valueOf(5L).equals(ks.getPrimeExponentQ())); }
/** * Test for <code>getPrimeP()</code> method<br> * Assertion: returns prime P */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getPrimeP", args = {} ) public final void testGetPrimeP() { RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE); assertTrue(BigInteger.valueOf(5L).equals(ks.getPrimeP())); }
/** * Test for <code>getPrimeQ()</code> method<br> * Assertion: returns prime Q */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getPrimeQ", args = {} ) public final void testGetPrimeQ() { RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE); assertTrue(BigInteger.valueOf(5L).equals(ks.getPrimeQ())); }
/** * Test for <code>getPublicExponent()</code> method<br> * Assertion: returns public exponent */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getPublicExponent", args = {} ) public final void testGetPublicExponent() { RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE); assertTrue(BigInteger.valueOf(5L).equals(ks.getPublicExponent())); }
/** * Test for <code>getModulus()</code> method<br> * Assertion: returns modulus */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getModulus", args = {} ) public final void testGetModulus() { RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE); assertTrue(BigInteger.valueOf(5L).equals(ks.getModulus())); }
/** * Test for <code>getPrivateExponent()</code> method<br> * Assertion: returns private exponent */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getPrivateExponent", args = {} ) public final void testGetPrivateExponent() { RSAPrivateCrtKeySpec ks = new RSAPrivateCrtKeySpec( BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(5L), BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE); assertTrue(BigInteger.valueOf(5L).equals(ks.getPrivateExponent())); }
public Ssh2RsaPrivateCrtKey(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent, BigInteger primeP, BigInteger primeQ, BigInteger primeExponentP, BigInteger primeExponentQ, BigInteger crtCoefficient) throws NoSuchAlgorithmException, InvalidKeySpecException { KeyFactory keyFactory = JCEProvider .getProviderForAlgorithm(JCEAlgorithms.JCE_RSA) == null ? KeyFactory .getInstance(JCEAlgorithms.JCE_RSA) : KeyFactory.getInstance( JCEAlgorithms.JCE_RSA, JCEProvider.getProviderForAlgorithm(JCEAlgorithms.JCE_RSA)); RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec(modulus, publicExponent, privateExponent, primeP, primeQ, primeExponentP, primeExponentQ, crtCoefficient); prv = (RSAPrivateCrtKey) keyFactory.generatePrivate(spec); }
private KeyPair generateLongFixedKeys() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException { RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( new BigInteger("a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",16), new BigInteger("010001",16)); RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec( new BigInteger("a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",16), new BigInteger("010001",16), new BigInteger("33a5042a90b27d4f5451ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c0266c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a39574501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6dcee883547c6a3b325",16), new BigInteger("e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e86296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda8e6443",16), new BigInteger("b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd",16), new BigInteger("28fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8dd3ede2448328f385d81b30e8e43b2fffa027861979",16), new BigInteger("1a8b38f398fa712049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729",16), new BigInteger("27156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24a79f4d",16)); KeyFactory fact = KeyFactory.getInstance("RSA", "BC"); return new KeyPair(fact.generatePublic(pubKeySpec), fact.generatePrivate(privKeySpec)); }
private KeyPair generateLongFixedKeys() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException { RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec( new BigInteger("a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",16), new BigInteger("010001",16)); RSAPrivateCrtKeySpec privKeySpec = new RSAPrivateCrtKeySpec( new BigInteger("a56e4a0e701017589a5187dc7ea841d156f2ec0e36ad52a44dfeb1e61f7ad991d8c51056ffedb162b4c0f283a12a88a394dff526ab7291cbb307ceabfce0b1dfd5cd9508096d5b2b8b6df5d671ef6377c0921cb23c270a70e2598e6ff89d19f105acc2d3f0cb35f29280e1386b6f64c4ef22e1e1f20d0ce8cffb2249bd9a2137",16), new BigInteger("010001",16), new BigInteger("33a5042a90b27d4f5451ca9bbbd0b44771a101af884340aef9885f2a4bbe92e894a724ac3c568c8f97853ad07c0266c8c6a3ca0929f1e8f11231884429fc4d9ae55fee896a10ce707c3ed7e734e44727a39574501a532683109c2abacaba283c31b4bd2f53c3ee37e352cee34f9e503bd80c0622ad79c6dcee883547c6a3b325",16), new BigInteger("e7e8942720a877517273a356053ea2a1bc0c94aa72d55c6e86296b2dfc967948c0a72cbccca7eacb35706e09a1df55a1535bd9b3cc34160b3b6dcd3eda8e6443",16), new BigInteger("b69dca1cf7d4d7ec81e75b90fcca874abcde123fd2700180aa90479b6e48de8d67ed24f9f19d85ba275874f542cd20dc723e6963364a1f9425452b269a6799fd",16), new BigInteger("28fa13938655be1f8a159cbaca5a72ea190c30089e19cd274a556f36c4f6e19f554b34c077790427bbdd8dd3ede2448328f385d81b30e8e43b2fffa027861979",16), new BigInteger("1a8b38f398fa712049898d7fb79ee0a77668791299cdfa09efc0e507acb21ed74301ef5bfd48be455eaeb6e1678255827580a8e4e8e14151d1510a82a3f2e729",16), new BigInteger("27156aba4126d24a81f3a528cbfb27f56886f840a9f6e86e17a44b94fe9319584b8e22fdde1e5a2e3bd8aa5ba8d8584194eb2190acf832b847f13a3d24a79f4d",16)); KeyFactory fact = KeyFactory.getInstance("RSA", BC); return new KeyPair(fact.generatePublic(pubKeySpec), fact.generatePrivate(privKeySpec)); }
/** * Extracts private key (predictive_services.pem) contents */ private static PrivateKey getPrivateKey(String privateKeyBase64) { String privKeyPEM = privateKeyBase64.replace("-----BEGIN RSA PRIVATE KEY-----\n", ""); privKeyPEM = privKeyPEM.replace("\n-----END RSA PRIVATE KEY-----", ""); // Base64 decode the data byte[] encoded = Base64.decodeBase64(privKeyPEM); try { DerInputStream derReader = new DerInputStream(encoded); DerValue[] seq = derReader.getSequence(0); if (seq.length < 9) { throw new GeneralSecurityException("Could not read private key"); } // skip version seq[0]; BigInteger modulus = seq[1].getBigInteger(); BigInteger publicExp = seq[2].getBigInteger(); BigInteger privateExp = seq[3].getBigInteger(); BigInteger primeP = seq[4].getBigInteger(); BigInteger primeQ = seq[5].getBigInteger(); BigInteger expP = seq[6].getBigInteger(); BigInteger expQ = seq[7].getBigInteger(); BigInteger crtCoeff = seq[8].getBigInteger(); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, primeP, primeQ, expP, expQ, crtCoeff); KeyFactory factory = KeyFactory.getInstance("RSA"); return factory.generatePrivate(keySpec); } catch (IOException | GeneralSecurityException e) { Throwables.propagate(e); } return null; }
/** * 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(); }
protected PrivateKey engineGeneratePrivate( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof PKCS8EncodedKeySpec) { try { return generatePrivate(PrivateKeyInfo.getInstance(((PKCS8EncodedKeySpec)keySpec).getEncoded())); } catch (Exception e) { // // in case it's just a RSAPrivateKey object... -- openSSL produces these // try { return new BCRSAPrivateCrtKey( RSAPrivateKey.getInstance(((PKCS8EncodedKeySpec)keySpec).getEncoded())); } catch (Exception ex) { throw new ExtendedInvalidKeySpecException("unable to process key spec: " + e.toString(), e); } } } else if (keySpec instanceof RSAPrivateCrtKeySpec) { return new BCRSAPrivateCrtKey((RSAPrivateCrtKeySpec)keySpec); } else if (keySpec instanceof RSAPrivateKeySpec) { return new BCRSAPrivateKey((RSAPrivateKeySpec)keySpec); } throw new InvalidKeySpecException("Unknown KeySpec type: " + keySpec.getClass().getName()); }
/** * construct a private key from an RSAPrivateCrtKeySpec * * @param spec the spec to be used in construction. */ BCRSAPrivateCrtKey( 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(); }
public static PrivateKey buildPrivateKey(DerKeySpec ks) { try { RSAPrivateCrtKeySpec keySpec = ks.rsaPrivateCrtKeySpec(); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePrivate(keySpec); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
/** * Decode PKCS#1 encoded private key into RSAPrivateCrtKeySpec. * * <p/>The ASN.1 syntax for the private key with CRT is * * <pre> * -- * -- Representation of RSA private key with information for the CRT algorithm. * -- * RSAPrivateKey ::= SEQUENCE { * version Version, * modulus INTEGER, -- n * publicExponent INTEGER, -- e * privateExponent INTEGER, -- d * prime1 INTEGER, -- p * prime2 INTEGER, -- q * exponent1 INTEGER, -- d mod (p-1) * exponent2 INTEGER, -- d mod (q-1) * coefficient INTEGER, -- (inverse of q) mod p * otherPrimeInfos OtherPrimeInfos OPTIONAL * } * </pre> * * @param keyBytes PKCS#1 encoded key * @throws IOException */ private void decode(byte[] keyBytes) throws IOException { DerParser parser = new DerParser(keyBytes); Asn1Object sequence = parser.read(); if (sequence.getType() != DerParser.SEQUENCE) throw new IOException("Invalid DER: not a sequence"); //$NON-NLS-1$ // Parse inside the sequence parser = sequence.getParser(); parser.read(); // Skip version BigInteger modulus = parser.read().getInteger(); BigInteger publicExp = parser.read().getInteger(); BigInteger privateExp = parser.read().getInteger(); BigInteger prime1 = parser.read().getInteger(); BigInteger prime2 = parser.read().getInteger(); BigInteger exp1 = parser.read().getInteger(); BigInteger exp2 = parser.read().getInteger(); BigInteger crtCoef = parser.read().getInteger(); keySpec = new RSAPrivateCrtKeySpec( modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); }
public void bad10() throws Exception { BigInteger bigInteger = new BigInteger("12345", 5); new DSAPrivateKeySpec(bigInteger, null, null, null); new DSAPublicKeySpec(bigInteger, null, bigInteger, null); // report once new DHPrivateKeySpec(bigInteger, null, null); new DHPublicKeySpec(bigInteger, null, null); new ECPrivateKeySpec(bigInteger, null); new RSAPrivateKeySpec(bigInteger, null); new RSAMultiPrimePrivateCrtKeySpec(bigInteger, null, null, null, null, null, null, null, null); new RSAPrivateCrtKeySpec(bigInteger, null, null, null, null, null, null, null); new RSAPublicKeySpec(bigInteger, null); new DSAPublicKeyImpl(bigInteger, null, null, null); }
/** * construct a private key from an RSAPrivateCrtKeySpec * * @param spec the spec to be used in construction. */ TempJCERSAPrivateCrtKey( 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(); }
public static RSAPrivateCrtKeySpec decodePKCS1(byte[] keyBytes) throws IOException { DerParser parser = new DerParser(keyBytes); Asn1Object sequence = parser.read(); sequence.validateSequence(); parser = new DerParser(sequence.getValue()); parser.read(); return new RSAPrivateCrtKeySpec(next(parser), next(parser), next(parser), next(parser), next(parser), next(parser), next(parser), next(parser)); }