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  */
  30 import java.io.*;
  31 import java.security.*;
  32 import javax.crypto.*;
  33 import javax.crypto.spec.*;
  34 import com.sun.crypto.provider.SunJCE;
  35 
  36 public class FlushBug {
  37     public static void main(String[] args) throws Exception {
  38 
  39         Provider prov = new com.sun.crypto.provider.SunJCE();
  40         Security.addProvider(prov);
  41 
  42         SecureRandom sr = new SecureRandom();
  43 
  44         // Create new DES key.
  45         KeyGenerator kg = KeyGenerator.getInstance("DES");
  46         kg.init(sr);
  47         Key key = kg.generateKey();
  48 
  49         // Generate an IV.
  50         byte[] iv_bytes = new byte[8];
  51         sr.nextBytes(iv_bytes);
  52         IvParameterSpec iv = new IvParameterSpec(iv_bytes);
  53 
  54         // Create the consumer
  55         Cipher decrypter = Cipher.getInstance("DES/CFB8/NoPadding");
  56         decrypter.init(Cipher.DECRYPT_MODE, key, iv);
  57         PipedInputStream consumer = new PipedInputStream();
  58         InputStream in = new CipherInputStream(consumer, decrypter);
  59 
  60         // Create the producer
  61         Cipher encrypter = Cipher.getInstance("DES/CFB8/NoPadding");
  62         encrypter.init(Cipher.ENCRYPT_MODE, key, iv);
  63         PipedOutputStream producer = new PipedOutputStream();
  64         OutputStream out = new CipherOutputStream(producer, encrypter);
  65 
  66         producer.connect(consumer); // connect pipe
  67 
  68         byte[] plaintext = "abcdef".getBytes();
  69         for (int i = 0; i < plaintext.length; i++) {
  70             out.write(plaintext[i]);
  71             out.flush();
  72             int b = in.read();
  73             String original = new String(plaintext, i, 1);
  74             String result = new String(new byte[] { (byte)b });
  75             System.out.println("  " + original + " -> " + result);
  76         }
  77     }
  78 }