I am trying to encrypt a string and store encrypted bytes in a primitive byte array using a CipherOutputStream , which is supported by ByteArrayOutputStream , but the size of the ByteArrayOutputStream object remains equal to zero and it does not encode any bytes after something is written to the CipherOutputStream object. Here is the code.
ByteArrayOutputStream out = new ByteArrayOutputStream(); CipherOutputStream cos = new CipherOutputStream(out, c); PrintWriter pw = new PrintWriter(cos); pw.println("Write something"); cos.flush(); out.flush(); System.out.println(out.size()); pw.close();
So, I tried to make a comparison by changing ByteArrayOutputStream to FileOutputStream using the code below. It turned out that the encrypted bytes are written to the target file. Does anyone know why I cannot use ByteArrayOutputStream here? Can you offer a solution?
FileOutputStream out = new FileOutputStream("/path/encrypted.txt"); CipherOutputStream cos = new CipherOutputStream(out, c); PrintWriter pw = new PrintWriter(cos); pw.println("Write something"); pw.close();
source share