Example usage for org.bouncycastle.jcajce.provider.asymmetric.ec IESCipher engineInit

List of usage examples for org.bouncycastle.jcajce.provider.asymmetric.ec IESCipher engineInit

Introduction

In this page you can find the example usage for org.bouncycastle.jcajce.provider.asymmetric.ec IESCipher engineInit.

Prototype

public void engineInit(int opmode, Key key, AlgorithmParameterSpec engineSpec, SecureRandom random)
            throws InvalidAlgorithmParameterException, InvalidKeyException 

Source Link

Usage

From source file:org.certificateservices.custom.c2x.its.crypto.DefaultCryptoManager.java

License:Open Source License

/**
 * Help method to perform a ECIES encryption to a recipient of a symmetric key. 
 * // w  ww .  j av  a  2 s  .c  o  m
 * @param publicKeyAlgorithm the algorithm used.
 * @param encryptionKey the public encryption key of the recipient
 * @param symmetricKey the symmetric key to encrypt
 * @return a EciesNistP256EncryptedKey to be included in a SecureMessage header.
 * 
 * @throws InvalidKeyException if supplied key was corrupt.
 * @throws InvalidAlgorithmParameterException if algorithm was badly specified.
 * @throws IllegalBlockSizeException if encrypted data was corrupt.
 * @throws BadPaddingException if encrypted data was corrupt.
 * @throws IllegalArgumentException if arguments where invalid or algorithm not supported.
 * @throws InvalidKeySpecException if supplied key specification was faulty.
 * @throws IOException if communication problem occurred with underlying systems.
 */

protected EciesNistP256EncryptedKey eCEISEncryptSymmetricKey(PublicKeyAlgorithm publicKeyAlgorithm,
        PublicKey encryptionKey, Key symmetricKey)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
        BadPaddingException, IllegalArgumentException, InvalidKeySpecException, IOException {
    if (publicKeyAlgorithm != PublicKeyAlgorithm.ecies_nistp256) {
        throw new IllegalArgumentException(
                "Unsupported encryption public key algorithm: " + publicKeyAlgorithm);
    }
    byte[] keyData = symmetricKey.getEncoded();

    IESCipher eCIESCipher = new ECIES();
    eCIESCipher.engineInit(Cipher.ENCRYPT_MODE, encryptionKey, new IESParameterSpec(null, null, 128),
            secureRandom);

    byte[] encryptedData = eCIESCipher.engineDoFinal(keyData, 0, keyData.length);
    byte[] v = new byte[ECIES_NIST_P256_V_LENGTH];
    System.arraycopy(encryptedData, 0, v, 0, ECIES_NIST_P256_V_LENGTH);

    EccPoint p = new EccPoint(publicKeyAlgorithm);
    p.deserialize(new DataInputStream(new ByteArrayInputStream(v)));

    byte[] c = new byte[publicKeyAlgorithm.getRelatedSymmetricAlgorithm().getKeyLength()];
    byte[] t = new byte[EciesNistP256EncryptedKey.OUTPUT_TAG_LENGTH];
    System.arraycopy(encryptedData, ECIES_NIST_P256_V_LENGTH, c, 0,
            publicKeyAlgorithm.getRelatedSymmetricAlgorithm().getKeyLength());
    System.arraycopy(encryptedData,
            ECIES_NIST_P256_V_LENGTH + publicKeyAlgorithm.getRelatedSymmetricAlgorithm().getKeyLength(), t, 0,
            EciesNistP256EncryptedKey.OUTPUT_TAG_LENGTH);
    return new EciesNistP256EncryptedKey(publicKeyAlgorithm, p, c, t);
}

From source file:org.certificateservices.custom.c2x.its.crypto.DefaultCryptoManager.java

License:Open Source License

/**
 * Help method to perform a ECIES decryption of a symmetric key. 
 * /*  www  . ja v a2s  .c  om*/
 * @param eciesNistP256EncryptedKey the EciesNistP256EncryptedKey header value from the SecuredMessage
 * @param decryptionKey the receiptients private key
 * @return a decrypted symmetric key.
 * 
 * @throws InvalidKeyException if supplied key was corrupt.
 * @throws InvalidAlgorithmParameterException if algorithm was badly specified.
 * @throws IllegalBlockSizeException if encrypted data was corrupt.
 * @throws BadPaddingException if encrypted data was corrupt.
 * @throws IllegalArgumentException if arguments where invalid or algorithm not supported.
 * @throws InvalidKeySpecException if supplied key specification was faulty.
 * @throws IOException if communication problem occurred with underlying systems.
 */

protected Key eCEISDecryptSymmetricKey(EciesNistP256EncryptedKey eciesNistP256EncryptedKey,
        PrivateKey decryptionKey)
        throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
        BadPaddingException, IllegalArgumentException, InvalidKeySpecException, IOException {
    if (eciesNistP256EncryptedKey.getPublicKeyAlgorithm() != PublicKeyAlgorithm.ecies_nistp256) {
        throw new IllegalArgumentException("Unsupported encryption public key algorithm: "
                + eciesNistP256EncryptedKey.getPublicKeyAlgorithm());
    }

    IESCipher eCIESCipher = new ECIES();
    eCIESCipher.engineInit(Cipher.DECRYPT_MODE, decryptionKey, new IESParameterSpec(null, null, 128),
            secureRandom);

    byte[] encryptedData = new byte[ECIES_NIST_P256_V_LENGTH
            + eciesNistP256EncryptedKey.getPublicKeyAlgorithm().getRelatedSymmetricAlgorithm().getKeyLength()
            + EciesNistP256EncryptedKey.OUTPUT_TAG_LENGTH];
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dis = new DataOutputStream(baos);

    eciesNistP256EncryptedKey.getV().serialize(dis);
    baos.close();
    System.arraycopy(baos.toByteArray(), 0, encryptedData, 0, ECIES_NIST_P256_V_LENGTH);
    System.arraycopy(eciesNistP256EncryptedKey.getC(), 0, encryptedData, ECIES_NIST_P256_V_LENGTH,
            eciesNistP256EncryptedKey.getPublicKeyAlgorithm().getRelatedSymmetricAlgorithm().getKeyLength());
    System.arraycopy(eciesNistP256EncryptedKey.getT(), 0, encryptedData,
            ECIES_NIST_P256_V_LENGTH + eciesNistP256EncryptedKey.getPublicKeyAlgorithm()
                    .getRelatedSymmetricAlgorithm().getKeyLength(),
            EciesNistP256EncryptedKey.OUTPUT_TAG_LENGTH);

    byte[] decryptedData = eCIESCipher.engineDoFinal(encryptedData, 0, encryptedData.length);

    return new SecretKeySpec(decryptedData, "AES");
}