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

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

Introduction

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

Prototype

public byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen)
            throws IllegalBlockSizeException, BadPaddingException 

Source Link

Usage

From source file:com.completetrsst.crypto.Crypto.java

License:Apache License

private static byte[] _cryptIES(byte[] input, Key recipient, boolean forEncryption)
        throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
    IESCipher cipher = new IESCipher(new IESEngine(new ECDHBasicAgreement(),
            new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA256Digest()),
            new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()))));

    cipher.engineInit(forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, recipient, new SecureRandom());
    return cipher.engineDoFinal(input, 0, input.length);
}

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. 
 * //from  ww w. jav  a2 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. 
 * //w w w.  j a  v a  2  s  .  co m
 * @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");
}