Example usage for org.bouncycastle.crypto.params RSAPrivateCrtKeyParameters getModulus

List of usage examples for org.bouncycastle.crypto.params RSAPrivateCrtKeyParameters getModulus

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.params RSAPrivateCrtKeyParameters getModulus.

Prototype

public BigInteger getModulus() 

Source Link

Usage

From source file:co.lqnt.lockbox.key.KeyFactory.java

License:Open Source License

/**
 * Generate a new private key./*from   ww w.ja  v a  2  s  . c  o  m*/
 *
 * @param size The size of the key in bits.
 *
 * @return The private key.
 */
public PrivateKey generatePrivateKey(final int size) {
    this.keyGenerator().init(new RSAKeyGenerationParameters(BigInteger.valueOf(65537),
            this.random().jceSecureRandom(), size, 80));

    AsymmetricKeyParameter keyParameters = this.keyGenerator().generateKeyPair().getPrivate();

    RSAPrivateCrtKeyParameters rsaKeyParameters;
    if (keyParameters instanceof RSAPrivateCrtKeyParameters) {
        rsaKeyParameters = (RSAPrivateCrtKeyParameters) keyParameters;
    } else {
        throw new RuntimeException("Invalid key pair generated by Bouncy Castle.");
    }

    return new PrivateKey(rsaKeyParameters.getModulus(), rsaKeyParameters.getPublicExponent(),
            rsaKeyParameters.getExponent(), rsaKeyParameters.getP(), rsaKeyParameters.getQ(),
            rsaKeyParameters.getDP(), rsaKeyParameters.getDQ(), rsaKeyParameters.getQInv());
}

From source file:co.lqnt.lockbox.key.KeyFactory.java

License:Open Source License

/**
 * Convert Bouncy Castle private key info to a Lockbox private key.
 *
 * @param keyInformation The private key information.
 *
 * @return The private key.//w  w w  .  jav  a 2s . c om
 * @throws PrivateKeyReadException If reading of the private key fails.
 */
protected PrivateKey convertPrivateKey(final PrivateKeyInfo keyInformation) throws PrivateKeyReadException {
    AsymmetricKeyParameter keyParameter;
    try {
        keyParameter = this.bcKeyParametersFactory().createPrivateKeyParameters(keyInformation);
    } catch (IOException e) {
        throw new PrivateKeyReadException(e);
    }

    RSAPrivateCrtKeyParameters privateKeyParameters;
    if (keyParameter instanceof RSAPrivateCrtKeyParameters) {
        privateKeyParameters = (RSAPrivateCrtKeyParameters) keyParameter;
    } else {
        throw new PrivateKeyReadException();
    }

    return new PrivateKey(privateKeyParameters.getModulus(), privateKeyParameters.getPublicExponent(),
            privateKeyParameters.getExponent(), privateKeyParameters.getP(), privateKeyParameters.getQ(),
            privateKeyParameters.getDP(), privateKeyParameters.getDQ(), privateKeyParameters.getQInv());
}

From source file:com.foilen.smalltools.crypt.bouncycastle.cert.RSATools.java

License:Open Source License

/**
 * Create a {@link Key} from the private {@link AsymmetricKeys}.
 *
 * @param asymmetricKeys/*from   ww  w. ja va2  s.c  om*/
 *            the asymmetric keys
 * @return the Java key
 */
public static PrivateKey createPrivateKey(AsymmetricKeys asymmetricKeys) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPrivateCrtKeyParameters privateKeyParameters = (RSAPrivateCrtKeyParameters) asymmetricKeys
                .getPrivateKey();
        RSAPrivateCrtKeySpec keySpec = new RSAPrivateCrtKeySpec(privateKeyParameters.getModulus(),
                privateKeyParameters.getPublicExponent(), privateKeyParameters.getExponent(),
                privateKeyParameters.getP(), privateKeyParameters.getQ(), privateKeyParameters.getDP(),
                privateKeyParameters.getDQ(), privateKeyParameters.getQInv());
        return keyFactory.generatePrivate(keySpec);
    } catch (Exception e) {
        throw new SmallToolsException("Problem generating the key", e);
    }
}

From source file:com.geoxp.oss.CryptoHelperTest.java

License:Apache License

