Search in sources :

Example 1 with ECKeyPairGenerator

use of org.bouncycastle.crypto.generators.ECKeyPairGenerator in project rskj by rsksmart.

the class CryptoTest method test14.

// ECIES_AES128_SHA256 + No Ephemeral Key + IV(all zeroes)
@Test
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);
    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: {}", ByteUtil.toHexString(message));
    byte[] cipher = iesEngine.processBlock(message, 0, message.length);
    log.info("cipher: {}", ByteUtil.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: " + ByteUtil.toHexString(orig));
}
Also used : ECKeyPairGenerator(org.bouncycastle.crypto.generators.ECKeyPairGenerator) AESEngine(org.bouncycastle.crypto.engines.AESEngine) HMac(org.bouncycastle.crypto.macs.HMac) SecureRandom(java.security.SecureRandom) SICBlockCipher(org.bouncycastle.crypto.modes.SICBlockCipher) KeyGenerationParameters(org.bouncycastle.crypto.KeyGenerationParameters) IESEngine(org.bouncycastle.crypto.engines.IESEngine) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) ECDHBasicAgreement(org.bouncycastle.crypto.agreement.ECDHBasicAgreement) KDF2BytesGenerator(org.bouncycastle.crypto.generators.KDF2BytesGenerator) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) Test(org.junit.Test)

Example 2 with ECKeyPairGenerator

use of org.bouncycastle.crypto.generators.ECKeyPairGenerator in project rskj by rsksmart.

the class ECIESTest method encrypt.

