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

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

Introduction

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

Prototype

public RSAPrivateCrtKeyParameters(BigInteger modulus, BigInteger publicExponent, BigInteger privateExponent,
        BigInteger p, BigInteger q, BigInteger dP, BigInteger dQ, BigInteger qInv) 

Source Link

Usage

From source file:ch.bfh.unicert.certimport.CertificateIssuer.java

License:GNU General Public License

/**
 * Given a private RSA key creates an object containing all relevant cipher parameters.
 *
 * @param rsaPrivKey a RSA private key/*from w  w  w  . j a v  a  2s.  com*/
 * @return the cipher parameters
 * @throws CertificateCreationException if there is an error
 */
private RSAPrivateCrtKeyParameters createIssuerCipherParams(RSAPrivateCrtKey rsaPrivKey)
        throws CertificateCreationException {
    RSAPrivateCrtKeyParameters cipherParams;
    try {
        cipherParams = new RSAPrivateCrtKeyParameters(rsaPrivKey.getModulus(), rsaPrivKey.getPublicExponent(),
                rsaPrivKey.getPrivateExponent(), rsaPrivKey.getPrimeP(), rsaPrivKey.getPrimeQ(),
                rsaPrivKey.getPrimeExponentP(), rsaPrivKey.getPrimeExponentQ(), rsaPrivKey.getCrtCoefficient());
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "Could not get issuer cipher parameters: {0}",
                new Object[] { ex.getMessage() });
        throw new CertificateCreationException("200 Could not get issuer cipher parameters");
    }
    return cipherParams;
}

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

License:Open Source License

/**
 * Get the Bouncy Castle RSA private key parameters.
 *
 * @return The Bouncy Castle RSA private key parameters.
 *///  w  w w. ja  va  2  s .  c om
public RSAPrivateCrtKeyParameters bcPrivateKeyParameters() {
    return new RSAPrivateCrtKeyParameters(this.modulus(), this.publicExponent(), this.privateExponent(),
            this.prime1(), this.prime2(), this.primeExponent1(), this.primeExponent2(), this.coefficient());
}

From source file:com.foilen.smalltools.crypt.bouncycastle.asymmetric.RSACrypt.java

License:Open Source License

@Override
public AsymmetricKeys createKeyPair(RSAKeyDetails keyDetails) {

    if (keyDetails.getModulus() == null && keyDetails.getPrivateExponent() == null
            && keyDetails.getPublicExponent() == null) {
        return null;
    }//from  ww  w.j ava2s. c  om

    BigInteger modulus = keyDetails.getModulus();
    AssertTools.assertNotNull(modulus, "The modulus must be present");

    AsymmetricKeys asymmetricKeys = new AsymmetricKeys();

    try {

        BigInteger publicExponent = keyDetails.getPublicExponent();
        BigInteger privateExponent = keyDetails.getPrivateExponent();
        if (publicExponent != null) {
            RSAKeyParameters publicKeyParameters = new RSAKeyParameters(false, modulus, publicExponent);
            asymmetricKeys.setPublicKey(publicKeyParameters);
        }

        if (privateExponent != null) {
            RSAKeyParameters privateKeyParameters;
            if (keyDetails.isCrt()) {
                privateKeyParameters = new RSAPrivateCrtKeyParameters(modulus, publicExponent, privateExponent,
                        keyDetails.getPrimeP(), keyDetails.getPrimeQ(), keyDetails.getPrimeExponentP(),
                        keyDetails.getPrimeExponentQ(), keyDetails.getCrtCoefficient());
            } else {
                privateKeyParameters = new RSAKeyParameters(true, modulus, privateExponent);
            }

            asymmetricKeys.setPrivateKey(privateKeyParameters);
        }

        return asymmetricKeys;

    } catch (Exception e) {
        throw new SmallToolsException("Could not create the keys", e);
    }
}

From source file:com.github.chuckbuckethead.cypher.keys.RSAKey.java

License:Open Source License

/**
 * @param key/*from   ww  w.jav  a2s.  co  m*/
 * @return
 */
private static RSAKeyParameters generatePrivateKeyParameter(RSAPrivateKey key) {
    RSAKeyParameters parameters = null;

    if (key instanceof RSAPrivateCrtKey) {
        RSAPrivateCrtKey crtKey = (RSAPrivateCrtKey) key;
        parameters = new RSAPrivateCrtKeyParameters(crtKey.getModulus(), crtKey.getPublicExponent(),
                crtKey.getPrivateExponent(), crtKey.getPrimeP(), crtKey.getPrimeQ(), crtKey.getPrimeExponentP(),
                crtKey.getPrimeExponentQ(), crtKey.getCrtCoefficient());
    } else {
        parameters = new RSAKeyParameters(true, key.getModulus(), key.getPrivateExponent());
    }

    return parameters;
}

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

License:Apache License

public CipherParameters getParameters() {
    if (!isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }/*from w w  w.j  ava  2s  . c  om*/
    // modulus = p * q;
    return new RSAPrivateCrtKeyParameters(p.getBigInteger().multiply(q.getBigInteger()), null, null,
            p.getBigInteger(), q.getBigInteger(), dp1.getBigInteger(), dq1.getBigInteger(), pq.getBigInteger());
}

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

License:Apache License

