@Override public void write(byte[] buffer, int offset, int length) throws IOException { _cipherOut.write(buffer, offset, length); }
@Override public void write(int ch) throws IOException { _cipherOut.write(ch); }
@Override public void write(int b) throws IOException { writeHeaderIfNecessary(); encryptedStream.write(b); }
@Override public void write(byte[] b, int off, int len) throws IOException { writeHeaderIfNecessary(); encryptedStream.write(b, off, len); }
@Override public void write(byte[] b) throws IOException { writeHeaderIfNecessary(); encryptedStream.write(b); }
/** * Persists the payload of {@link ConfidentialKey} to the disk. */ @Override protected void store(ConfidentialKey key, byte[] payload) throws IOException { try { Cipher sym = Secret.getCipher("AES"); sym.init(Cipher.ENCRYPT_MODE, masterKey); try (OutputStream fos = Files.newOutputStream(getFileFor(key).toPath()); CipherOutputStream cos = new CipherOutputStream(fos, sym)) { cos.write(payload); cos.write(MAGIC); } } catch (GeneralSecurityException e) { throw new IOException("Failed to persist the key: "+key.getId(),e); } catch (InvalidPathException e) { throw new IOException(e); } }
SecretKeySpec skeySpec = new SecretKeySpec(y.getBytes(), "AES"); FileInputStream fis; FileOutputStream fos; CipherOutputStream cos; // File you are reading from fis = new FileInputStream("/tmp/a.txt"); // File output fos = new FileOutputStream("/tmp/b.txt"); // Here the file is encrypted. The cipher1 has to be created. // Key Length should be 128, 192 or 256 bit => i.e. 16 byte SecretKeySpec skeySpec = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES"); Cipher cipher1 = Cipher.getInstance("AES"); cipher1.init(Cipher.ENCRYPT_MODE, skeySpec); cos = new CipherOutputStream(fos, cipher1); // Here you read from the file in fis and write to cos. byte[] b = new byte[8]; int i = fis.read(b); while (i != -1) { cos.write(b, 0, i); i = fis.read(b); } cos.flush();
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(); }
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 CipherOutputStream( new FileOutputStream(encryptedDataFilePath), inCipher); cipherOutputStream.write(plainText.getBytes("UTF-8")); cipherOutputStream.close();
/** * Override of CipherOutputStream's write to count the number of bytes that have been encrypted. * This method now throws an exception if an attempt to write bytes beyond a maximum is made. */ @Override public void write(byte b[], int off, int len) throws IOException { count += len; if (count > maxOutputSize) { throw new IOException("Attempt to write " + count + " bytes was made. A maximum of " + maxOutputSize + " is allowed for an encryption stream."); } super.write(b, off, len); }
/** * Override of CipherOutputStream's write for a single byte to count it. This method now throws an * exception if an attempt to write bytes beyond a maximum is made. */ @Override public void write(int b) throws IOException { count++; if (count > maxOutputSize) { throw new IOException("Attempt to write " + count + " bytes was made. A maximum of " + maxOutputSize + " is allowed for an encryption stream."); } super.write(b); } }
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(); }
while((read=encfis.read())!=-1) cos.write(read); cos.flush();
@Override public void write(int ch) throws IOException { _cipherOut.write(ch); }
@Override public void write(byte[] buffer, int offset, int length) throws IOException { _cipherOut.write(buffer, offset, length); }
@Override /** * <p>Writes the specified byte to this output stream.</p> * * <p>This just proxies to the underlying CipherOutputStream.</p> * * @param b the data */ public void write(int b) throws IOException { cipherOutputStream.write(b); } }
public void write(byte []buffer, int offset, int length) throws IOException { _cipherOut.write(buffer, offset, length); }
@Override public void write(byte[] b) throws IOException { writeHeaderIfNecessary(); encryptedStream.write(b); }
public void write(int ch) throws IOException { _cipherOut.write(ch); }