List of usage examples for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher
public CBCBlockCipher(BlockCipher cipher)
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);/*ww w . j av a 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:at.archistar.crypto.symmetric.AESEncryptor.java
@Override public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidCipherTextException { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes)); return cipherData(cipher, data); }
From source file:at.archistar.crypto.symmetric.AESEncryptor.java
@Override public byte[] decrypt(byte[] data, byte[] randomKeyBytes) throws InvalidKeyException, InvalidAlgorithmParameterException, IOException, IllegalStateException, InvalidCipherTextException { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes)); return cipherData(cipher, data); }
From source file:ch.dissem.bitmessage.cryptography.bc.BouncyCryptography.java
License:Apache License
@Override public byte[] crypt(boolean encrypt, byte[] data, byte[] key_e, byte[] initializationVector) { BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); CipherParameters params = new ParametersWithIV(new KeyParameter(key_e), initializationVector); cipher.init(encrypt, params);//from w w w . j a va2 s .co m byte[] buffer = new byte[cipher.getOutputSize(data.length)]; int length = cipher.processBytes(data, 0, data.length, buffer, 0); try { length += cipher.doFinal(buffer, length); } catch (InvalidCipherTextException e) { throw new IllegalArgumentException(e); } if (length < buffer.length) { return Arrays.copyOfRange(buffer, 0, length); } return buffer; }
From source file:co.lqnt.lockbox.Cipher.java
License:Open Source License
/** * Construct a new bi-directional cipher. */// ww w. j a va 2s .co m public Cipher() { CodecInterface base64UriCodec = new Base64UriCodec(); AsymmetricBlockCipher rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest()); BufferedBlockCipher aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); Digest sha1Digest = new SHA1Digest(); SecureRandom random = new SecureRandom(); this.encryptionCipher = new EncryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest, random); this.decryptionCipher = new DecryptionCipher(base64UriCodec, rsaCipher, aesCipher, sha1Digest); }
From source file:co.lqnt.lockbox.DecryptionCipher.java
License:Open Source License
/** * Construct a new decryption cipher./*from w ww . j av a 2 s.c om*/ */ public DecryptionCipher() { this.base64UriCodec = new Base64UriCodec(); this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest()); this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); this.sha1Digest = new SHA1Digest(); this.asciiCharset = Charset.forName("US-ASCII"); }
From source file:co.lqnt.lockbox.EncryptionCipher.java
License:Open Source License
/** * Construct a new encryption cipher.//w w w . j a v a 2s . c o m */ public EncryptionCipher() { this.base64UriCodec = new Base64UriCodec(); this.rsaCipher = new OAEPEncoding(new RSAEngine(), new SHA1Digest()); this.aesCipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); this.sha1Digest = new SHA1Digest(); this.random = new SecureRandom(); this.asciiCharset = Charset.forName("US-ASCII"); }
From source file:co.rsk.crypto.KeyCrypterAes.java
License:Open Source License
/** * Password based encryption using AES - CBC 256 bits. *///from w ww . ja va2 s . c o m @Override public EncryptedData encrypt(byte[] plainBytes, KeyParameter key) { checkNotNull(plainBytes); checkNotNull(key); try { // Generate iv - each encryption call has a different iv. byte[] iv = new byte[BLOCK_LENGTH]; secureRandom.nextBytes(iv); ParametersWithIV keyWithIv = new ParametersWithIV(key, iv); // Encrypt using AES. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(true, keyWithIv); byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)]; final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0); final int length2 = cipher.doFinal(encryptedBytes, length1); return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2)); } catch (Exception e) { throw new KeyCrypterException("Could not encrypt bytes.", e); } }
From source file:co.rsk.crypto.KeyCrypterAes.java
License:Open Source License
/** * Decrypt bytes previously encrypted with this class. * * @param dataToDecrypt The data to decrypt * @param key The AES key to use for decryption * @return The decrypted bytes * @throws KeyCrypterException if bytes could not be decrypted *//*from w ww .ja v a 2s . c o m*/ @Override public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter key) { checkNotNull(dataToDecrypt); checkNotNull(key); try { ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(key.getKey()), dataToDecrypt.initialisationVector); // Decrypt the message. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(false, keyWithIv); byte[] cipherBytes = dataToDecrypt.encryptedBytes; byte[] decryptedBytes = new byte[cipher.getOutputSize(cipherBytes.length)]; final int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, decryptedBytes, 0); final int length2 = cipher.doFinal(decryptedBytes, length1); return Arrays.copyOf(decryptedBytes, length1 + length2); } catch (Exception e) { throw new KeyCrypterException("Could not decrypt bytes", e); } }
From source file:cologne.eck.dr.op.crypto.password_hashing.Battcrypt_v0.java
License:Open Source License
@Override public byte[] hashPassword(int outlen, byte[] in, byte[] salt, int t_cost, int m_cost, Object... varArgs) throws DataLengthException, IllegalStateException, InvalidCipherTextException { SHA512Digest sha = new SHA512Digest(); int[] data = new int[DATA_SIZE_INT]; BlowfishEngine blowfish;//from w ww. java 2 s . co m long upgradeLoops = 1; long loops; int memSize = 4 << m_cost;//= 4 * 2 ** m_cost int memMask = memSize - 1; int[] mem; byte[] hashBuffer = new byte[HASH_LENGTH_BYTE];// holds hash value as bytes byte[] dataBuffer = new byte[DATA_SIZE_BYTE];// holds encrypted bytes // These are the PHP max. values if (m_cost > 18 || // maximum: 2.147.483.648 bytes (t_cost & 0xffff) > 62 || (t_cost >> 16) > 63 || outlen > HASH_LENGTH_BYTE) { throw new IllegalArgumentException("invalid parameters"); } int tmp = t_cost >> 16; if (tmp != 0) { // upgradeLoops = 1, 2, 3, 4, 6, 8, 12, 16, ... upgradeLoops = (long) (3 - (tmp & 1)) << ((tmp - 1) >> 1); } // loops = 2, 3, 4, 6, 8, 12, 16, ... tmp = t_cost & 0xffff; loops = (long) ((tmp & 1) + 2) << (tmp >> 1); // key = SHA512(SHA512(salt) || in) byte[] keyBytes = new byte[HASH_LENGTH_BYTE]; sha.update(salt, 0, salt.length); sha.doFinal(keyBytes, 0); sha.reset(); sha.update(keyBytes, 0, HASH_LENGTH_BYTE); sha.update(in, 0, in.length);//password sha.doFinal(keyBytes, 0); sha.reset(); if (wipePassword == true) { Arrays.fill(in, (byte) 0); } // initialize cipher with 448 bit (56 byte) key: // truncate keyBytes: byte[] blowfishKey = new byte[56]; System.arraycopy(keyBytes, 0, blowfishKey, 0, 56); // use zeros as IV byte[] iv = new byte[IV_LENGTH_BYTE]; KeyParameter params = new KeyParameter(blowfishKey); Arrays.fill(blowfishKey, (byte) 0); ParametersWithIV ivParams = new ParametersWithIV(params, iv); blowfish = new BlowfishEngine(); // CBC, no padding: all vectors are multiples of Blowfish block length BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(blowfish)); cipher.init(true, ivParams); // initialize memory-hard vector: mem = new int[DATA_SIZE_INT * memSize]; for (long u = 0; u < upgradeLoops; u++) { // initialize data: // data = SHA512(BIG_ENDIAN_64( 0) || key) || ... // ... || SHA512(BIG_ENDIAN_64(31) || key) byte[] counterBytesBE = new byte[8]; // holds counter as long to update for (int i = 0; i < DATA_SIZE_BYTE / HASH_LENGTH_BYTE; i++) { counterBytesBE[7] = (byte) i; // set first byte sha.update(counterBytesBE, 0, counterBytesBE.length); // BIG_ENDIAN_64(i) sha.update(keyBytes, 0, HASH_LENGTH_BYTE); sha.doFinal(hashBuffer, 0); sha.reset(); // hash values allow weak garbage collector attack - // so, avoid new allocations: for (int j = 0; j < HASH_LENGTH_BYTE / 4; j++) { data[HASH_LENGTH_INT * i + j] = ((hashBuffer[j * 4 + 3] & 0xFF) << 24) | ((hashBuffer[j * 4 + 2] & 0xFF) << 16) | ((hashBuffer[j * 4 + 1] & 0xFF) << 8) | (hashBuffer[j * 4 + 0] & 0xFF); // little endian order } Arrays.fill(hashBuffer, (byte) 0); } // Initialize memory: for (int i = 0; i < memSize; i++) { // data = blowfish_encrypt_cbc(data) // mem = mem || data for (int j = 0; j < DATA_SIZE_INT; j++) { dataBuffer[j * 4 + 0] = (byte) (data[j]);// little endian dataBuffer[j * 4 + 1] = (byte) (data[j] >>> 8); dataBuffer[j * 4 + 2] = (byte) (data[j] >>> 16); dataBuffer[j * 4 + 3] = (byte) (data[j] >>> 24); } int len = cipher.processBytes(dataBuffer, 0, DATA_SIZE_BYTE, dataBuffer, 0); cipher.doFinal(dataBuffer, len); cipher.reset(); // get iv for next encryption step: // "running CBC": the last block of the // previous call is the IV for the next call System.arraycopy(dataBuffer, DATA_SIZE_BYTE - IV_LENGTH_BYTE, iv, 0, IV_LENGTH_BYTE); ivParams = new ParametersWithIV(params, iv); cipher.init(true, ivParams); for (int j = 0; j < DATA_SIZE_INT; j++) { data[j] = ((dataBuffer[j * 4 + 3] & 0xFF) << 24) | ((dataBuffer[j * 4 + 2] & 0xFF) << 16) | ((dataBuffer[j * 4 + 1] & 0xFF) << 8) | (dataBuffer[j * 4 + 0] & 0xFF); // little endian order } System.arraycopy(data, 0, mem, DATA_SIZE_INT * i, DATA_SIZE_INT); } // encrypt data: for (int j = 0; j < DATA_SIZE_INT; j++) { dataBuffer[j * 4 + 0] = (byte) (data[j]);// little endian dataBuffer[j * 4 + 1] = (byte) (data[j] >>> 8); dataBuffer[j * 4 + 2] = (byte) (data[j] >>> 16); dataBuffer[j * 4 + 3] = (byte) (data[j] >>> 24); } int len = cipher.processBytes(dataBuffer, 0, DATA_SIZE_BYTE, dataBuffer, 0); cipher.doFinal(dataBuffer, len); cipher.reset(); System.arraycopy(dataBuffer, DATA_SIZE_BYTE - IV_LENGTH_BYTE, iv, 0, IV_LENGTH_BYTE); ivParams = new ParametersWithIV(params, iv); cipher.init(true, ivParams); for (int j = 0; j < DATA_SIZE_INT; j++) { data[j] = ((dataBuffer[j * 4 + 3] & 0xFF) << 24) | ((dataBuffer[j * 4 + 2] & 0xFF) << 16) | ((dataBuffer[j * 4 + 1] & 0xFF) << 8) | (dataBuffer[j * 4 + 0] & 0xFF); // little endian order } // work: for (long i = 0; i < loops; i++) { for (int j = 0; j < memSize; j++) { // in the C++ reference implementation and the paper // this rValue a 64 bit integer, but this makes only a // difference for memSize > 0xFFFFFFFF +1, while the // recommended maximum for memSize is 2^32 int rValue = ((((int) data[DATA_SIZE_INT - 1]) << 24) & 0xff000000) | ((((int) data[DATA_SIZE_INT - 1]) << 8) & 0x00ff0000) | ((((int) data[DATA_SIZE_INT - 1]) >>> 8) & 0x0000ff00) | ((((int) data[DATA_SIZE_INT - 1]) >>> 24) & 0x000000ff); int index = (int) (DATA_SIZE_INT * (rValue & memMask)); for (int k = 0; k < DATA_SIZE_INT; k++) { mem[j * DATA_SIZE_INT + k] ^= data[k] ^ mem[index + k]; } // convert to byte: for (int k = 0; k < DATA_SIZE_INT; k++) { dataBuffer[k * 4 + 0] = (byte) (mem[j * DATA_SIZE_INT + k]); dataBuffer[k * 4 + 1] = (byte) (mem[j * DATA_SIZE_INT + k] >>> 8); dataBuffer[k * 4 + 2] = (byte) (mem[j * DATA_SIZE_INT + k] >>> 16); dataBuffer[k * 4 + 3] = (byte) (mem[j * DATA_SIZE_INT + k] >>> 24); } int len1 = cipher.processBytes(dataBuffer, 0, DATA_SIZE_BYTE, dataBuffer, 0); cipher.doFinal(dataBuffer, len1); cipher.reset(); // get iv for next step: System.arraycopy(dataBuffer, DATA_SIZE_BYTE - IV_LENGTH_BYTE, iv, 0, IV_LENGTH_BYTE); for (int k = 0; k < DATA_SIZE_INT; k++) { mem[j * DATA_SIZE_INT + k] = ((dataBuffer[k * 4 + 3] & 0xFF) << 24) | ((dataBuffer[k * 4 + 2] & 0xFF) << 16) | ((dataBuffer[k * 4 + 1] & 0xFF) << 8) | (dataBuffer[k * 4 + 0] & 0xFF); // little endian order } ivParams = new ParametersWithIV(params, iv); cipher.init(true, ivParams); // data ^= mem[j] for (int k = 0; k < DATA_SIZE_INT; k++) { data[k] ^= mem[DATA_SIZE_INT * j + k]; } } } // Finish // key = truncate(SHA512(SHA512(data || key)), outlen) || zeros(HASH_LENGTH - outlen) // convert to byte: for (int k = 0; k < DATA_SIZE_INT; k++) { dataBuffer[k * 4 + 0] = (byte) (data[k]); dataBuffer[k * 4 + 1] = (byte) (data[k] >>> 8); dataBuffer[k * 4 + 2] = (byte) (data[k] >>> 16); dataBuffer[k * 4 + 3] = (byte) (data[k] >>> 24); } sha.update(dataBuffer, 0, DATA_SIZE_BYTE); sha.update(keyBytes, 0, HASH_LENGTH_BYTE); sha.doFinal(keyBytes, 0); sha.reset(); } sha.update(keyBytes, 0, HASH_LENGTH_BYTE); sha.doFinal(keyBytes, 0); sha.reset(); byte[] out = new byte[outlen]; System.arraycopy(keyBytes, 0, out, 0, out.length); // Clean-up: Arrays.fill(keyBytes, (byte) 0); Arrays.fill(dataBuffer, (byte) 0); Arrays.fill(iv, (byte) 0); Arrays.fill(data, 0); Arrays.fill(mem, (byte) 0); // wipe the key from parameters Arrays.fill(params.getKey(), (byte) 0); // prevent dead code eliminations (compiler optimizations): if ((keyBytes[HASH_LENGTH_BYTE - 1] | blowfishKey[blowfishKey.length - 1] | dataBuffer[DATA_SIZE_BYTE - 1] | hashBuffer[HASH_LENGTH_BYTE - 1] | data[DATA_SIZE_INT - 1] | iv[IV_LENGTH_BYTE - 1] | mem[mem.length - 1] | params.getKey()[params.getKey().length - 1]) != 0) { System.err.print("zeroization failed!"); } if ((wipePassword == true) && (in[in.length - 1] != 0)) { System.err.print("zeroization failed!"); } return out; }