org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher Java Examples
The following examples show how to use
org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KeyCrypterScrypt.java From bcm-android with GNU General Public License v3.0 | 6 votes |
/** * Decrypt bytes previously encrypted with this class. * * @param dataToDecrypt The data to decrypt * @param aesKey The AES key to use for decryption * @return The decrypted bytes * @throws KeyCrypterException if bytes could not be decrypted */ @Override public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(dataToDecrypt); checkNotNull(aesKey); try { ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.initialisationVector); // Decrypt the message. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); 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); } }
Example #2
Source File: SensitiveDataPreApi23.java From android-java-connect-rest-sample with MIT License | 6 votes |
protected byte[] decrypt(byte[] data) { try { SecretKey key = loadKey(); byte[] ivBytes = new byte[16]; // 16 bytes is the IV size for AES256 System.arraycopy(data, 0, ivBytes, 0, ivBytes.length); // Get IV from data byte[] dataWithoutIV = new byte[data.length - ivBytes.length]; // Remove the room made for the IV System.arraycopy(data, ivBytes.length, dataWithoutIV, 0, dataWithoutIV.length); // Then the encrypted data PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(key.getEncoded()), ivBytes)); return cipherData(cipher, dataWithoutIV); } catch(InvalidCipherTextException e) { Log.e(TAG, "Can't decrypt data", e); } return null; }
Example #3
Source File: SensitiveDataPreApi23.java From android-java-connect-rest-sample with MIT License | 6 votes |
protected byte[] encrypt(byte[] data) { // 16 bytes is the IV size for AES256 try { SecretKey key = loadKey(); // Random IV SecureRandom rng = new SecureRandom(); byte[] ivBytes = new byte[16]; // 16 bytes is the IV size for AES256 rng.nextBytes(ivBytes); PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(true, new ParametersWithIV(new KeyParameter(key.getEncoded()), ivBytes)); byte[] encryptedData = cipherData(cipher, data); byte[] encryptedDataWithIV = new byte[encryptedData.length + ivBytes.length]; // Make room for IV System.arraycopy(ivBytes, 0, encryptedDataWithIV, 0, ivBytes.length); // Add IV System.arraycopy(encryptedData, 0, encryptedDataWithIV, ivBytes.length, encryptedData.length); // Then the encrypted data return encryptedDataWithIV; } catch(InvalidCipherTextException e) { Log.e(TAG, "Can't encrypt data", e); } return null; }
Example #4
Source File: KeyCrypterScrypt.java From GreenBits with GNU General Public License v3.0 | 6 votes |
/** * Decrypt bytes previously encrypted with this class. * * @param dataToDecrypt The data to decrypt * @param aesKey The AES key to use for decryption * @return The decrypted bytes * @throws KeyCrypterException if bytes could not be decrypted */ @Override public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(dataToDecrypt); checkNotNull(aesKey); try { ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.initialisationVector); // Decrypt the message. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); 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); } }
Example #5
Source File: KeyCrypterScrypt.java From GreenBits with GNU General Public License v3.0 | 6 votes |
/** * Password based encryption using AES - CBC 256 bits. */ @Override public EncryptedData 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 EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2)); } catch (Exception e) { throw new KeyCrypterException("Could not encrypt bytes.", e); } }
Example #6
Source File: Crypto.java From KeePassJava2 with Apache License 2.0 | 6 votes |
/** * Encryption and Decryption Helper * * @param input the candidate for transformation * @param base64in true if base 64 encoded * @param base64out true if we require base 64 out * @param cipher a Cipher initialised for Encrypt or Decrypt * @return the transformed result */ static String CryptoTransform(String input, boolean base64in, boolean base64out, PaddedBufferedBlockCipher cipher) { byte[] bytes; if (base64in) { bytes = Helpers.decodeBase64Content(input.getBytes(), false); } else { bytes = input.getBytes(); } byte[] output = new byte[cipher.getOutputSize(bytes.length)]; int outputlen = cipher.processBytes(bytes, 0, bytes.length, output, 0); try { int len = cipher.doFinal(output, outputlen); // padded buffer is required on bas64 i.e. encrypted direction if (base64out) { return Helpers.encodeBase64Content(output, false); } // trim to buffer length return new String(output, 0, outputlen + len); } catch (InvalidCipherTextException e) { throw new IllegalStateException(e); } }
Example #7
Source File: aes.java From bitshares_wallet with MIT License | 6 votes |
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) { try { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); byte[] clear = new byte[cipher.getOutputSize(cipertext.length)]; int len = cipher.processBytes(cipertext, 0, cipertext.length, clear,0); len += cipher.doFinal(clear, len); ByteBuffer byteBuffer = ByteBuffer.allocate(len); byteBuffer.put(clear, 0, len); return byteBuffer; } catch(Exception e) { e.printStackTrace(); } return null; }
Example #8
Source File: aes.java From bitshares_wallet with MIT License | 6 votes |
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) { assert (key.length == 32 && iv.length == 16); try { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); byte[] outBuf = new byte[cipher.getOutputSize(plaintext.length)]; int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0); processed += cipher.doFinal(outBuf, processed); ByteBuffer byteBuffer = ByteBuffer.allocate(processed); byteBuffer.put(outBuf, 0, processed); return byteBuffer; } catch(Exception e) { e.printStackTrace(); } return null; }
Example #9
Source File: AESEncrypt.java From nuls with MIT License | 6 votes |
public static byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws CryptoException { Util.checkNotNull(dataToDecrypt); Util.checkNotNull(aesKey); try { ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.getInitialisationVector()); // Decrypt the validator. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, keyWithIv); byte[] cipherBytes = dataToDecrypt.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 CryptoException(); } }
Example #10
Source File: AESEncrypt.java From nuls with MIT License | 6 votes |
public static EncryptedData encrypt(byte[] plainBytes, byte[] iv, KeyParameter aesKey) throws RuntimeException { Util.checkNotNull(plainBytes); Util.checkNotNull(aesKey); try { if (iv == null) { iv = EncryptedData.DEFAULT_IV; //SECURE_RANDOM.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 EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2)); } catch (Exception e) { throw new RuntimeException(e); } }
Example #11
Source File: KeyCrypterScrypt.java From green_android with GNU General Public License v3.0 | 6 votes |
/** * Decrypt bytes previously encrypted with this class. * * @param dataToDecrypt The data to decrypt * @param aesKey The AES key to use for decryption * @return The decrypted bytes * @throws KeyCrypterException if bytes could not be decrypted */ @Override public byte[] decrypt(EncryptedData dataToDecrypt, KeyParameter aesKey) throws KeyCrypterException { checkNotNull(dataToDecrypt); checkNotNull(aesKey); try { ParametersWithIV keyWithIv = new ParametersWithIV(new KeyParameter(aesKey.getKey()), dataToDecrypt.initialisationVector); // Decrypt the message. BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); 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); } }
Example #12
Source File: KeyCrypterScrypt.java From green_android with GNU General Public License v3.0 | 6 votes |
/** * Password based encryption using AES - CBC 256 bits. */ @Override public EncryptedData 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 EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2)); } catch (Exception e) { throw new KeyCrypterException("Could not encrypt bytes.", e); } }
Example #13
Source File: aes.java From guarda-android-wallets with GNU General Public License v3.0 | 6 votes |
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) { try { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); byte[] clear = new byte[cipher.getOutputSize(cipertext.length)]; int len = cipher.processBytes(cipertext, 0, cipertext.length, clear,0); len += cipher.doFinal(clear, len); ByteBuffer byteBuffer = ByteBuffer.allocate(len); byteBuffer.put(clear, 0, len); return byteBuffer; } catch(Exception e) { e.printStackTrace(); } return null; }
Example #14
Source File: aes.java From guarda-android-wallets with GNU General Public License v3.0 | 6 votes |
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) { assert (key.length == 32 && iv.length == 16); try { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); byte[] outBuf = new byte[cipher.getOutputSize(plaintext.length)]; int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0); processed += cipher.doFinal(outBuf, processed); ByteBuffer byteBuffer = ByteBuffer.allocate(processed); byteBuffer.put(outBuf, 0, processed); return byteBuffer; } catch(Exception e) { e.printStackTrace(); } return null; }
Example #15
Source File: aes.java From AndroidWallet with GNU General Public License v3.0 | 6 votes |
public static ByteBuffer decrypt(byte[] key, byte[] iv, byte[] cipertext) { try { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new ParametersWithIV(new KeyParameter(key), iv)); byte[] clear = new byte[cipher.getOutputSize(cipertext.length)]; int len = cipher.processBytes(cipertext, 0, cipertext.length, clear, 0); len += cipher.doFinal(clear, len); ByteBuffer byteBuffer = ByteBuffer.allocate(len); byteBuffer.put(clear, 0, len); return byteBuffer; } catch (Exception e) { e.printStackTrace(); } return null; }
Example #16
Source File: aes.java From AndroidWallet with GNU General Public License v3.0 | 6 votes |
public static ByteBuffer encrypt(byte[] key, byte[] iv, byte[] plaintext) { assert (key.length == 64 && iv.length == 32); try { PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(true, new ParametersWithIV(new KeyParameter(key), iv)); byte[] outBuf = new byte[cipher.getOutputSize(plaintext.length)]; int processed = cipher.processBytes(plaintext, 0, plaintext.length, outBuf, 0); processed += cipher.doFinal(outBuf, processed); ByteBuffer byteBuffer = ByteBuffer.allocate(processed); byteBuffer.put(outBuf, 0, processed); return byteBuffer; } catch (Exception e) { e.printStackTrace(); } return null; }
Example #17
Source File: KeyCrypterScrypt.java From bcm-android with GNU General Public License v3.0 | 6 votes |
/** * Password based encryption using AES - CBC 256 bits. */ @Override public EncryptedData 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 EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2)); } catch (Exception e) { throw new KeyCrypterException("Could not encrypt bytes.", e); } }
Example #18
Source File: IoUtil.java From OpenYOLO-Android with Apache License 2.0 | 5 votes |
static PaddedBufferedBlockCipher createAes128CtrPkcs7PaddingCipher( boolean encrypting, byte[] iv, byte[] key) { AESEngine aes = new AESEngine(); SICBlockCipher aesCtr = new SICBlockCipher(aes); PaddedBufferedBlockCipher aesCtrPkcs7 = new PaddedBufferedBlockCipher(aesCtr, new PKCS7Padding()); aesCtrPkcs7.init(encrypting, new ParametersWithIV(new KeyParameter(key), iv)); return aesCtrPkcs7; }
Example #19
Source File: Encryption.java From KeePassJava2 with Apache License 2.0 | 5 votes |
/** * Create a decrypted input stream from an encrypted one */ public static InputStream getDecryptedInputStream (InputStream encryptedInputStream, byte[] keyData, byte[] ivData) { final ParametersWithIV keyAndIV = new ParametersWithIV(new KeyParameter(keyData), ivData); PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); pbbc.init(false, keyAndIV); return new CipherInputStream(encryptedInputStream, pbbc); }
Example #20
Source File: Encryption.java From KeePassJava2 with Apache License 2.0 | 5 votes |
/** * Create an encrypted output stream from an unencrypted output stream */ public static OutputStream getEncryptedOutputStream (OutputStream decryptedOutputStream, byte[] keyData, byte[] ivData) { final ParametersWithIV keyAndIV = new ParametersWithIV(new KeyParameter(keyData), ivData); PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); pbbc.init(true, keyAndIV); return new CipherOutputStream(decryptedOutputStream, pbbc); }
Example #21
Source File: SensitiveDataPreApi23.java From android-java-connect-rest-sample with MIT License | 5 votes |
private byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data) throws InvalidCipherTextException { int minSize = cipher.getOutputSize(data.length); byte[] outBuf = new byte[minSize]; int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0); int length2 = cipher.doFinal(outBuf, length1); int actualLength = length1 + length2; byte[] result = new byte[actualLength]; System.arraycopy(outBuf, 0, result, 0, result.length); return result; }
Example #22
Source File: KeyCrypterScrypt.java From bitherj with Apache License 2.0 | 5 votes |
/** * 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 */ @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(); int minimumSize = cipher.getOutputSize(cipherBytes.length); byte[] outputBuffer = new byte[minimumSize]; int length1 = cipher.processBytes(cipherBytes, 0, cipherBytes.length, outputBuffer, 0); int length2 = cipher.doFinal(outputBuffer, length1); int actualLength = length1 + length2; byte[] decryptedBytes = new byte[actualLength]; System.arraycopy(outputBuffer, 0, decryptedBytes, 0, actualLength); Utils.wipeBytes(outputBuffer); return decryptedBytes; } catch (Exception e) { throw new KeyCrypterException("Could not decrypt bytes", e); } }
Example #23
Source File: Crypto.java From KeePassJava2 with Apache License 2.0 | 2 votes |
/** * Get a cipher * * @param mode encryption or decryption * @param iv a 16 byte iv * @return an initialised Cipher */ PaddedBufferedBlockCipher getCipher(CMode mode, byte[] iv) { PaddedBufferedBlockCipher result = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); result.init(mode.getEncrypt(), new ParametersWithIV(new KeyParameter(getKey()), iv)); return result; }