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

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

Introduction

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

Prototype

public IESWithCipherParameters(byte[] derivation, byte[] encoding, int macKeySize, int cipherKeySize) 

Source Link

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 ww.  j  a va  2 s. co 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:dorkbox.util.crypto.CryptoECC.java

License:Apache License

/**
 * AES-256 ONLY!/* w ww  .ja  va  2 s .c  o  m*/
 */
public static IESWithCipherParameters generateSharedParametersWithCipher(SecureRandom secureRandom) {
    int macSize = CryptoECC.macSize; // must be the MAC size

    byte[] derivation = new byte[macSize / 8]; // MUST be random EACH TIME encrypt/sign happens!
    byte[] encoding = new byte[macSize / 8];

    secureRandom.nextBytes(derivation);
    secureRandom.nextBytes(encoding);

    return new IESWithCipherParameters(derivation, encoding, macSize, 256);
}

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

License:Apache License

@SuppressWarnings("rawtypes")
@Override//from w w w. j a  v  a2 s  .  c  om
public IESWithCipherParameters read(Kryo kryo, Input input, Class type) {
    int length;

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

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

    /////////////
    int macKeySize = input.readInt(true);

    /////////////
    int cipherKeySize = input.readInt(true);

    return new IESWithCipherParameters(derivation, encoding, macKeySize, cipherKeySize);
}

From source file:net.nharyes.secrete.ecies.ECIES.java

License:Open Source License

private static ECIESMessage encryptData(PublicKey key, byte[] data, boolean binary, SecureRandom random)
        throws ECIESException {

    try {//from w  w  w.j  a va  2s. c o m

        // check key algorithm
        if (!key.getAlgorithm().equals(ASYMMETRIC_ALGORITHM))
            throw new ECIESException("Wrong key algorithm");

        // generate shared information
        byte[] sh1 = new byte[SHARED_INFORMATION_SIZE_BYTES];
        random.nextBytes(sh1);
        byte[] sh2 = new byte[SHARED_INFORMATION_SIZE_BYTES];
        random.nextBytes(sh2);
        byte[] iv = new byte[IV_SIZE_BYTES];
        random.nextBytes(iv);

        // generate R
        byte[] r = new byte[Curve25519.KEY_SIZE];
        random.nextBytes(r);
        byte[] R = new byte[Curve25519.KEY_SIZE];
        Curve25519.curve(R, r, null);

        // IES engine
        IESEngine ies = getIESEngine();

        // initialize engine
        Curve25519EncryptionParameter ep = new Curve25519EncryptionParameter(key.getEncoded(), r);
        ParametersWithIV p = new ParametersWithIV(
                new IESWithCipherParameters(sh1, sh2, MAC_KEY_SIZE_BITS, AES_KEY_SIZE_BITS), iv);
        ies.init(true, null, ep, p);

        // encrypt data
        byte[] cd = ies.processBlock(data, 0, data.length);

        // return message
        return new ECIESMessage(sh1, sh2, iv, R, cd, binary);

    } catch (InvalidCipherTextException ex) {

        throw new ECIESException("Message corrupted or wrong key", ex);
    }
}

From source file:net.nharyes.secrete.ecies.ECIES.java

License:Open Source License

public static byte[] decryptMessage(PrivateKey key, ECIESMessage message) throws ECIESException {

    try {/*from   ww  w  . j av a 2  s.c om*/

        // check key algorithm
        if (!key.getAlgorithm().equals(ASYMMETRIC_ALGORITHM))
            throw new ECIESException("Wrong key algorithm");

        // IES engine
        IESEngine ies = getIESEngine();

        // initialize engine
        Curve25519DecryptionParameter ep = new Curve25519DecryptionParameter(key.getEncoded(), message.getR());
        ParametersWithIV p = new ParametersWithIV(new IESWithCipherParameters(message.getSh1(),
                message.getSh2(), MAC_KEY_SIZE_BITS, AES_KEY_SIZE_BITS), message.getIv());
        ies.init(false, null, ep, p);

        // decrypt and return data
        return ies.processBlock(message.getCd(), 0, message.getCd().length);

    } catch (InvalidCipherTextException ex) {

        throw new ECIESException("Message corrupted or wrong key", ex);
    }
}

From source file:org.ethereum.crypto.CryptoTest.java

License:Open Source License

