List of usage examples for org.bouncycastle.crypto.params ParametersWithIV ParametersWithIV
public ParametersWithIV(CipherParameters parameters, byte[] iv)
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:at.archistar.crypto.symmetric.ChaCha20Encryptor.java
@Override public byte[] encrypt(byte[] data, byte[] randomKeyBytes) throws IOException, InvalidKeyException, InvalidAlgorithmParameterException, InvalidCipherTextException, ImpossibleException { ChaChaEngine cipher = new ChaChaEngine(); cipher.init(true, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes)); byte[] result = new byte[data.length]; cipher.processBytes(data, 0, data.length, result, 0); return result; }
From source file:at.archistar.crypto.symmetric.ChaCha20Encryptor.java
@Override public byte[] decrypt(byte[] data, byte[] randomKeyBytes) throws InvalidKeyException, InvalidAlgorithmParameterException, IOException, ImpossibleException, IllegalStateException, InvalidCipherTextException { ChaChaEngine cipher = new ChaChaEngine(); cipher.init(false, new ParametersWithIV(new KeyParameter(randomKeyBytes), randomIvBytes)); byte[] result = new byte[data.length]; cipher.processBytes(data, 0, data.length, result, 0); return result; }
From source file:cc.agentx.security.AesCipher.java
License:Apache License
@Override protected void _init(boolean isEncrypt, byte[] iv) { String keyStr = new String(key.getEncoded()); ParametersWithIV params = new ParametersWithIV( new KeyParameter(KeyHelper.generateKeyDigest(keyLength, keyStr)), iv); cipher.init(isEncrypt, params);//from ww w . j a va 2 s .c o m }
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);/* w w w .j av a2 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:ch.lamacrypt.internal.crypto.CPCipher.java
License:Open Source License
/** * Encrypts a given file with ChaCha20 w/ Poly1305 as MAC in the * encrypt-then-MAC scheme./*from ww w . j a va 2 s . c om*/ * <p> * After each chunk of 8KiB, a MAC tag is written out. This means the output * file size is increased by 16B/8192B = 0.001953125%. * * @param key * @param nonce * @param input * @param output * @throws IOException */ protected void encrypt(byte[] key, byte[] nonce, InputStream input, OutputStream output) throws IOException { this.cipher.init(true, new ParametersWithIV(new KeyParameter(key), nonce)); byte[] ciphertextMac = new byte[16], readBuf = new byte[BUFFER_SIZE], chachaBuf = new byte[BUFFER_SIZE]; initMAC(cipher); int r = 0; while ((r = input.read(readBuf)) != -1) { cipher.processBytes(readBuf, 0, r, chachaBuf, 0); output.write(chachaBuf, 0, r); updateMAC(chachaBuf, 0, r); } mac.doFinal(ciphertextMac, 0); output.write(ciphertextMac); }
From source file:ch.lamacrypt.internal.crypto.CPCipher.java
License:Open Source License
/** * Decrypts a given file with ChaCha20 w/ Poly1305 as MAC in * encrypt-then-MAC scheme./* www.j av a2s .c o m*/ * <p> * Reads data from the InputStream and writes the decrypted data to the * OutputStream * * @param key * @param nonce * @param input * @param output * @throws IOException */ protected void decrypt(byte[] key, byte[] nonce, InputStream input, OutputStream output) throws IOException { this.cipher.init(false, new ParametersWithIV(new KeyParameter(key), nonce)); byte[] computedMac = new byte[16], receivedMac = new byte[16], readBuf = new byte[BUFFER_SIZE], chachaBuf = new byte[BUFFER_SIZE]; initMAC(cipher); int r = 0; while ((r = input.read(readBuf)) != -1) { if (r == BUFFER_SIZE) { // use C in whole to update the MAC and decrypt updateMAC(readBuf, 0, r); cipher.processBytes(readBuf, 0, r, chachaBuf, 0); output.write(chachaBuf, 0, r); } else { // use all but the last 16 bytes from C to update the MAC and decrypt updateMAC(Arrays.copyOfRange(readBuf, 0, r - 16), 0, r - 16); cipher.processBytes(Arrays.copyOfRange(readBuf, 0, r - 16), 0, r - 16, chachaBuf, 0); output.write(chachaBuf, 0, r - 16); // copy the last 16 bytes as the original MAC receivedMac = Arrays.copyOfRange(readBuf, r - 16, r); } } // check if the two MACs match mac.doFinal(computedMac, 0); if (!Arrays.constantTimeAreEqual(computedMac, receivedMac)) { throw new TlsFatalAlert(AlertDescription.bad_record_mac); } }
From source file:co.lqnt.lockbox.DecryptionCipher.java
License:Open Source License
/** * Decrypt some data with AES and PKCS #7 padding. * * @param key The key to use./*from w ww. j a v a2 s . c o m*/ * @param iv The initialization vector to use. * @param data The data to decrypt. * * @return The decrypted data. * @throws DecryptionFailedException If the decryption failed. */ protected byte[] decryptAes(final byte[] key, final byte[] iv, final byte[] data) throws DecryptionFailedException { CipherParameters parameters = new ParametersWithIV(new KeyParameter(key), iv); this.aesCipher().reset(); try { this.aesCipher().init(false, parameters); } catch (IllegalArgumentException e) { throw new DecryptionFailedException(e); } int outputSize = this.aesCipher().getOutputSize(data.length); byte[] decrypted = new byte[outputSize]; int length = this.aesCipher().processBytes(data, 0, data.length, decrypted, 0); try { length += this.aesCipher().doFinal(decrypted, length); } catch (InvalidCipherTextException e) { throw new DecryptionFailedException(e); } catch (DataLengthException e) { throw new DecryptionFailedException(e); } return Arrays.copyOfRange(decrypted, 0, length); }
From source file:co.lqnt.lockbox.EncryptionCipher.java
License:Open Source License
/** * Encrypt some data with AES and PKCS #7 padding. * * @param key The key to use.//from w ww .ja va 2 s. com * @param iv The initialization vector to use. * @param data The data to encrypt. * * @return The decrypted data. */ protected byte[] encryptAes(final byte[] key, final byte[] iv, final byte[] data) { CipherParameters parameters = new ParametersWithIV(new KeyParameter(key), iv); this.aesCipher().reset(); this.aesCipher().init(true, parameters); int outputSize = this.aesCipher().getOutputSize(data.length); byte[] encrypted = new byte[outputSize]; int length = this.aesCipher().processBytes(data, 0, data.length, encrypted, 0); try { length += this.aesCipher().doFinal(encrypted, length); } catch (InvalidCipherTextException e) { throw new RuntimeException(e); } catch (DataLengthException e) { throw new RuntimeException(e); } return Arrays.copyOfRange(encrypted, 0, length); }