List of usage examples for java.security.spec RSAPrivateCrtKeySpec RSAPrivateCrtKeySpec
public RSAPrivateCrtKeySpec(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent,
BigInteger primeP, BigInteger primeQ, BigInteger primeExponentP, BigInteger primeExponentQ,
BigInteger crtCoefficient)
From source file:io.kubernetes.client.util.SSLUtils.java
public static RSAPrivateCrtKeySpec decodePKCS1(byte[] keyBytes) throws IOException { DerParser parser = new DerParser(keyBytes); Asn1Object sequence = parser.read(); sequence.validateSequence();//ww w . java 2 s. c om 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)); }
From source file:com.oneis.common.utils.SSLCertificates.java
private static PrivateKey readPEMPrivateKey(String filename) throws java.io.IOException, java.security.GeneralSecurityException { ByteArrayInputStream bIn = readPEM(filename); ASN1InputStream aIn = new ASN1InputStream(bIn); ASN1Sequence seq = (ASN1Sequence) aIn.readObject(); if (!(seq.getObjectAt(1) instanceof DERInteger)) { throw new RuntimeException("Can't read RSA private key from " + filename + " - if file starts '-----BEGIN PRIVATE KEY-----' then it needs converting to RSA format with 'openssl rsa -in server-in.key -out server.key'."); }//from ww w . j av a2 s . com DERInteger mod = (DERInteger) seq.getObjectAt(1); DERInteger pubExp = (DERInteger) seq.getObjectAt(2); DERInteger privExp = (DERInteger) seq.getObjectAt(3); DERInteger p1 = (DERInteger) seq.getObjectAt(4); DERInteger p2 = (DERInteger) seq.getObjectAt(5); DERInteger exp1 = (DERInteger) seq.getObjectAt(6); DERInteger exp2 = (DERInteger) seq.getObjectAt(7); DERInteger crtCoef = (DERInteger) seq.getObjectAt(8); RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec(mod.getValue(), pubExp.getValue(), privExp.getValue(), p1.getValue(), p2.getValue(), exp1.getValue(), exp2.getValue(), crtCoef.getValue()); KeyFactory factory = KeyFactory.getInstance("RSA"); return factory.generatePrivate(privSpec); }
From source file:net.adamcin.httpsig.testutil.KeyTestUtil.java
public static KeyPair getKeyPairFromProperties(String parentName, String keyName) { InputStream is = null;// www .j av a 2s .com try { is = KeyTestUtil.class.getResourceAsStream("/" + parentName + "/" + keyName + ".properties"); Properties props = new Properties(); props.load(is); if (TYPE_RSA.equals(props.getProperty(P_TYPE))) { RSAPrivateKeySpec privSpec = null; if (props.getProperty(RSA_P) != null && props.getProperty(RSA_Q) != null && props.getProperty(RSA_U) != null) { privSpec = new RSAPrivateCrtKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E)), new BigInteger(props.getProperty(RSA_D)), new BigInteger(props.getProperty(RSA_P)), new BigInteger(props.getProperty(RSA_Q)), new BigInteger(props.getProperty(RSA_PE)), new BigInteger(props.getProperty(RSA_QE)), new BigInteger(props.getProperty(RSA_U))); } else { privSpec = new RSAPrivateKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_D))); } RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E))); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } else if (TYPE_DSA.equals(props.getProperty(P_TYPE))) { DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(new BigInteger(props.getProperty(DSA_X)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(new BigInteger(props.getProperty(DSA_Y)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } } catch (Exception e) { LOGGER.error("Failed to read properties", e); } finally { IOUtils.closeQuietly(is); } return null; }
From source file:com.mastercard.mcbp.utils.crypto.CryptoServiceImpl.java
/** * {@inheritDoc}// w w w. j av a 2s.c o m */ @Override public final int initRsaPrivateKey(final ByteArray primeP, final ByteArray primeQ, final ByteArray primeExponentP, final ByteArray primeExponentQ, final ByteArray crtCoefficient) throws McbpCryptoException { try { final BigInteger p = new BigInteger(primeP.toHexString(), 16); final BigInteger q = new BigInteger(primeQ.toHexString(), 16); final BigInteger dp = new BigInteger(primeExponentP.toHexString(), 16); final BigInteger dq = new BigInteger(primeExponentQ.toHexString(), 16); final BigInteger a = new BigInteger(crtCoefficient.toHexString(), 16); final BigInteger n = p.multiply(q); final BigInteger e = dp.modInverse(p.subtract(BigInteger.ONE)); final BigInteger d = e.modInverse(p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)) .divide((p.subtract(BigInteger.ONE)).gcd(q.subtract(BigInteger.ONE)))); final RSAPrivateKey rsaKey = (RSAPrivateKey) KeyFactory.getInstance("RSA") .generatePrivate(new RSAPrivateCrtKeySpec(n, e, d, p, q, dp, dq, a)); initRsaPrivate(rsaKey); return n.bitLength() / 8; } catch (final NoSuchAlgorithmException | InvalidKeySpecException e) { throw new McbpCryptoException(e.toString()); } }
From source file:org.apache.jmeter.protocol.oauth.sampler.PrivateKeyReader.java
/** * Convert PKCS#1 encoded private key into RSAPrivateCrtKeySpec. * /*from w w w . jav a 2s .c om*/ * <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 * @return KeySpec * @throws IOException */ private RSAPrivateCrtKeySpec getRSAKeySpec(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(); RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); return keySpec; }
From source file:org.casbah.provider.PKCS1EncodedKeyTest.java
@Before public void setup() throws GeneralSecurityException { KeyFactory kf = KeyFactory.getInstance("RSA"); RSAPrivateCrtKeySpec keyspec = new RSAPrivateCrtKeySpec(MODULUS, PUBLIC_EXPONENT, PRIVATE_EXPONENT, PRIME1, PRIME2, EXPONENT1, EXPONENT2, COEFFICIENT); wrappedKey = (RSAPrivateCrtKey) kf.generatePrivate(keyspec); }
From source file:org.casbah.provider.SSLeayEncoderTest.java
private static RSAPrivateCrtKey generateKey() throws GeneralSecurityException { KeyFactory kf = KeyFactory.getInstance("RSA"); RSAPrivateCrtKeySpec keyspec = new RSAPrivateCrtKeySpec(MODULUS, PUBLIC_EXPONENT, PRIVATE_EXPONENT, PRIME1, PRIME2, EXPONENT1, EXPONENT2, COEFFICIENT); return (RSAPrivateCrtKey) kf.generatePrivate(keyspec); }
From source file:org.cloudfoundry.identity.uaa.oauth.SignerProvider.java
static KeyPair parseKeyPair(String pemData) { Matcher m = PEM_DATA.matcher(pemData.trim()); if (!m.matches()) { throw new IllegalArgumentException("String is not PEM encoded data"); }/*w w w . ja v a 2 s. com*/ String type = m.group(1); final byte[] content = b64Decode(utf8Encode(m.group(2))); PublicKey publicKey; PrivateKey privateKey = null; try { KeyFactory fact = KeyFactory.getInstance("RSA"); if (type.equals("RSA PRIVATE KEY")) { ASN1Sequence seq = ASN1Sequence.getInstance(content); if (seq.size() != 9) { 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); privateKey = fact.generatePrivate(privSpec); } else if (type.equals("PUBLIC KEY")) { KeySpec keySpec = new X509EncodedKeySpec(content); publicKey = fact.generatePublic(keySpec); } else if (type.equals("RSA PUBLIC KEY")) { 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 { throw new IllegalArgumentException(type + " is not a supported format"); } return new KeyPair(publicKey, privateKey); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } }