List of usage examples for org.bouncycastle.crypto.engines IESEngine processBlock
public byte[] processBlock(byte[] in, int inOff, int inLen) throws InvalidCipherTextException
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 ww w . j av a2 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:com.DSC.crypto.Cipher.java
License:Open Source License
/** * //from w w w . jav a 2s. com * @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 .ja v a 2s .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
/** * ECC encrypts data with a specified key. * * @param logger/*w w w . j av a 2 s. c om*/ * may be null, if no log output is necessary * * @return empty byte[] if error */ public static byte[] encrypt(IESEngine eccEngine, CipherParameters private1, CipherParameters public2, IESParameters cipherParams, byte[] message, Logger logger) { eccEngine.init(true, private1, public2, cipherParams); //noinspection Duplicates try { return eccEngine.processBlock(message, 0, message.length); } catch (Exception e) { if (logger != null) { logger.error("Unable to perform ECC cipher.", e); } return new byte[0]; } }
From source file:dorkbox.util.crypto.CryptoECC.java
License:Apache License
/** * ECC decrypt data with a specified key. * * @param logger//from w w w. j a v a2 s . co m * may be null, if no log output is necessary * * @return empty byte[] if error */ public static byte[] decrypt(IESEngine eccEngine, CipherParameters private2, CipherParameters public1, IESParameters cipherParams, byte[] encrypted, Logger logger) { eccEngine.init(false, private2, public1, cipherParams); //noinspection Duplicates try { return eccEngine.processBlock(encrypted, 0, encrypted.length); } catch (Exception e) { if (logger != null) { logger.error("Unable to perform ECC cipher.", e); } return new byte[0]; } }
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 v a 2s .co 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 w w w . ja va2 s .c o m // 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);//from w w w . j a v a 2s. 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);// w w w.j a v a2 s. c om 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.forgerock.openicf.framework.remote.security.ECIESEncryptor.java
License:Open Source License
protected byte[] doFinal(byte[] bytes, IESParameters params, boolean encrypt) { try {// w ww. j ava 2 s .c om 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); } }