congrats Icon
New! Announcing Tabnine Chat Beta
Learn More
Tabnine Logo
CipherOutputStream
Code IndexAdd Tabnine to your IDE (free)

How to use
CipherOutputStream
in
javax.crypto

Best Java code snippets using javax.crypto.CipherOutputStream (Showing top 20 results out of 1,404)

Refine searchRefine arrow

  • Cipher
  • FileOutputStream
  • FileInputStream
  • ByteArrayOutputStream
  • OutputStream
origin: syncany/syncany

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();
}

origin: stackoverflow.com

 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();
}
origin: jenkinsci/jenkins

/**
 * 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);
  }
}
origin: stackoverflow.com

 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();
origin: stackoverflow.com

FileInputStream fis = new FileInputStream(new File("D:/Shashank/inputVideo.avi"));
   File outfile = new File("D:/Shashank/encVideo.avi");
   int read;
   encipher.init(Cipher.ENCRYPT_MODE, skey);
   CipherInputStream cis = new CipherInputStream(fis, encipher);
   decipher.init(Cipher.DECRYPT_MODE, skey);
   CipherOutputStream cos = new CipherOutputStream(decfos,decipher);
   while((read = cis.read())!=-1)
         fos.write((char)read);
         fos.flush();
   fos.close();
   while((read=encfis.read())!=-1)
     cos.write(read);
     cos.flush();
 cos.close();
origin: stackoverflow.com

 fileinputstrm=new FileInputStream(path);
BufferedInputStream input=new BufferedInputStream(fileinputstrm);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
PBEKeySpec pbeKeySpec = new PBEKeySpec("pass".toCharArray());          

PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 20);
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

FileOutputStream output = new FileOutputStream(path + ".icrpt");
CipherOutputStream cos = new CipherOutputStream(output, pbeCipher);

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
output.write(bytes.toByteArray());
cos.close();
origin: Quicksign/kafka-encryption

@Override
public byte[] encrypt(byte[] data, byte[] key) throws Exception {
  SecretKeySpec secretKeySpec = new SecretKeySpec(key, KEY_SPEC);
  byte[] iv = new byte[IV_SIZE];
  secureRandom.nextBytes(iv);
  GCMParameterSpec gcmParamSpec = new GCMParameterSpec(TAG_BIT_LENGTH, iv);
  Cipher cipher = Cipher.getInstance(ALGO_TRANSFORMATION_STRING);
  cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, gcmParamSpec, secureRandom);
  cipher.updateAAD(TAG.getBytes(StandardCharsets.UTF_8));
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  baos.write(iv);
  try (CipherOutputStream cipherOutputStream = new CipherOutputStream(baos, cipher)) {
    cipherOutputStream.write(data);
  }
  return baos.toByteArray();
}
origin: gradle.plugin.earth.cube.gradle.plugins/cube-gradleplugins-commons

private void encrypt(int cipherMode, String sKey, File inFile, File outFile) throws Exception {
  Key key = new SecretKeySpec(sKey.getBytes(), "AES");
  Cipher cipher = Cipher.getInstance("AES");
  cipher.init(cipherMode, key);
    byte[] buf = new byte[BUF_SIZE];
  try(InputStream in = new FileInputStream(inFile)) {
    try(OutputStream out = new CipherOutputStream(new FileOutputStream(outFile), cipher)) {
      int n = in.read(buf);
      out.write(buf,  0, n);
    }
  }
}
origin: structr/structr

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();
}
origin: opendedup/sdfs

public static void encryptFile(File src, File dst) throws Exception {
  if (!dst.getParentFile().exists())
    dst.getParentFile().mkdirs();
  Cipher encrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
  encrypt.init(Cipher.ENCRYPT_MODE, key, spec);
  // opening streams
  FileOutputStream fos = new FileOutputStream(dst);
  FileInputStream fis = new FileInputStream(src);
  CipherOutputStream cout = new CipherOutputStream(fos, encrypt);
  IOUtils.copy(fis, cout);
  cout.flush();
  cout.close();
  fis.close();
}
origin: stackoverflow.com

byte[] buffer = new byte[8192];
FileInputStream fis = new FileInputStream(source);
FileOutputStream fos = new FileOutputStream(target);
encoding.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(fos, encoding);
int numBytes;
while ((numBytes = fis.read(buffer)) != -1) {
  cos.write(buffer, 0, numBytes);
fos.flush();
fis.close();
fos.close();
cos.close();
return true;
origin: org.elasticsearch/elasticsearch

/** Encrypt the keystore entries and return the encrypted data. */
private byte[] encrypt(char[] password, byte[] salt, byte[] iv) throws GeneralSecurityException, IOException {
  assert isLoaded();
  ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, password, salt, iv);
  try (CipherOutputStream cipherStream = new CipherOutputStream(bytes, cipher);
     DataOutputStream output = new DataOutputStream(cipherStream)) {
    output.writeInt(entries.get().size());
    for (Map.Entry<String, Entry> mapEntry : entries.get().entrySet()) {
      output.writeUTF(mapEntry.getKey());
      Entry entry = mapEntry.getValue();
      output.writeUTF(entry.type.name());
      output.writeInt(entry.bytes.length);
      output.write(entry.bytes);
    }
  }
  return bytes.toByteArray();
}
origin: stackoverflow.com