@Test
public void testSSHSignatureBlobSign_RSA() throws Exception {
    RSAKeyPairGenerator rsakpg = new RSAKeyPairGenerator();
    RSAKeyGenerationParameters params = new RSAKeyGenerationParameters(new BigInteger("35"), new SecureRandom(),
            2048, 8);/*  ww w  .  j  a v  a2 s  .  c  o m*/
    rsakpg.init(params);

    AsymmetricCipherKeyPair kp = rsakpg.generateKeyPair();

    RSAPrivateCrtKeyParameters privParams = (RSAPrivateCrtKeyParameters) kp.getPrivate();
    RSAKeyParameters pubParams = (RSAKeyParameters) kp.getPublic();

    KeySpec ks = new RSAPrivateKeySpec(privParams.getModulus(), privParams.getExponent());
    PrivateKey priv = KeyFactory.getInstance("RSA").generatePrivate(ks);

    ks = new RSAPublicKeySpec(pubParams.getModulus(), pubParams.getExponent());
    PublicKey pub = KeyFactory.getInstance("RSA").generatePublic(ks);

    byte[] data = PLAINTEXT.getBytes();
    byte[] sig = CryptoHelper.sshSignatureBlobSign(data, priv);

    Assert.assertTrue(CryptoHelper.sshSignatureBlobVerify(data, sig, pub));
}

From source file:com.joyent.http.signature.crypto.NativeRSABlindedEngine.java

License:Open Source License

/**
 * Process a single block using the basic RSA algorithm.
 *
 * @param in the input array.//from   ww  w . ja va  2  s . c  o  m
 * @param inOff the offset into the input buffer where the data starts.
 * @param inLen the length of the data to be processed.
 * @return the result of the RSA process.
 * @exception DataLengthException the input block is too large.
 */
@Override
public byte[] processBlock(final byte[] in, final int inOff, final int inLen) {
    if (key == null) {
        throw new IllegalStateException("RSA engine not initialised");
    }

    BigInteger input = core.convertInput(in, inOff, inLen);

    BigInteger result;
    if (key instanceof RSAPrivateCrtKeyParameters) {
        RSAPrivateCrtKeyParameters k = (RSAPrivateCrtKeyParameters) key;

        BigInteger e = k.getPublicExponent();
        // can't do blinding without a public exponent
        if (e != null) {
            BigInteger m = k.getModulus();
            BigInteger r = BigIntegers.createRandomInRange(ONE, m.subtract(ONE), random);

            // This is a modification to use the GMP native library method
            BigInteger blindedModPow = Gmp.modPowSecure(r, e, m);

            BigInteger blindedInput = blindedModPow.multiply(input).mod(m);
            BigInteger blindedResult = core.processBlock(blindedInput);

            // This is a modification to use the GMP native library method
            BigInteger rInv = Gmp.modInverse(r, m);

            result = blindedResult.multiply(rInv).mod(m);
            // defence against Arjen Lenstras CRT attack
            // This is a modification to use the GMP native library method
            if (!input.equals(Gmp.modPowInsecure(result, e, m))) {
                throw new IllegalStateException("RSA engine faulty decryption/signing detected");
            }
        } else {
            result = core.processBlock(input);
        }
    } else {
        result = core.processBlock(input);
    }

    return core.convertOutput(result);
}

From source file:com.licel.jcardsim.crypto.RSAPrivateCrtKeyImpl.java

License:Apache License

/**
 * Construct and initialize rsa key with RSAPrivateCrtKeyParameters.
 * Use in KeyPairImpl//from  w ww  .  jav a  2 s.  c  o m
 * @see javacard.security.KeyPair
 * @see RSAPrivateCrtKeyParameters
 * @param params key params from BouncyCastle API
 */
public RSAPrivateCrtKeyImpl(RSAPrivateCrtKeyParameters params) {
    super(new RSAKeyParameters(true, params.getModulus(), params.getExponent()));
    type = KeyBuilder.TYPE_RSA_CRT_PRIVATE;
    setParameters(params);
}

From source file:dorkbox.util.crypto.CryptoRSA.java

License:Apache License

@SuppressWarnings("RedundantIfStatement")
public static boolean compare(RSAPrivateCrtKeyParameters private1, RSAPrivateCrtKeyParameters private2) {
    if (!private1.getModulus().equals(private2.getModulus())) {
        return false;
    }//from w  ww  .j  a v a 2s.  com
    if (!private1.getExponent().equals(private2.getExponent())) {
        return false;
    }
    if (!private1.getDP().equals(private2.getDP())) {
        return false;
    }
    if (!private1.getDQ().equals(private2.getDQ())) {
        return false;
    }
    if (!private1.getP().equals(private2.getP())) {
        return false;
    }
    if (!private1.getPublicExponent().equals(private2.getPublicExponent())) {
        return false;
    }
    if (!private1.getQ().equals(private2.getQ())) {
        return false;
    }
    if (!private1.getQInv().equals(private2.getQInv())) {
        return false;
    }

    return true;
}

