List of usage examples for org.bouncycastle.crypto BufferedBlockCipher getOutputSize
public int getOutputSize(int length)
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 av a 2 s. c o 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.rsk.crypto.KeyCrypterAes.java
License:Open Source License
/** * Password based encryption using AES - CBC 256 bits. *///w w w . j a v a2s . co 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 w w . ja va 2 s . 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:com.android.isoma.enc.Encryption.java
License:Open Source License
public byte[] callCipher(BufferedBlockCipher cipher, byte[] data) throws DataLengthException, IllegalStateException, InvalidCipherTextException { int size = cipher.getOutputSize(data.length); byte[] result = new byte[size]; int olen = cipher.processBytes(data, 0, data.length, result, 0); int last = cipher.doFinal(result, olen); final byte[] plain = new byte[olen + last]; System.arraycopy(result, 0, plain, 0, plain.length); // cipher.reset(); return plain; }
From source file:com.completetrsst.crypto.Crypto.java
License:Apache License
private static byte[] process(byte[] input, BufferedBlockCipher bufferedBlockCipher, CipherParameters cipherParameters, boolean forEncryption) throws InvalidCipherTextException { bufferedBlockCipher.init(forEncryption, cipherParameters); int inputOffset = 0; int inputLength = input.length; int maximumOutputLength = bufferedBlockCipher.getOutputSize(inputLength); byte[] output = new byte[maximumOutputLength]; int outputOffset = 0; int outputLength = 0; int bytesProcessed; bytesProcessed = bufferedBlockCipher.processBytes(input, inputOffset, inputLength, output, outputOffset); outputOffset += bytesProcessed;//w w w. jav a2s. com outputLength += bytesProcessed; bytesProcessed = bufferedBlockCipher.doFinal(output, outputOffset); outputOffset += bytesProcessed; outputLength += bytesProcessed; if (outputLength == output.length) { return output; } else { byte[] truncatedOutput = new byte[outputLength]; System.arraycopy(output, 0, truncatedOutput, 0, outputLength); return truncatedOutput; } }
From source file:com.giacomodrago.immediatecrypt.aes.AESFacadeImpl.java
License:Open Source License
@Override public AESEncryptedMessage encrypt(byte[] plaintext, String password) throws EncryptionException { // Check password is not empty if (password.isEmpty()) { throw new EncryptionException("Password is empty."); }/*from w w w .ja va 2 s . c om*/ // Generate random password salt String salt = RandomStringUtils.randomAlphanumeric(SALT_LENGTH); ParametersWithIV params = createEncryptionParameters(password, salt); byte[] iv = params.getIV(); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); cipher.init(true, params); byte[] ciphertext = new byte[cipher.getOutputSize(plaintext.length)]; int outputLen = cipher.processBytes(plaintext, 0, plaintext.length, ciphertext, 0); try { cipher.doFinal(ciphertext, outputLen); } catch (DataLengthException ex) { throw new EncryptionException(ex); } catch (IllegalStateException ex) { throw new EncryptionException(ex); } catch (InvalidCipherTextException ex) { throw new EncryptionException(ex); } return new AESEncryptedMessage(salt, iv, ciphertext); }
From source file:com.giacomodrago.immediatecrypt.aes.AESFacadeImpl.java
License:Open Source License
@Override public byte[] decrypt(AESEncryptedMessage encryptedMessage, String password) throws EncryptionException { byte[] ciphertext = encryptedMessage.getCiphertext(); String salt = encryptedMessage.getSalt(); byte[] iv = encryptedMessage.getIv(); ParametersWithIV params = createDecryptionParameters(password, salt, iv); BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())); try {//w w w. ja v a2 s .c o m cipher.init(false, params); } catch (IllegalArgumentException ex) { throw new EncryptionException(ex); } byte[] plaintext = new byte[cipher.getOutputSize(ciphertext.length)]; int outputLen = cipher.processBytes(ciphertext, 0, ciphertext.length, plaintext, 0); try { cipher.doFinal(plaintext, outputLen); } catch (DataLengthException ex) { throw new EncryptionException(ex); } catch (IllegalStateException ex) { throw new EncryptionException(ex); } catch (InvalidCipherTextException ex) { throw new EncryptionException(ex); } return plaintext; }
From source file:com.google.bitcoin.crypto.KeyCrypterScrypt.java
License:MIT License
/** * Password based encryption using AES - CBC 256 bits. *///from w w w . j a v a 2 s . c o 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 ww w. ja v a 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); } }
From source file:com.gpfcomics.android.cryptnos.ImportExportHandler.java
License:Open Source License
/** * Given a cipher and the size of an input file, determine whether or not we have * enough memory on hand to encrypt or decrypt the data. Since we have to do all * our cryptography in memory, we can only work with the amount of memory * currently available.//from ww w .ja v a 2s . c o m * @param cipher The BufferedBlockCipher we'll be using to encrypt/decrypt * @param fileSize The size of the input file in bytes * @param caller The calling activity * @return True if there's sufficient memory to decrypt the file, false otherwise */ private static boolean haveSufficientMemory(BufferedBlockCipher cipher, boolean encrypting, long fileSize, Activity caller) { // There are other factors that should eliminate this before we get to this // step, but we can't deal with a file larger than 2GB. There aren't any // current Android devices with that much RAM anyway. if (fileSize > (long) Integer.MAX_VALUE) return false; // As an error check, make sure the cipher and caller objects are not null: if (cipher == null || caller == null) return false; // Now put on our asbestos underpants: try { // Get the memory information from the activity service: MemoryInfo mi = new MemoryInfo(); ActivityManager activityManager = (ActivityManager) caller.getSystemService(Context.ACTIVITY_SERVICE); activityManager.getMemoryInfo(mi); // In order to decrypt the file, we'll need at least as many bytes as // the encrypted and decrypted data combined. In order to get this, // we'll add the size of the input file to the output size of the data // after it passes through the cipher. Note that it doesn't matter // whether we're doing encryption or decryption at this point; the cipher // object knows and the output size will be appropriate for the output // mode. If the sum of these two values is less than the available // memory, we should be good to go. //return mi.availMem > fileSize + (long)cipher.getOutputSize((int)fileSize); if (encrypting) return mi.availMem > fileSize + (long) cipher.getOutputSize((int) fileSize); else return mi.availMem > (long) cipher.getBlockSize() + (long) cipher.getOutputSize((int) fileSize); } catch (Exception e) { return false; } }