cipher.init(Cipher.ENCRYPT_MODE, pubKey);
InputStream fis = new ByteArrayInputStream(test.getBytes("UTF-8"));
FileOutputStream fos = new FileOutputStream(ciphertextFile);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int i;
while ((i = fis.read(block)) != -1) {
  cos.write(block, 0, i);
cos.close();
cipher.init(Cipher.DECRYPT_MODE, privKey);
fis = new FileInputStream(ciphertextFile);
CipherInputStream cis = new CipherInputStream(fis, cipher);
fos = new FileOutputStream(cleartextAgainFile);
  fos.write(block, 0, i);
fos.close();
origin: stackoverflow.com

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();
   }
 }
origin: stackoverflow.com

inCipher.init(Cipher.ENCRYPT_MODE, publicKey);
outCipher.init(Cipher.DECRYPT_MODE, privateKey);
    new FileOutputStream(encryptedDataFilePath), inCipher);
cipherOutputStream.write(plainText.getBytes("UTF-8"));
cipherOutputStream.close();
  new CipherInputStream(new FileInputStream(encryptedDataFilePath),
    outCipher);
byte [] roundTrippedBytes = new byte[1000]; // TODO: dynamically resize as we get more data
origin: stackoverflow.com

 FileInputStream fin = new FileInputStream("test.txt");
FileOutputStream out = new FileOutputStream("encrypted.p4e");
CipherOutputStream cout = new CipherOutputStream(out, encrypt);
int input = 0;
while ((input = fin.read()) != -1) {
  cout.write(input);
}
out.close(); // remove this line and it works
cout.close();
origin: org.apache.poi/poi-ooxml

/**
 * Returns the output stream for writing the data.<p>
 * Make sure to close it, otherwise the last cipher block is not written completely.
 *
 * @return the outputstream
 * @throws IOException if the writing to the underlying file fails
 */
public OutputStream getOutputStream() throws IOException {
  Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING);
  return new CipherOutputStream(new FileOutputStream(tempFile), ciEnc);
}
origin: stackoverflow.com

 FileInputStream inputStream = new FileInputStream(inputFile);
FileOutputStream fileout = new FileOutputStream(outputFile);
CipherOutputStream out = new CipherOutputStream(fileout , cipher);

try {
  byte[] buffer = new byte[8192];
  int count;

  while ((count = inputStream.read(buffer)) > 0) {
    out.write(buffer, 0, count);
  }
} finally {
  out.close();
  inputStream.close();
}
origin: stackoverflow.com

 public static void main(String[] args) {
  encryptFile("exampleInput.txt", "exampleOutput.txt");
}

public static void encryptFile(String source, String sink) {
  FileInputStream fis = null;
  try {
    fis = new FileInputStream(source);
    CipherOutputStream cos = null;
    try {
      cos = new CipherOutputStream(new FileOutputStream(sink), getEncryptionCipher());
      IOUtils.copy(fis, cos);
    } finally {
      if (cos != null)
        cos.close();
    }
  } finally {
    if (fis != null)
      fis.close();
  }
}

private static Cipher getEncryptionCipher() {
  // Create AES cipher with whatever padding and other properties you want
  Cipher cipher = ... ;
  // Create AES secret key
  Key key = ... ;
  cipher.init(Cipher.ENCRYPT_MODE, key);
}
origin: jenkinsci/jenkins

public CombinedCipherOutputStream(OutputStream out, Cipher asym, String algorithm) throws IOException, GeneralSecurityException {
  super(out);
  // create a new symmetric cipher key used for this stream
  String keyAlgorithm = getKeyAlgorithm(algorithm);
  SecretKey symKey = KeyGenerator.getInstance(keyAlgorithm).generateKey();
  // place the symmetric key by encrypting it with asymmetric cipher
  out.write(asym.doFinal(symKey.getEncoded()));
  // the rest of the data will be encrypted by this symmetric cipher
  Cipher sym = Secret.getCipher(algorithm);
  sym.init(Cipher.ENCRYPT_MODE,symKey, keyAlgorithm.equals(algorithm) ? null : new IvParameterSpec(symKey.getEncoded()));
  super.out = new CipherOutputStream(out,sym);
}
javax.cryptoCipherOutputStream

Javadoc

This class wraps an output stream and a cipher so that write methods send the data through the cipher before writing them to the underlying output stream.

The cipher must be initialized for the requested operation before being used by a CipherOutputStream. For example, if a cipher initialized for encryption is used with a CipherOutputStream, the CipherOutputStream tries to encrypt the data writing it out.

Most used methods

  • <init>
    Creates a new CipherOutputStream instance for an OutputStream and a Cipher.
  • close
    Close this cipher output stream. On the underlying cipher doFinal will be invoked, and any buffered
  • write
    Writes the len bytes from buffer b starting at offset off to this cipher output stream.
  • flush
    Flushes this cipher output stream.

Popular in Java

  • Reactive rest calls using spring rest template
  • runOnUiThread (Activity)
  • addToBackStack (FragmentTransaction)
  • notifyDataSetChanged (ArrayAdapter)
  • ObjectMapper (com.fasterxml.jackson.databind)
    ObjectMapper provides functionality for reading and writing JSON, either to and from basic POJOs (Pl
  • Kernel (java.awt.image)
  • MessageFormat (java.text)
    Produces concatenated messages in language-neutral way. New code should probably use java.util.Forma
  • JarFile (java.util.jar)
    JarFile is used to read jar entries and their associated data from jar files.
  • Handler (java.util.logging)
    A Handler object accepts a logging request and exports the desired messages to a target, for example
  • Project (org.apache.tools.ant)
    Central representation of an Ant project. This class defines an Ant project with all of its targets,
  • Best plugins for Eclipse
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now