Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: joven.chiew@gmail.com Newsgroups: comp.lang.java.programmer Subject: Encryption problem with CipherInputStream/CipherOutputStream Date: Sun, 15 Jul 2012 23:59:38 -0700 (PDT) Organization: http://groups.google.com Lines: 106 Message-ID: NNTP-Posting-Host: 175.156.143.190 Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1342421978 20479 127.0.0.1 (16 Jul 2012 06:59:38 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 16 Jul 2012 06:59:38 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=175.156.143.190; posting-account=c3_QTgoAAACjRXfLngb0qTzo_SAfcwmh User-Agent: G2/1.0 Xref: csiph.com comp.lang.java.programmer:16037 Hi I have the following code I tested to encrypt/decrypt file and all is go= od. It works correctly for full file encryption and now I want to test part= ial encryption of 100000 bytes. I tried read the 100000 from a text file fu= ll of "qwerty" text and encrypt it in another file and I am able to decrypt= this file back to the 100000 bytes of "qwerty" output to a new test file. So it seems like the partial encryption is ok, all I need is implement Ciph= erInputStream to decrypt the first 100000 bytes write to a output file and = do a normal FileInputStream and write to the output write. Theoretically I = should have implemented the partial file encryption/decryption.=20 Now, when I open the output file, I saw unreadable character at EOL. What i= s wrong, how can I correct it?=20 EOF -> "qwertyqwertyqwertyqwertyqwertyqwerty=B1=84w'=E7=F8]uX\=9CaQ2=CBI=F5= =93=DC=B1=F5=FE=CB=DCTN=B3'" Cheers. public static void encryptFile(SecretKey key, File sourceFile, File secretFile) throws Exception { byte[] salt =3D new byte[8]; Random random =3D new Random(); random.nextBytes(salt); =20 final int BUFF_SIZE =3D 100000; final byte[] buffer =3D new byte[BUFF_SIZE]; =20 PBEParameterSpec spec =3D new PBEParameterSpec(salt, ITERATIONCOUNT); Cipher cipher =3D Cipher.getInstance(ALGORITHM2); cipher.init(Cipher.ENCRYPT_MODE, key, spec); =20 InputStream in =3D null; OutputStream out =3D null; in =3D new FileInputStream(sourceFile); out =3D new FileOutputStream(secretFile); CipherOutputStream cos =3D new CipherOutputStream(out, cipher); =20 try { out.write(salt); boolean pEncrypt =3D true; while (true) { synchronized (buffer) { int amountRead =3D in.read(buffer); if (amountRead =3D=3D -1) { break; } if (pEncrypt) { cos.write(buffer, 0, amountRead); pEncrypt =3D false; } else { out.write(buffer, 0, amountRead); } } } } finally { if (in !=3D null) { in.close(); } if (cos !=3D null) { cos.close(); out.close(); } } } =20 public static void decryptFile(SecretKey key, File secretFile, File decryptFile) throws Exception { byte[] salt =3D new byte[8]; =20 final int BUFF_SIZE =3D 100000; final byte[] buffer =3D new byte[BUFF_SIZE]; =20 InputStream bis =3D null; OutputStream out =3D null; bis =3D new FileInputStream(secretFile); out =3D new FileOutputStream(decryptFile); =20 bis.read(salt); =20 PBEParameterSpec spec =3D new PBEParameterSpec(salt, ITERATIONCOUNT); Cipher cipher =3D Cipher.getInstance(ALGORITHM2); cipher.init(Cipher.DECRYPT_MODE, key, spec); =20 CipherInputStream in =3D new CipherInputStream(bis, cipher); =20 int b =3D 1; Boolean pEncrypt =3D true; while (true) { synchronized (buffer) { if (pEncrypt) { b =3D in.read(buffer); pEncrypt =3D false; } else { b =3D bis.read(buffer); } if (b =3D=3D -1) { break; } out.write(buffer, 0, b); } } out.close(); bis.close(); in.close(); }