@SuppressWarnings("rawtypes")
@Override/*from  w  ww .ja  v a  2s .c om*/
public RSAPrivateCrtKeyParameters read(Kryo kryo, Input input, Class type) {
    byte[] bytes;
    int length;

    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger DP = new BigInteger(bytes);

    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger DQ = new BigInteger(bytes);

    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger exponent = new BigInteger(bytes);

    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger modulus = new BigInteger(bytes);

    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger P = new BigInteger(bytes);

    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger publicExponent = new BigInteger(bytes);

    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger q = new BigInteger(bytes);

    /////////////
    length = input.readInt(true);
    bytes = new byte[length];
    input.readBytes(bytes, 0, length);
    BigInteger qInv = new BigInteger(bytes);

    return new RSAPrivateCrtKeyParameters(modulus, publicExponent, exponent, P, q, DP, DQ, qInv);
}

From source file:frost.crypt.FrostCrypt.java

License:Open Source License

public synchronized String detachedSign(byte[] message, String key) {

    StringTokenizer keycutter = new StringTokenizer(key, ":");
    RSAPrivateCrtKeyParameters privKey = new RSAPrivateCrtKeyParameters(
            new BigInteger(Base64.decode(keycutter.nextToken())),
            new BigInteger(Base64.decode(keycutter.nextToken())),
            new BigInteger(Base64.decode(keycutter.nextToken())),
            new BigInteger(Base64.decode(keycutter.nextToken())),
            new BigInteger(Base64.decode(keycutter.nextToken())),
            new BigInteger(Base64.decode(keycutter.nextToken())),
            new BigInteger(Base64.decode(keycutter.nextToken())),
            new BigInteger(Base64.decode(keycutter.nextToken())));

    signer.init(true, privKey);//from  w w  w .j  a v a2s. c o  m
    signer.update(message, 0, message.length);

    byte[] signature = null;
    try {
        signature = signer.generateSignature();
    } catch (CryptoException e) {
        Logger.error(this, "Exception thrown in detachedSign(String message, String key): " + e.toString());
    }
    signer.reset();
    try {
        String result = new String(Base64.encode(signature), "ISO-8859-1");
        return result;
    } catch (UnsupportedEncodingException ex) {
        Logger.error(this, "ISO-8859-1 encoding is not supported: " + ex.toString());
    }
    return null;
}

From source file:hh.learnj.test.license.test.lincense3j.RSAUtil.java

public static byte[] rsaPriDecrypt(RSAPrivateKey prvKeyIn, byte[] encodedBytes, int type) {

    RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) prvKeyIn;

    BigInteger mod = prvKey.getModulus();
    BigInteger pubExp = prvKey.getPublicExponent();
    BigInteger privExp = prvKey.getPrivateExponent();
    BigInteger pExp = prvKey.getPrimeExponentP();
    BigInteger qExp = prvKey.getPrimeExponentQ();
    BigInteger p = prvKey.getPrimeP();
    BigInteger q = prvKey.getPrimeQ();
    BigInteger crtCoef = prvKey.getCrtCoefficient();

    RSAKeyParameters privParameters = new RSAPrivateCrtKeyParameters(mod, pubExp, privExp, p, q, pExp, qExp,
            crtCoef);/*  w  ww. j  a  v  a 2 s  .c  o m*/

    AsymmetricBlockCipher eng = new RSAEngine();
    if (type == PKCS1)
        eng = new PKCS1Encoding(eng);

    eng.init(false, privParameters);
    byte[] data = null;
    try {
        data = eng.processBlock(encodedBytes, 0, encodedBytes.length);
        return data;
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }

}

From source file:hh.learnj.test.license.test.lincense3j.RSAUtil.java

public static byte[] rsaPriEncrypt(RSAPrivateKey prvKeyIn, byte[] encodedBytes, int type) {
    RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) prvKeyIn;
    BigInteger mod = prvKey.getModulus();
    BigInteger pubExp = prvKey.getPublicExponent();
    BigInteger privExp = prvKey.getPrivateExponent();
    BigInteger pExp = prvKey.getPrimeExponentP();
    BigInteger qExp = prvKey.getPrimeExponentQ();
    BigInteger p = prvKey.getPrimeP();
    BigInteger q = prvKey.getPrimeQ();
    BigInteger crtCoef = prvKey.getCrtCoefficient();
    RSAKeyParameters privParameters = new RSAPrivateCrtKeyParameters(mod, pubExp, privExp, p, q, pExp, qExp,
            crtCoef);//  www.  j av  a 2  s  . c  o m
    AsymmetricBlockCipher eng = new RSAEngine();
    if (type == PKCS1)
        eng = new PKCS1Encoding(eng);
    eng.init(true, privParameters);
    byte[] data = null;
    try {
        data = eng.processBlock(encodedBytes, 0, encodedBytes.length);
        return data;
    } catch (Exception e) {
        System.out.println(e);
        return null;
    }
}

From source file:jazmin.server.relay.udp.webrtc.TlsUtils.java

License:Open Source License

static AsymmetricKeyParameter loadPrivateKeyResource(String resource) throws IOException {
    PemObject pem = loadPemResource(resource);
    if (pem.getType().endsWith("RSA PRIVATE KEY")) {
        RSAPrivateKey rsa = RSAPrivateKey.getInstance(pem.getContent());
        return new RSAPrivateCrtKeyParameters(rsa.getModulus(), rsa.getPublicExponent(),
                rsa.getPrivateExponent(), rsa.getPrime1(), rsa.getPrime2(), rsa.getExponent1(),
                rsa.getExponent2(), rsa.getCoefficient());
    }//  www .  j  a v a  2s. c  om
    if (pem.getType().endsWith("PRIVATE KEY")) {
        return PrivateKeyFactory.createKey(pem.getContent());
    }
    throw new IllegalArgumentException("'resource' doesn't specify a valid private key");
}