1 /*
   2  * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 0000000
  27  * @summary FlushBug
  28  * @author Jan Luehe
  29  * @key randomness
  30  */
  31 import java.io.*;
  32 import java.security.*;
  33 import javax.crypto.*;
  34 import javax.crypto.spec.*;
  35 import com.sun.crypto.provider.SunJCE;
  36 
  37 public class FlushBug {
  38     public static void main(String[] args) throws Exception {
  39 
  40         Provider prov = new com.sun.crypto.provider.SunJCE();
  41         Security.addProvider(prov);
  42 
  43         SecureRandom sr = new SecureRandom();
  44 
  45         // Create new DES key.
  46         KeyGenerator kg = KeyGenerator.getInstance("DES");
  47         kg.init(sr);
  48         Key key = kg.generateKey();
  49 
  50         // Generate an IV.
  51         byte[] iv_bytes = new byte[8];
  52         sr.nextBytes(iv_bytes);
  53         IvParameterSpec iv = new IvParameterSpec(iv_bytes);
  54 
  55         // Create the consumer
  56         Cipher decrypter = Cipher.getInstance("DES/CFB8/NoPadding");
  57         decrypter.init(Cipher.DECRYPT_MODE, key, iv);
  58         PipedInputStream consumer = new PipedInputStream();
  59         InputStream in = new CipherInputStream(consumer, decrypter);
  60 
  61         // Create the producer
  62         Cipher encrypter = Cipher.getInstance("DES/CFB8/NoPadding");
  63         encrypter.init(Cipher.ENCRYPT_MODE, key, iv);
  64         PipedOutputStream producer = new PipedOutputStream();
  65         OutputStream out = new CipherOutputStream(producer, encrypter);
  66 
  67         producer.connect(consumer); // connect pipe
  68 
  69         byte[] plaintext = "abcdef".getBytes();
  70         for (int i = 0; i < plaintext.length; i++) {
  71             out.write(plaintext[i]);
  72             out.flush();
  73             int b = in.read();
  74             String original = new String(plaintext, i, 1);
  75             String result = new String(new byte[] { (byte)b });
  76             System.out.println("  " + original + " -> " + result);
  77         }
  78     }
  79 }