# Bug in CipherInputStream.read()

Edward A. Ipser, Jr. (ipser@ipser.com)
Mon, 25 Aug 1997 23:32:30 -0700

- Messages sorted by: [ date ][ thread ][ subject ][ author ]
- Next message: Henrique A.F.S.Faria: "how to buy the JCE"
- Previous message: development: "about DAS"

    From: "Edward A. Ipser, Jr." <ipser@ipser.com>

    To: "'java-security@java.sun.com'" <java-security@web2.javasoft.com>

    Subject: Bug in CipherInputStream.read()

    Date: Mon, 25 Aug 1997 23:32:30 -0700

------ =_NextPart_000_01BCB2BE.E83689F0
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

A bug in CipherInputStream.read() and a bug in the documentation. I'm =
using jce1_1-ea.exe.

I've been unable to get it to work consistently on big files. In some =
cases it hangs. In other cases it loses input data.

I lose data (its dropped) with:

while ((length =3D cis.read(in)) >=3D 0)
fos.write(in,0,length) ;

This simple loop also looses data (substituting 0s):

while ((c =3D cis.read()) >=3D 0)
fos.write(c) ;

The encrypting loop was:

while ((length =3D fis.read(in,0,in.length)) >=3D 0)
cos.write(in,0,length) ;

Also, the test program doesn't work in Symantec Visual Cafe 1.1 PR2 =
debugger though that is more probably their fault than yours.

Finally, the documentation suggests CipherInputStream.read(byte,int,int) =
has a different behavior from FileInputStream.read(byte,int,int). =
Surly, the statement that the return value is "the number of bytes read =
into b, or -1 if fewer than len encrypted/decrypted bytes remained" must =
be a typo? It implies that -1 will be returned instead of the correct =
length when the last chunk of data is read as opposed to the behavior =
with FileInputStream which returns -1 only after the last chunk of data =
has been read and there is no more data to read.

The complete test program is attached.

=20

Thanks in adance for your attention to these problems.
=00=00
------ =_NextPart_000_01BCB2BE.E83689F0
Content-Type: application/octet-stream; name="TestEncryption.java"
Content-Transfer-Encoding: base64

Ly8NCi8vIFRlc3QgRW5jcnlwdGlvbg0KLy8gQ29weXJpZ2h0IChjKSAxOTk3IElwc2VyIFNvZnR3
YXJlIENvbXBhbnkNCi8vIEFsbCByaWdodHMgcmVzZXJ2ZWQNCi8v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