From source file:dorkbox.util.serialization.RsaPrivateKeySerializer.java

License:Apache License

@Override
public void write(Kryo kryo, Output output, RSAPrivateCrtKeyParameters key) {
    byte[] bytes;
    int length;//from   w w w.  j  av a  2  s  .co  m

    /////////////
    bytes = key.getDP().toByteArray();
    length = bytes.length;

    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = key.getDQ().toByteArray();
    length = bytes.length;

    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = key.getExponent().toByteArray();
    length = bytes.length;

    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = key.getModulus().toByteArray();
    length = bytes.length;

    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = key.getP().toByteArray();
    length = bytes.length;

    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = key.getPublicExponent().toByteArray();
    length = bytes.length;

    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = key.getQ().toByteArray();
    length = bytes.length;

    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);

    /////////////
    bytes = key.getQInv().toByteArray();
    length = bytes.length;

    output.writeInt(length, true);
    output.writeBytes(bytes, 0, length);
}

From source file:frost.crypt.FrostCrypt.java

License:Open Source License

/**
 * Generate a new RSA 1024 bit key pair.
 * @returns String[0] is private key; String[1] is public key
 *//*  ww w  . ja v  a 2  s  .  c o  m*/
public synchronized String[] generateKeys() {

    RSAKeyPairGenerator keygen = new RSAKeyPairGenerator();
    keygen.init(new RSAKeyGenerationParameters(
            new BigInteger("3490529510847650949147849619903898133417764638493387843990820577"),
            getSecureRandom(), 1024, 80));
    //this big integer is the winner of some competition as far as I remember

    AsymmetricCipherKeyPair keys = keygen.generateKeyPair();

    //extract the keys
    RSAKeyParameters pubKey = (RSAKeyParameters) keys.getPublic();
    RSAPrivateCrtKeyParameters privKey = (RSAPrivateCrtKeyParameters) keys.getPrivate();

    //the return value
    String[] result = new String[2];
    StringBuffer temp = new StringBuffer();

    //create the keys
    temp.append(new String(Base64.encode(pubKey.getExponent().toByteArray())));
    temp.append(":");
    temp.append(new String(Base64.encode(pubKey.getModulus().toByteArray())));
    result[1] = temp.toString(); // public key

    //rince and repeat, this time exactly the way its done in the constructor
    temp = new StringBuffer();
    temp.append(new String(Base64.encode(privKey.getModulus().toByteArray())));
    temp.append(":");
    temp.append(new String(Base64.encode(privKey.getPublicExponent().toByteArray())));
    temp.append(":");
    temp.append(new String(Base64.encode(privKey.getExponent().toByteArray())));
    temp.append(":");
    temp.append(new String(Base64.encode(privKey.getP().toByteArray())));
    temp.append(":");
    temp.append(new String(Base64.encode(privKey.getQ().toByteArray())));
    temp.append(":");
    temp.append(new String(Base64.encode(privKey.getDP().toByteArray())));
    temp.append(":");
    temp.append(new String(Base64.encode(privKey.getDQ().toByteArray())));
    temp.append(":");
    temp.append(new String(Base64.encode(privKey.getQInv().toByteArray())));
    result[0] = temp.toString(); // private key

    return result;
}

From source file:org.fnppl.opensdx.security.AsymmetricKeyPair.java

License:Open Source License

public AsymmetricKeyPair(AsymmetricCipherKeyPair keyPair) {
    type = OSDXKey.ALGO_RSA;//from  ww  w  .  j  a  v a2 s . co m

    CipherParameters pub = keyPair.getPublic();
    CipherParameters priv = keyPair.getPrivate();

    RSAKeyParameters rpub = (RSAKeyParameters) pub;
    RSAPrivateCrtKeyParameters rpriv = (RSAPrivateCrtKeyParameters) priv;

    this.pubkey = new PublicKey(rpub.getModulus(), rpub.getExponent());
    this.privkey = new PrivateKey(rpriv.getModulus(), rpriv.getExponent());

    this.bitcount = rpub.getModulus().bitLength();
}