Example usage for org.bouncycastle.crypto.params ParametersWithRandom getParameters

List of usage examples for org.bouncycastle.crypto.params ParametersWithRandom getParameters

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.params ParametersWithRandom getParameters.

Prototype

public CipherParameters getParameters() 

Source Link

Usage

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

License:Apache License

/**
 * initialise the RSA engine./*w  w w  .ja v a  2  s.c o  m*/
 *
 * @param forEncryption true if we are encrypting, false otherwise.
 * @param param         the necessary RSA key parameters.
 */
public void init(boolean forEncryption, CipherParameters param) {
    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom rParam = (ParametersWithRandom) param;

        key = (RSAKeyParameters) rParam.getParameters();
    } else {
        key = (RSAKeyParameters) param;
    }

    this.forEncryption = forEncryption;

    if (key instanceof RSAPrivateCrtKeyParameters) {
        isPrivate = true;
        //
        // we have the extra factors, use the Chinese Remainder Theorem - the author
        // wishes to express his thanks to Dirk Bonekaemper at rtsffm.com for
        // advice regarding the expression of this.
        //
        RSAPrivateCrtKeyParameters crtKey = (RSAPrivateCrtKeyParameters) key;

        p = new GmpInteger(crtKey.getP());
        q = new GmpInteger(crtKey.getQ());
        dP = new GmpInteger(crtKey.getDP());
        dQ = new GmpInteger(crtKey.getDQ());
        qInv = crtKey.getQInv();

        exponent = modulus = null;
    } else {
        isPrivate = false;
        exponent = new GmpInteger(key.getExponent());
        modulus = new GmpInteger(key.getModulus());
        isSmallExponent = exponent.bitLength() < 64;

        p = q = dP = dQ = null;
        qInv = null;
    }
}

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

License:Open Source License

/**
 * initialise the RSA engine.//from   w w w  .ja  va2 s. co m
 *
 * @param forEncryption true if we are encrypting, false otherwise.
 * @param param the necessary RSA key parameters.
 */
@Override
public void init(final boolean forEncryption, final CipherParameters param) {
    core.init(forEncryption, param);

    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom rParam = (ParametersWithRandom) param;

        key = (RSAKeyParameters) rParam.getParameters();
        random = rParam.getRandom();
    } else {
        key = (RSAKeyParameters) param;
        random = new SecureRandom();
    }
}

From source file:org.xipki.commons.security.pkcs12.NssPlainRSASigner.java

License:Open Source License

@Override
public void init(final boolean forEncryption, final CipherParameters param) {
    if (!forEncryption) {
        throw new RuntimeCryptoException("verification mode not supported.");
    }/*from  w w  w.j a  va2 s  . c o  m*/

    if (param instanceof ParametersWithRandom) {
        ParametersWithRandom randomedParam = (ParametersWithRandom) param;

        key = (RSAKeyParameters) randomedParam.getParameters();
    } else {
        key = (RSAKeyParameters) param;
    }

    RSAPrivateKeySpec keySpec = null;
    if (key instanceof RSAPrivateCrtKeyParameters) {
        RSAPrivateCrtKeyParameters params = (RSAPrivateCrtKeyParameters) key;
        keySpec = new RSAPrivateCrtKeySpec(params.getModulus(), // modulus
                params.getPublicExponent(), // publicExponent
                params.getExponent(), // privateExponent
                params.getP(), // primeP
                params.getQ(), // primeQ
                params.getDP(), // primeExponentP
                params.getDQ(), // primeExponentQ
                params.getQInv());// crtCoefficient
    } else {
        RSAKeyParameters params = (RSAKeyParameters) key;
        keySpec = new RSAPrivateKeySpec(params.getModulus(), params.getExponent());
    }
    RSAPrivateKey signingKey;
    try {
        signingKey = (RSAPrivateKey) rsaKeyFactory.generatePrivate(keySpec);
    } catch (InvalidKeySpecException ex) {
        throw new RuntimeCryptoException("could not generate RSA private key from param: " + ex.getMessage());
    }

    try {
        cipher.init(Cipher.ENCRYPT_MODE, signingKey);
    } catch (InvalidKeyException ex) {
        LogUtil.error(LOG, ex);
        throw new RuntimeCryptoException("could not initialize the cipher: " + ex.getMessage());
    }
}