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

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

Introduction

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

Prototype

public IESEngine(BasicAgreement agree, DerivationFunction kdf, Mac mac) 

Source Link

Document

Set up for use with stream mode, where the key derivation function is used to provide a stream of bytes to xor with the message.

Usage

From source file:ECIESTest.java

public TestResult perform() {
    SecureRandom random = new SecureRandom();
    ECCurve.Fp curve = new ECCurve.Fp(
            new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q
            new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a
            new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b

    ECDomainParameters params = new ECDomainParameters(curve,
            curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G
            new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n

    ECKeyPairGenerator pGen = new ECKeyPairGenerator();
    ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(params, random);

    pGen.init(genParam);//from  w  w  w. j  ava  2  s  .c o  m

    AsymmetricCipherKeyPair p1 = pGen.generateKeyPair();
    AsymmetricCipherKeyPair p2 = pGen.generateKeyPair();

    //
    // stream test
    //
    IESEngine i1 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()));
    IESEngine i2 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()));
    byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
    IESParameters p = new IESParameters(d, e, 64);

    i1.init(true, p1.getPrivate(), p2.getPublic(), p);
    i2.init(false, p2.getPrivate(), p1.getPublic(), p);

    byte[] message = Hex.decode("1234567890abcdef");

    try {
        byte[] out1 = i1.processBlock(message, 0, message.length);

        byte[] out2 = i2.processBlock(out1, 0, out1.length);

        if (!sameAs(out2, message)) {
            return new SimpleTestResult(false, this.getName() + ": stream cipher test failed");
        }

    } catch (Exception ex) {
        return new SimpleTestResult(false, this.getName() + ": stream cipher test exception " + ex.toString());
    }

    //
    // twofish with IV0 test
    //
    BufferedBlockCipher c1 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine()));
    BufferedBlockCipher c2 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine()));
    i1 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()), c1);
    i2 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()), c2);
    d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
    e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
    p = new IESWithCipherParameters(d, e, 64, 128);

    i1.init(true, p1.getPrivate(), p2.getPublic(), p);
    i2.init(false, p2.getPrivate(), p1.getPublic(), p);

    message = Hex.decode("1234567890abcdef");

    try {
        byte[] out1 = i1.processBlock(message, 0, message.length);

        byte[] out2 = i2.processBlock(out1, 0, out1.length);

        if (!sameAs(out2, message)) {
            return new SimpleTestResult(false, this.getName() + ": twofish cipher test failed");
        }
    } catch (Exception ex) {
        return new SimpleTestResult(false, this.getName() + ": twofish cipher test exception " + ex.toString());
    }

    return new SimpleTestResult(true, this.getName() + ": Okay");
}

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

License:Open Source License

/**
 * /*from   w  ww. j  a  v a 2s .  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

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

From source file:dorkbox.util.crypto.CryptoECC.java

License:Apache License

/**
 * Uses SHA512/*from ww  w. j a v a  2s.co m*/
 */
public static IESEngine createEngine() {
    return new IESEngine(new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA384Digest()),
            new HMac(new SHA512Digest()));
}

From source file:org.forgerock.openicf.framework.remote.security.ECIESEncryptor.java

License:Open Source License

protected IESEngine initialiseEngine(boolean encrypt, CipherParameters privateKey, CipherParameters publicKey,
        IESParameters parameters) {//from   w ww. j  av a  2  s .c o m
    IESEngine engine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()),
            new HMac(new SHA1Digest()));
    engine.init(encrypt, privateKey, publicKey, parameters);
    return engine;
}