Search in sources :

Example 41 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.

the class RSAPrivateCrtKeySpecTest method testGetPrimeExponentP.

/**
 * Test for <code>getPrimeExponentP()</code> method<br>
 * Assertion: returns prime exponent P
 */
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()));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 42 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.

the class RSAPrivateCrtKeySpecTest method testGetPrimeExponentQ.

/**
 * Test for <code>getPrimeExponentQ()</code> method<br>
 * Assertion: returns prime exponent Q
 */
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()));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 43 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project j2objc by google.

the class RSAPrivateCrtKeySpecTest method testGetPrimeP.

/**
 * Test for <code>getPrimeP()</code> method<br>
 * Assertion: returns prime P
 */
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()));
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec)

Example 44 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project vert.x by eclipse.

the class PrivateKeyParser method getRSAKeySpec.

/**
 * Convert PKCS#1 encoded private key into RSAPrivateCrtKeySpec.
 * <p/>
 * <p/>The ASN.1 syntax for the private key with CRT is
 * <p/>
 * <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 VertxException
 */
public static RSAPrivateCrtKeySpec getRSAKeySpec(byte[] keyBytes) throws VertxException {
    DerParser parser = new DerParser(keyBytes);
    Asn1Object sequence = parser.read();
    if (sequence.getType() != DerParser.SEQUENCE) {
        throw new VertxException("Invalid DER: not a sequence");
    }
    // Parse inside the sequence
    parser = sequence.getParser();
    // Skip version
    parser.read();
    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();
    return new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) VertxException(io.vertx.core.VertxException) BigInteger(java.math.BigInteger)

Example 45 with RSAPrivateCrtKeySpec

use of java.security.spec.RSAPrivateCrtKeySpec in project dbeaver by serge-rider.

the class PKCS1Util method loadPrivateKeyFromPKCS1.

public static PrivateKey loadPrivateKeyFromPKCS1(String privateKeyPem) throws GeneralSecurityException, IOException {
    DerInputStream derReader = new DerInputStream(Base64.decode(privateKeyPem));
    DerValue[] seq = derReader.getSequence(0);
    if (seq.length < 9) {
        throw new GeneralSecurityException("Could not parse a PKCS1 private key.");
    }
    // skip version seq[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 factory = KeyFactory.getInstance("RSA");
    return factory.generatePrivate(keySpec);
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) DerValue(sun.security.util.DerValue) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) DerInputStream(sun.security.util.DerInputStream) KeyFactory(java.security.KeyFactory)

Aggregations

RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)48 KeyFactory (java.security.KeyFactory)16 BigInteger (java.math.BigInteger)14 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)12 PrivateKey (java.security.PrivateKey)11 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)10 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)9 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)9 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)8 PublicKey (java.security.PublicKey)7 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)7 KeySpec (java.security.spec.KeySpec)6 IOException (java.io.IOException)5 KeyPair (java.security.KeyPair)5 RSAPublicKey (java.security.interfaces.RSAPublicKey)5 GeneralSecurityException (java.security.GeneralSecurityException)4 InvalidKeyException (java.security.InvalidKeyException)4 Signature (java.security.Signature)4 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)4 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)4