2 Replies Latest reply: Sep 11, 2010 1:46 PM by 843790 RSS

    Writing lines into temporary buffer

    843790
      I wonder, if its possible to write line (for example through BufferedWriter.write, BufferedWriter.newLine) into some temporary stream or something. Because if I create BufferedWriter with Writer constructor (for example FileWriter) it will write it directly to a file. But i want to do something with the data before their will be written - like encrypt them. I dont want to encrypt line by line but the whole buffer so I'll call encrypt method just once. I imagine something like writing lines into some temporary buffer, encrypt whole buffer, and then store those data to file. After that, I want to read the file into stream or something, decrypt those data into for example bufferedReader and read line by line.

      I think Im just complicating this. If you have some better idea, out with it :)

      The point is, that I need to write lines to file on disk - text lines. And then read them line by line. But I want to encrypt whole data with caling encrypt method once and I dont want to create some temporary file on disk.
        • 1. Re: Writing lines into temporary buffer
          EJP
          In general what you are talking about is accomplished by FilterOutputStreams. In this case see CipherOutputSreeam.
          • 2. Re: Writing lines into temporary buffer
            843790
            I like this way with CipherOutputStream, but theres small problem...it does nothing.
            Cryptography crypto = new Cryptography("some string which is key");
            
            CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("C:\\file.test"), crypto.encrypt());
            PrintWriter pw = new PrintWriter(cos);
            
            pw.println("hello world");
            pw.println("hello world");
            pw.flush();
            pw.close();
            object crypto is instance of Cryptography with constructor: (where cipher is instance of Cipher and key is instance of SecretKeySpec initialized with "AES")
            public Cryptography(String key) {
                    try {
                        md5 = MessageDigest.getInstance("MD5");
                        md5.update(key.getBytes("UTF-8"));
                        byte[] hash = md5.digest();
                        cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                        this.key = new SecretKeySpec(hash, "AES");
                    } catch (UnsupportedEncodingException ex) {
                        System.err.println("Unsupported encoding exc. in Cryptography.constructor(): " + ex.getMessage());
                    } catch (NoSuchAlgorithmException ex) {
                        System.err.println("No such algorithm exc. in Cryptography.constructor(): " + ex.getMessage());
                    } catch (NoSuchPaddingException ex) {
                        System.err.println("No such padding exc. in Cryptography.constructor(): " + ex.getMessage());
                    }
                }
            method encrypt:
            public Cipher encrypt() {
                    try {
                        cipher.init(Cipher.ENCRYPT_MODE, key);
                        return cipher;
                    } catch (InvalidKeyException ex) {
                        System.err.println("Invalid key exc. in Criptography.encrypt(): " + ex.getMessage());
                        return null;
                    }
                }
            As I said, after println into writer, and closing it, theres nothing in the file when the code runs.

            THIS IS VERY INTERESTING...I DONE NOTHING AND SUDDENLY IT WORKS...HAVE NO CLUE HOW THE...

            Edited by: Lukasito on Sep 11, 2010 11:45 AM