Java Code Examples for org.bouncycastle.crypto.modes.GCMBlockCipher#init()
The following examples show how to use
org.bouncycastle.crypto.modes.GCMBlockCipher#init() .
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: GCMDataB.java From InflatableDonkey with MIT License | 7 votes |
public static byte[] decrypt(byte[] key, byte[] data) { // TODO utilize GCMAES#decrypt method try { if (data.length < NONCE_LENGTH + TAG_LENGTH) { throw new IllegalArgumentException("data packet too short"); } int cipherTextLength = data.length - NONCE_LENGTH - TAG_LENGTH; byte[] nonce = Arrays.copyOf(data, NONCE_LENGTH); GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(key), TAG_LENGTH * 8, nonce); cipher.init(false, parameters); byte[] out = new byte[cipher.getOutputSize(cipherTextLength + TAG_LENGTH)]; int pos = cipher.processBytes(data, NONCE_LENGTH, data.length - NONCE_LENGTH, out, 0); pos += cipher.doFinal(out, pos); return Arrays.copyOf(out, pos); } catch (IllegalStateException | InvalidCipherTextException ex) { throw new IllegalArgumentException(ex); } }
Example 2
Source File: AESGCMBytesEncryptor.java From flair-engine with Apache License 2.0 | 6 votes |
/** * Decrypt the byte array. * * @param encryptedByteArray cipher text */ @Override public byte[] decrypt(byte[] encryptedByteArray) { if (encryptedByteArray.length <= this.ivGenerator.getKeyLength()) { throw new IllegalCipherTextSizeException(); } byte[] iv = subArray(encryptedByteArray, 0, this.ivGenerator.getKeyLength()); encryptedByteArray = subArray(encryptedByteArray, this.ivGenerator.getKeyLength(), encryptedByteArray.length); GCMBlockCipher blockCipher = new GCMBlockCipher(new AESEngine()); blockCipher.init(false, new AEADParameters(secretKey, 128, iv, null)); return process(blockCipher, encryptedByteArray); }
Example 3
Source File: AESGCMBytesEncryptor.java From flair-engine with Apache License 2.0 | 5 votes |
/** * Encrypt the byte array. * * @param byteArray plain text */ @Override public byte[] encrypt(byte[] byteArray) { byte[] iv = this.ivGenerator.generateKey(); GCMBlockCipher blockCipher = new GCMBlockCipher(new AESEngine()); blockCipher.init(true, new AEADParameters(secretKey, 128, iv, null)); byte[] encrypted = process(blockCipher, byteArray); return iv != null ? concatenate(iv, encrypted) : encrypted; }
Example 4
Source File: AESGCM.java From InflatableDonkey with MIT License | 5 votes |
/** * Returns decrypted data. * * @param key * @param nonce nonce/ IV * @param header * @param encryptedData * @param tag * @param optional optional AADBytes (post header) * @return decrypted data * @throws IllegalArgumentException on decryption exceptions * @throws NullPointerException on null arguments */ public static byte[] decrypt( byte[] key, byte[] nonce, byte[] header, byte[] encryptedData, byte[] tag, Optional<byte[]> optional) { try { GCMBlockCipher cipher = new GCMBlockCipher(new AESFastEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(key), tag.length * 8, nonce, header); cipher.init(false, parameters); if (optional.isPresent()) { byte[] aadBytes = optional.get(); cipher.processAADBytes(aadBytes, 0, aadBytes.length); } byte[] out = new byte[cipher.getOutputSize(encryptedData.length + tag.length)]; int pos = cipher.processBytes(encryptedData, 0, encryptedData.length, out, 0); pos += cipher.processBytes(tag, 0, tag.length, out, pos); pos += cipher.doFinal(out, pos); return Arrays.copyOf(out, pos); } catch (IllegalStateException | InvalidCipherTextException | RuntimeCryptoException ex) { throw new IllegalStateException("GCM decrypt error", ex); } }
Example 5
Source File: StreamCryptor.java From InflatableDonkey with MIT License | 5 votes |
public CipherOutputStream newCipherOutputStream(OutputStream os, byte[] password) throws IOException { byte[] salt = randomBytes(saltLength); byte[] nonce = randomBytes(nonceLength); os.write(salt); os.write(nonce); byte[] dk = kdf.apply(password, salt); GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce); cipher.init(true, parameters); return new CipherOutputStream(os, cipher); }
Example 6
Source File: StreamCryptor.java From InflatableDonkey with MIT License | 5 votes |
public CipherInputStream newCipherInputStream(InputStream is, byte[] password) throws IOException { byte[] salt = IOUtils.readFully(is, saltLength); byte[] nonce = IOUtils.readFully(is, nonceLength); byte[] dk = kdf.apply(password, salt); GCMBlockCipher cipher = new GCMBlockCipher(new AESEngine()); AEADParameters parameters = new AEADParameters(new KeyParameter(dk), tagLength * 8, nonce); cipher.init(false, parameters); return new CipherInputStream(is, cipher); }