public static byte[] encrypt(ECPoint toPub, byte[] plaintext) throws InvalidCipherTextException, IOException {
    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    SecureRandom random = new SecureRandom();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(curve, random);
    eGen.init(gParam);
    byte[] IV = new byte[KEY_SIZE / 8];
    new SecureRandom().nextBytes(IV);
    AsymmetricCipherKeyPair ephemPair = eGen.generateKeyPair();
    BigInteger prv = ((ECPrivateKeyParameters) ephemPair.getPrivate()).getD();
    ECPoint pub = ((ECPublicKeyParameters) ephemPair.getPublic()).getQ();
    EthereumIESEngine iesEngine = makeIESEngine(true, toPub, prv, IV);
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(curve, random);
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);
    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, random));
    byte[] cipher = iesEngine.processBlock(plaintext, 0, plaintext.length);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bos.write(pub.getEncoded(false));
    bos.write(IV);
    bos.write(cipher);
    return bos.toByteArray();
}
Also used : ECKeyPairGenerator(org.bouncycastle.crypto.generators.ECKeyPairGenerator) SecureRandom(java.security.SecureRandom) BigInteger(java.math.BigInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ECPoint(org.bouncycastle.math.ec.ECPoint)

Example 3 with ECKeyPairGenerator

use of org.bouncycastle.crypto.generators.ECKeyPairGenerator in project rskj by rsksmart.

the class ECIESCoder method encrypt.

public static byte[] encrypt(ECPoint toPub, byte[] plaintext, byte[] macData) {
    ECKeyPairGenerator eGen = new ECKeyPairGenerator();
    SecureRandom random = new SecureRandom();
    KeyGenerationParameters gParam = new ECKeyGenerationParameters(CURVE, random);
    eGen.init(gParam);
    byte[] iv = new byte[KEY_SIZE / 8];
    new SecureRandom().nextBytes(iv);
    AsymmetricCipherKeyPair ephemPair = eGen.generateKeyPair();
    BigInteger prv = ((ECPrivateKeyParameters) ephemPair.getPrivate()).getD();
    ECPoint pub = ((ECPublicKeyParameters) ephemPair.getPublic()).getQ();
    EthereumIESEngine iesEngine = makeIESEngine(true, toPub, prv, iv);
    ECKeyGenerationParameters keygenParams = new ECKeyGenerationParameters(CURVE, random);
    ECKeyPairGenerator generator = new ECKeyPairGenerator();
    generator.init(keygenParams);
    ECKeyPairGenerator gen = new ECKeyPairGenerator();
    gen.init(new ECKeyGenerationParameters(ECKey.CURVE, random));
    byte[] cipher;
    try {
        cipher = iesEngine.processBlock(plaintext, 0, plaintext.length, macData);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write(pub.getEncoded(false));
        bos.write(iv);
        bos.write(cipher);
        return bos.toByteArray();
    } catch (InvalidCipherTextException e) {
        throw Throwables.propagate(e);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
Also used : ECKeyPairGenerator(org.bouncycastle.crypto.generators.ECKeyPairGenerator) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) SecureRandom(java.security.SecureRandom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ECPoint(org.bouncycastle.math.ec.ECPoint) KeyGenerationParameters(org.bouncycastle.crypto.KeyGenerationParameters) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) BigInteger(java.math.BigInteger)

Example 4 with ECKeyPairGenerator

use of org.bouncycastle.crypto.generators.ECKeyPairGenerator in project rskj by rsksmart.

the class CryptoTest method test15.

// ECIES_AES128_SHA256 + Ephemeral Key + IV(all zeroes)
@Test
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);
    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: {}", ByteUtil.toHexString(message));
    byte[] cipher = iesEngine.processBlock(message, 0, message.length);
    log.info("cipher: {}", ByteUtil.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: " + ByteUtil.toHexString(orig));
}
Also used : ECKeyPairGenerator(org.bouncycastle.crypto.generators.ECKeyPairGenerator) EphemeralKeyPairGenerator(org.bouncycastle.crypto.generators.EphemeralKeyPairGenerator) KeyEncoder(org.bouncycastle.crypto.KeyEncoder) AESEngine(org.bouncycastle.crypto.engines.AESEngine) HMac(org.bouncycastle.crypto.macs.HMac) SecureRandom(java.security.SecureRandom) SICBlockCipher(org.bouncycastle.crypto.modes.SICBlockCipher) KeyGenerationParameters(org.bouncycastle.crypto.KeyGenerationParameters) AsymmetricCipherKeyPair(org.bouncycastle.crypto.AsymmetricCipherKeyPair) IESEngine(org.bouncycastle.crypto.engines.IESEngine) ECDHBasicAgreement(org.bouncycastle.crypto.agreement.ECDHBasicAgreement) KDF2BytesGenerator(org.bouncycastle.crypto.generators.KDF2BytesGenerator) SHA256Digest(org.bouncycastle.crypto.digests.SHA256Digest) BufferedBlockCipher(org.bouncycastle.crypto.BufferedBlockCipher) ECIESPublicKeyParser(org.bouncycastle.crypto.parsers.ECIESPublicKeyParser) Test(org.junit.Test)

Aggregations

SecureRandom (java.security.SecureRandom)4 ECKeyPairGenerator (org.bouncycastle.crypto.generators.ECKeyPairGenerator)4 AsymmetricCipherKeyPair (org.bouncycastle.crypto.AsymmetricCipherKeyPair)3 KeyGenerationParameters (org.bouncycastle.crypto.KeyGenerationParameters)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BigInteger (java.math.BigInteger)2 BufferedBlockCipher (org.bouncycastle.crypto.BufferedBlockCipher)2 ECDHBasicAgreement (org.bouncycastle.crypto.agreement.ECDHBasicAgreement)2 SHA256Digest (org.bouncycastle.crypto.digests.SHA256Digest)2 AESEngine (org.bouncycastle.crypto.engines.AESEngine)2 IESEngine (org.bouncycastle.crypto.engines.IESEngine)2 KDF2BytesGenerator (org.bouncycastle.crypto.generators.KDF2BytesGenerator)2 HMac (org.bouncycastle.crypto.macs.HMac)2 SICBlockCipher (org.bouncycastle.crypto.modes.SICBlockCipher)2 ECPoint (org.bouncycastle.math.ec.ECPoint)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 InvalidCipherTextException (org.bouncycastle.crypto.InvalidCipherTextException)1 KeyEncoder (org.bouncycastle.crypto.KeyEncoder)1 EphemeralKeyPairGenerator (org.bouncycastle.crypto.generators.EphemeralKeyPairGenerator)1