List of usage examples for org.bouncycastle.crypto.engines AESEngine AESEngine
public AESEngine()
From source file:org.cryptacular.util.CipherUtilTest.java
License:Open Source License
@DataProvider(name = "aead-block-cipher") public Object[][] getAeadBlockCipherData() { return new Object[][] { new Object[] { // Plaintext is NOT multiple of block size "I never picked cotton like my mother did", new GCMBlockCipher(new AESEngine()), }, new Object[] { // Plaintext is multiple of block size "Cogito ergo sum.", new GCMBlockCipher(new AESEngine()), }, // CCM new Object[] { "Thousands of candles can be lit from a single candle and the life " + "of the candle will not be shortened.", new CCMBlockCipher(new TwofishEngine()), }, // OCB new Object[] { "I slept and dreamt life was joy. I awoke and saw that life was " + "service. I acted and behold: service was joy.", new OCBBlockCipher(new AESEngine(), new AESEngine()), }, }; }
From source file:org.cryptacular.util.CipherUtilTest.java
License:Open Source License
@Test(dataProvider = "plaintext-files") public void testBlockCipherEncryptDecryptStream(final String path) throws Exception { final BlockCipher cipher = new CBCBlockCipher(new AESEngine()); final SecretKey key = SecretKeyGenerator.generate(cipher); final Nonce nonce = new CounterNonce("vt-crypt", 1); final File file = new File(path); final String expected = new String(StreamUtil.readAll(file)); final ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); CipherUtil.encrypt(cipher, key, nonce, StreamUtil.makeStream(file), tempOut); final ByteArrayInputStream tempIn = new ByteArrayInputStream(tempOut.toByteArray()); final ByteArrayOutputStream actual = new ByteArrayOutputStream(); CipherUtil.decrypt(cipher, key, tempIn, actual); assertEquals(new String(actual.toByteArray()), expected); }
From source file:org.cryptacular.util.CipherUtilTest.java
License:Open Source License
@Test(dataProvider = "plaintext-files") public void testAeadBlockCipherEncryptDecryptStream(final String path) throws Exception { final AEADBlockCipher cipher = new GCMBlockCipher(new AESEngine()); final SecretKey key = SecretKeyGenerator.generate(cipher.getUnderlyingCipher()); final File file = new File(path); final String expected = new String(StreamUtil.readAll(file)); final ByteArrayOutputStream tempOut = new ByteArrayOutputStream(); CipherUtil.encrypt(cipher, key, new RBGNonce(), StreamUtil.makeStream(file), tempOut); final ByteArrayInputStream tempIn = new ByteArrayInputStream(tempOut.toByteArray()); final ByteArrayOutputStream actual = new ByteArrayOutputStream(); CipherUtil.decrypt(cipher, key, tempIn, actual); assertEquals(new String(actual.toByteArray()), expected); }
From source file:org.ethereum.crypto.CryptoTest.java
License:Open Source License
@Test // basic encryption/decryption public void test11() throws Throwable { byte[] keyBytes = HashUtil.keccak256("...".getBytes()); log.info("key: {}", Hex.toHexString(keyBytes)); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode("22400891000000000000000000000000"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, new byte[16]); AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); ctrEngine.init(true, params);/* ww w . j a v a 2 s .c om*/ byte[] cipher = new byte[16]; ctrEngine.processBlock(payload, 0, cipher, 0); log.info("cipher: {}", Hex.toHexString(cipher)); byte[] output = new byte[cipher.length]; ctrEngine.init(false, params); ctrEngine.processBlock(cipher, 0, output, 0); assertEquals(Hex.toHexString(output), Hex.toHexString(payload)); log.info("original: {}", Hex.toHexString(payload)); }
From source file:org.ethereum.crypto.CryptoTest.java
License:Open Source License
@Test // big packet encryption public void test12() throws Throwable { AESEngine engine = new AESEngine(); SICBlockCipher ctrEngine = new SICBlockCipher(engine); byte[] keyBytes = Hex.decode("a4627abc2a3c25315bff732cb22bc128f203912dd2a840f31e66efb27a47d2b1"); byte[] ivBytes = new byte[16]; byte[] payload = Hex.decode( "0109efc76519b683d543db9d0991bcde99cc9a3d14b1d0ecb8e9f1f66f31558593d746eaa112891b04ef7126e1dce17c9ac92ebf39e010f0028b8ec699f56f5d0c0d00"); byte[] cipherText = Hex.decode( "f9fab4e9dd9fc3e5d0d0d16da254a2ac24df81c076e3214e2c57da80a46e6ae4752f4b547889fa692b0997d74f36bb7c047100ba71045cb72cfafcc7f9a251762cdf8f"); KeyParameter key = new KeyParameter(keyBytes); ParametersWithIV params = new ParametersWithIV(key, ivBytes); ctrEngine.init(true, params);//from w ww .j av a2 s.c o m byte[] in = payload; byte[] out = new byte[in.length]; int i = 0; while (i < in.length) { ctrEngine.processBlock(in, i, out, i); i += engine.getBlockSize(); if (in.length - i < engine.getBlockSize()) break; } // process left bytes if (in.length - i > 0) { byte[] tmpBlock = new byte[16]; System.arraycopy(in, i, tmpBlock, 0, in.length - i); ctrEngine.processBlock(tmpBlock, 0, tmpBlock, 0); System.arraycopy(tmpBlock, 0, out, i, in.length - i); } log.info("cipher: {}", Hex.toHexString(out)); assertEquals(Hex.toHexString(cipherText), Hex.toHexString(out)); }
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);//from w ww. j a v a 2 s. c o m 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);//from w w w . j a v a 2 s . c o 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 ww. j a v a 2 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);/* ww w .j a v a2 s . c o 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 ww w. j a v a 2 s . c o m*/ return iesEngine; }