@Test // ECIES_AES128_SHA256 + No Ephemeral Key + IV(all zeroes)
public void test14() throws Throwable {

    AESEngine aesEngine = new AESEngine();

    IESEngine iesEngine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()),
            new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesEngine)));

    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 IESWithCipherParameters(d, e, 64, 128);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[16]);

    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());

    eGen.init(gParam);//  w w w  .j  ava 2s  . com

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

    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);

    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom()));

    iesEngine.init(true, p1.getPrivate(), p2.getPublic(), parametersWithIV);

    byte[] message = Hex.decode("010101");
    log.info("payload: {}", Hex.toHexString(message));

    byte[] cipher = iesEngine.processBlock(message, 0, message.length);
    log.info("cipher: {}", Hex.toHexString(cipher));

    IESEngine decryptorIES_Engine = new IESEngine(new ECDHBasicAgreement(),
            new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()),
            new BufferedBlockCipher(new SICBlockCipher(aesEngine)));

    decryptorIES_Engine.init(false, p2.getPrivate(), p1.getPublic(), parametersWithIV);

    byte[] orig = decryptorIES_Engine.processBlock(cipher, 0, cipher.length);

    log.info("orig: " + Hex.toHexString(orig));
}

From source file:org.ethereum.crypto.CryptoTest.java

License:Open Source License

@Test // ECIES_AES128_SHA256 + Ephemeral Key + IV(all zeroes)
public void test15() throws Throwable {

    byte[] privKey = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1");

    ECKey ecKey = ECKey.fromPrivate(privKey);

    ECPrivateKeyParameters ecPrivKey = new ECPrivateKeyParameters(ecKey.getPrivKey(), ECKey.CURVE);
    ECPublicKeyParameters ecPubKey = new ECPublicKeyParameters(ecKey.getPubKeyPoint(), ECKey.CURVE);

    AsymmetricCipherKeyPair myKey = new AsymmetricCipherKeyPair(ecPubKey, ecPrivKey);

    AESEngine aesEngine = new AESEngine();

    IESEngine iesEngine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()),
            new HMac(new SHA256Digest()), new BufferedBlockCipher(new SICBlockCipher(aesEngine)));

    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 IESWithCipherParameters(d, e, 64, 128);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[16]);

    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());

    eGen.init(gParam);// w  w w .j a v  a  2s.co  m

    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom());
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);

    EphemeralKeyPairGenerator kGen = new EphemeralKeyPairGenerator(generator, new KeyEncoder() {
        public byte[] getEncoded(AsymmetricKeyParameter keyParameter) {
            return ((ECPublicKeyParameters) keyParameter).getQ().getEncoded();
        }
    });

    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, new SecureRandom()));

    iesEngine.init(myKey.getPublic(), parametersWithIV, kGen);

    byte[] message = Hex.decode("010101");
    log.info("payload: {}", Hex.toHexString(message));

    byte[] cipher = iesEngine.processBlock(message, 0, message.length);
    log.info("cipher: {}", Hex.toHexString(cipher));

    IESEngine decryptorIES_Engine = new IESEngine(new ECDHBasicAgreement(),
            new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()),
            new BufferedBlockCipher(new SICBlockCipher(aesEngine)));

    decryptorIES_Engine.init(myKey.getPrivate(), parametersWithIV, new ECIESPublicKeyParser(ECKey.CURVE));

    byte[] orig = decryptorIES_Engine.processBlock(cipher, 0, cipher.length);

    log.info("orig: " + Hex.toHexString(orig));
}

From source file:org.ethereum.crypto.ECIESCoder.java

License:Open Source License

public static byte[] decrypt(ECPoint ephem, BigInteger prv, byte[] iv, byte[] cipher, byte[] macData)
        throws InvalidCipherTextException {
    AESEngine aesEngine = new AESEngine();

    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(),
            new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(),
            new BufferedBlockCipher(new SICBlockCipher(aesEngine)));

    byte[] d = new byte[] {};
    byte[] e = new byte[] {};

    IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, iv);

    iesEngine.init(false, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(ephem, CURVE),
            parametersWithIV);//w  w  w . j av  a2 s. c  o  m

    return iesEngine.processBlock(cipher, 0, cipher.length, macData);
}

From source file:org.ethereum.crypto.ECIESCoder.java

License:Open Source License

private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] iv) {
    AESEngine aesEngine = new AESEngine();

    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(),
            new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(),
            new BufferedBlockCipher(new SICBlockCipher(aesEngine)));

    byte[] d = new byte[] {};
    byte[] e = new byte[] {};

    IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, iv);

    iesEngine.init(isEncrypt, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(pub, CURVE),
            parametersWithIV);// w w  w.  ja  va 2  s.  co  m
    return iesEngine;
}

From source file:org.ethereum.crypto.ECIESTest.java

License:Open Source License

private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] IV) {
    AESEngine aesEngine = new AESEngine();

    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(),
            new ConcatKDFBytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest()), new SHA256Digest(),
            new BufferedBlockCipher(new SICBlockCipher(aesEngine)));

    byte[] d = new byte[] {};
    byte[] e = new byte[] {};

    IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, IV);

    iesEngine.init(isEncrypt, new ECPrivateKeyParameters(prv, curve), new ECPublicKeyParameters(pub, curve),
            parametersWithIV);//from   w  w w  . j av a 2s.c  om
    return iesEngine;
}