Example usage for org.bouncycastle.crypto.engines IESEngine getMac

List of usage examples for org.bouncycastle.crypto.engines IESEngine getMac

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.engines IESEngine getMac.

Prototype

public Mac getMac() 

Source Link

Usage

From source file:com.DSC.crypto.Cipher.java

License:Open Source License

/**
 * //from  w w  w.  j a v a 2 s .  co  m
 * @param priKey
 * @param pubKey
 * @param passphrase
 * @param data
 * @return
 * @throws InvalidCipherTextException
 */
public static byte[] encryptKey(CipherParameters priKey, CipherParameters pubKey, String passphrase,
        byte[] data) throws InvalidCipherTextException {
    /* Initialize IESEngine in stream mode */
    IESEngine engine = new IESEngine(new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()),
            new HMac(new SHA256Digest()));

    /* Set the IESEngine cipher parameters as the passphrase and passphrase reversed */
    IESParameters param = new IESParameters(passphrase.getBytes(),
            new StringBuilder(passphrase).reverse().toString().getBytes(), engine.getMac().getMacSize() * 8);

    /* Initialize the engine and encrypt the key */
    engine.init(true, priKey, pubKey, param);
    return engine.processBlock(data, 0, data.length);
}

From source file:com.DSC.crypto.Cipher.java

License:Open Source License

/**
 * /*  w  w w. j  a v a 2 s  . com*/
 * @param priKey
 * @param pubKey
 * @param passphrase
 * @param data
 * @return
 * @throws InvalidCipherTextException
 */
public static byte[] decryptKey(CipherParameters priKey, CipherParameters pubKey, String passphrase,
        byte[] data) throws InvalidCipherTextException {
    /* IESEngine in stream mode */
    IESEngine engine = new IESEngine(new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()),
            new HMac(new SHA256Digest()));

    /* Set the IESEngine cipher parameters as the passphrase and passphrase reversed */
    IESParameters param = new IESParameters(passphrase.getBytes(),
            new StringBuilder(passphrase).reverse().toString().getBytes(), engine.getMac().getMacSize() * 8);

    /* Initialize the engine and decrypt the key */
    engine.init(false, priKey, pubKey, param);
    return engine.processBlock(data, 0, data.length);
}