@Override public void close() throws IOException { encryptedStream.close();
@Override public void close() throws IOException { Hessian2Output out = _out; _out = null; if (out != null) { _cipherOut.close(); _bodyOut.close(); out.writeInt(0); out.completeEnvelope(); out.close();
static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { // Here you read the cleartext. FileInputStream fis = new FileInputStream("data/cleartext"); // This stream write the encrypted text. This stream will be wrapped by another stream. FileOutputStream fos = new FileOutputStream("data/encrypted"); // Length is 16 byte // Careful when taking user input!!! http://stackoverflow.com/a/3452620/1188357 SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); // Create cipher Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, sks); // Wrap the output stream CipherOutputStream cos = new CipherOutputStream(fos, cipher); // Write bytes int b; byte[] d = new byte[8]; while((b = fis.read(d)) != -1) { cos.write(d, 0, b); // Flush and close streams. cos.flush(); cos.close(); fis.close();
cipherOutputStream.close();
cipherOutputStream = null;
private static byte[] encryptWithAesGcm(byte[] plaintext, byte[] randomKeyBytes, byte[] randomIvBytes) throws IOException, InvalidKeyException,
InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException {
SecretKey randomKey = new SecretKeySpec(randomKeyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, randomKey, new IvParameterSpec(randomIvBytes));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(byteArrayOutputStream, cipher);
cipherOutputStream.write(plaintext);
cipherOutputStream.close();
return byteArrayOutputStream.toByteArray();
new FileOutputStream(encryptedDataFilePath), inCipher);
cipherOutputStream.write(plainText.getBytes("UTF-8"));
cipherOutputStream.close();
CipherOutputStream cos = new CipherOutputStream(fos2, ciEnc);
IOUtils.copy(zis, cos);
cos.close();
fos2.close();
zos.closeArchiveEntry();
outputStream.close();
if ( cipherOutputStream != null ) {
cipherOutputStream.close();
FileOutputStream fos = null; CipherOutputStream cos = null; FileInputStream fis = null; try { fis = new FileInputStream(inputFile); fos = new FileOutputStream(outputFile); cos = new CipherOutputStream(fos, cipher); // once cos wraps the fos, you should set it to null fos = null; } finally { if (cos != null) { cos.close(); if (fos != null) { fos.close(); if (fis != null) { fis.close();
private
static byte[] encryptBlocks(final byte[] data, final Cipher cipher, final int dataSize) throws IOException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length);
final int count = (data.length / dataSize) + 1;
int remaining = data.length;
for (int i=0; i<count; i++) {
final int offset = i*dataSize;
final int length = Math.min(dataSize, remaining);
final CipherOutputStream cos = new CipherOutputStream(bos, cipher);
cos.write(data, offset, length);
cos.flush();
cos.close();
remaining -= length;
return bos.toByteArray();
cos.flush();
cos.close();
@SuppressWarnings("PMD.UnusedPrivateMethod") // false positive private static <T> void saveEncryptedJsonToFile(String fileName, T object, Type typeOfObject) throws Exception { SecretKeySpec sks = new SecretKeySpec(StringHelper.hexStringToByteArray(Config.ENCRYPTION_KEY), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters()); try (OutputStream os = Files.newOutputStream(Paths.get(fileName))) { CipherOutputStream out = new CipherOutputStream(os, cipher); JsonWriter writer = new JsonWriter(new OutputStreamWriter(out)); getSerializer().toJson(object, typeOfObject, writer); writer.close(); out.close();
public void close() throws IOException { if (!closed) { super.close(); closed = true;
public void close() throws IOException { if (!cosClosed) { super.close(); cosClosed = true;
public void close() throws IOException { if (!closed) { super.close(); closed = true;
public void close() throws IOException { if (!cosClosed) { super.close(); cosClosed = true;
public void close() throws IOException { if (!closed) { super.close(); closed = true;
public void close() throws IOException { if (!closed) { super.close(); closed = true;
public void close() throws IOException { if (!closed) { super.close(); closed = true;
@Override public void close() throws IOException { encryptedStream.close();