List of usage examples for org.bouncycastle.crypto.modes CBCBlockCipher CBCBlockCipher
public CBCBlockCipher(BlockCipher cipher)
From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java
License:Apache License
@Override public OutputStream encryptedOutputStream(final Path path, final String password) throws IOException, EncryptionFailedException { try {/*ww w.j a v a2 s .co m*/ final byte[] salt = generateSalt(); final byte[] key = generateKey(password, salt); final byte[] iv = generateIV(); final byte[] fileInitBlock = generateOutputInitBlock(salt, iv); final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); final KeyParameter keyParam = new KeyParameter(key); final CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(true, params); final BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(path)); out.write(fileInitBlock); return new CipherOutputStream(out, cipher); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new EncryptionFailedException(e); } }
From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java
License:Apache License
@Override public String encryptString(final String string, final String password) throws EncryptionFailedException { try {/*from ww w . j a v a 2s. c o m*/ final byte[] salt = generateSalt(); final byte[] key = generateKey(password, salt); final byte[] iv = generateIV(); final byte[] outputInitBlock = generateOutputInitBlock(salt, iv); final PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); final KeyParameter keyParam = new KeyParameter(key); final CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(true, params); final byte in[] = string.getBytes(); final byte out[] = new byte[cipher.getOutputSize(in.length)]; final int len1 = cipher.processBytes(in, 0, in.length, out, 0); cipher.doFinal(out, len1); final byte[] result = Arrays.concatenate(outputInitBlock, out); return Base64.toBase64String(result); } catch (InvalidKeySpecException | NoSuchAlgorithmException | InvalidCipherTextException e) { throw new EncryptionFailedException(e); } }
From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java
License:Apache License
@Override public InputStream decryptedInputStream(final Path path, final String password) throws IOException, DecryptionFailedException { try {// w w w. j a v a 2s.c o m InputStream in = new BufferedInputStream(Files.newInputStream(path)); byte[] initBlock = readInitBlock(in); byte[] salt = extractSalt(initBlock); byte[] iv = extractIV(initBlock); byte[] key = generateKey(password, salt); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); KeyParameter keyParam = new KeyParameter(key); CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(false, params); return new CipherInputStream(in, cipher); } catch (InvalidKeySpecException | NoSuchAlgorithmException e) { throw new DecryptionFailedException(e); } }
From source file:com.github.flbaue.jcrypttool.v2.domain.AesEncryptionService.java
License:Apache License
@Override public String decryptString(final String string, final String password) throws DecryptionFailedException { try {//from www . ja v a 2 s.co m byte[] inputBytes = Base64.decode(string); byte[] salt = extractSalt(inputBytes); byte[] iv = extractIV(inputBytes); byte[] key = generateKey(password, salt); byte[] encryptedData = extractEncryptedData(inputBytes); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()), new PKCS7Padding()); KeyParameter keyParam = new KeyParameter(key); CipherParameters params = new ParametersWithIV(keyParam, iv); cipher.init(false, params); final byte out[] = new byte[cipher.getOutputSize(encryptedData.length)]; int length = cipher.processBytes(encryptedData, 0, encryptedData.length, out, 0); byte[] result; length += cipher.doFinal(out, length); result = new byte[length]; System.arraycopy(out, 0, result, 0, length); return new String(result); } catch (InvalidCipherTextException | InvalidKeySpecException | NoSuchAlgorithmException e) { throw new DecryptionFailedException(e); } }
From source file:com.github.horrorho.inflatabledonkey.crypto.AESCBC.java
License:Open Source License
public static byte[] decryptAESCBC(byte[] key, byte[] iv, byte[] data) { // AES CBC PKCS7 decrypt try {//w w w . ja v a 2 s . c o m CipherParameters cipherParameters = new ParametersWithIV(new KeyParameter(key), iv); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher( new CBCBlockCipher(new AESFastEngine()), new PKCS7Padding()); cipher.init(false, cipherParameters); byte[] buffer = new byte[cipher.getOutputSize(data.length)]; int pos = cipher.processBytes(data, 0, data.length, buffer, 0); pos += cipher.doFinal(buffer, pos); return Arrays.copyOf(buffer, pos); } catch (DataLengthException | IllegalStateException | InvalidCipherTextException ex) { throw new IllegalArgumentException("decrypt failed", ex); } }
From source file:com.github.horrorho.inflatabledonkey.dataprotection.DPAESCBCCipher.java
License:Open Source License
public DPAESCBCCipher(int blockSize) { this(new CBCBlockCipher(new AESEngine()), blockSize); }
From source file:com.github.horrorho.inflatabledonkey.pcs.xfile.BlockDecrypters.java
License:Open Source License
public static BlockDecrypter create(byte[] key) { BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); SHA1Digest digest = new SHA1Digest(); return create(cipher, digest, key); }
From source file:com.github.horrorho.liquiddonkey.cloud.file.FileDecrypter.java
License:Open Source License
/** * Returns a new instance./*from w ww .j a v a2s . c om*/ * * @return a new instance, not null */ public static FileDecrypter create() { return FileDecrypter.from(new BufferedBlockCipher(new CBCBlockCipher(new AESEngine())), new SHA1Digest()); }
From source file:com.google.bitcoin.crypto.KeyCrypterScrypt.java
License:MIT License
/** * Password based encryption using AES - CBC 256 bits. *///ww w. j ava 2 s. co m @Override public EncryptedPrivateKey encrypt(byte[] plainBytes, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(plainBytes); checkNotNull(aesKey); try { // Generate iv - each encryption call has a different iv. byte[] iv = new byte[BLOCK_LENGTH]; secureRandom.nextBytes(iv); ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, iv); // Encrypt using AES. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); 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 EncryptedPrivateKey(iv, Arrays.copyOf(encryptedBytes, length1 + length2)); } catch (Exception e) { throw new KeyCrypterException("Could not encrypt bytes.", e); } }
From source file:com.google.bitcoin.crypto.KeyCrypterScrypt.java
License:MIT License
/** * Decrypt bytes previously encrypted with this class. * * @param privateKeyToDecode The private key to decrypt * @param aesKey The AES key to use for decryption * @return The decrypted bytes * @throws KeyCrypterException if bytes could not be decoded to a valid key *///from w w w .ja va 2 s . c om @Override public byte[] decrypt(EncryptedPrivateKey privateKeyToDecode, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(privateKeyToDecode); checkNotNull(aesKey); try { ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), privateKeyToDecode.getInitialisationVector()); // Decrypt the message. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, keyWithIv); byte[] cipherBytes = privateKeyToDecode.getEncryptedBytes(); 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); } }