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

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

Introduction

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

Prototype

public IESParameters(byte[] derivation, byte[] encoding, int macKeySize) 

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);//w ww  .ja v  a2s  .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

/**
 * /*w w w .  j  av a2  s. c o 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 ww.java2 s  . c om
 * @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

/**
 * These parameters are shared between the two parties. These are a NONCE (use ONCE number!!)
 *///from ww w .j  ava 2s.c o m
public static IESParameters generateSharedParameters(SecureRandom secureRandom) {

    int macSize = CryptoECC.macSize; // must be the MAC size

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

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

    return new IESParameters(derivation, encoding, macSize);
}

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

License:Apache License

@SuppressWarnings("rawtypes")
@Override/*from   w ww .j  a v  a  2s.  c  o m*/
public IESParameters 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);

    return new IESParameters(derivation, encoding, macKeySize);
}

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

License:Open Source License

/**
 *  Encryption equivalent to the Crypto++ default ECIES<ECP> settings:
 *
 *  DL_KeyAgreementAlgorithm:        DL_KeyAgreementAlgorithm_DH<struct ECPPoint,struct EnumToType<enum CofactorMultiplicationOption,0> >
 *  DL_KeyDerivationAlgorithm:       DL_KeyDerivationAlgorithm_P1363<struct ECPPoint,0,class P1363_KDF2<class SHA1> >
 *  DL_SymmetricEncryptionAlgorithm: DL_EncryptionAlgorithm_Xor<class HMAC<class SHA1>,0>
 *  DL_PrivateKey:                   DL_Key<ECPPoint>
 *  DL_PrivateKey_EC<class ECP>//from   w  ww .j ava 2s.co m
 *
 *  Used for Whisper V3
 */
public static byte[] decryptSimple(BigInteger privKey, byte[] cipher)
        throws IOException, InvalidCipherTextException {
    EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(),
            new MGF1BytesGeneratorExt(new SHA1Digest(), 1), new HMac(new SHA1Digest()), new SHA1Digest(), null);

    IESParameters p = new IESParameters(null, null, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[0]);

    iesEngine.setHashMacKey(false);

    iesEngine.init(new ECPrivateKeyParameters(privKey, CURVE), parametersWithIV,
            new ECIESPublicKeyParser(ECKey.CURVE));

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

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

License:Open Source License

protected byte[] doFinal(byte[] bytes, IESParameters params, boolean encrypt) {
    try {//from w ww.ja  v  a 2s .c o  m
        final IESEngine engine = initialiseEngine(encrypt, privateKeyParameter, publicKeyParameter,
                new IESParameters(params.getDerivationV(), params.getEncodingV(), params.getMacKeySize()));
        return engine.processBlock(bytes, 0, bytes.length);
    } catch (InvalidCipherTextException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

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

License:Open Source License

protected IESParameters generateIESParameterSpec() {
    byte[] d = new byte[16];
    byte[] e = new byte[16];
    random.nextBytes(d);//from  www.j a  v a2  s . c  om
    random.nextBytes(e);
    return new IESParameters(d, e, 128);
}

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

License:Open Source License

@Override
public byte[] decrypt(byte[] bytes) {

    byte[] d = new byte[16];
    byte[] e = new byte[16];

    System.arraycopy(bytes, 0, d, 0, 16);
    System.arraycopy(bytes, 16, e, 0, 16);

    IESParameters param = new IESParameters(d, e, 128);

    return doFinal(bytes, param, false);
}