From: Matt F. <mf...@fi...> - 2007-08-02 07:44:51
|
Haven't read the whole lot of the code but it looks like the problem lies here... BufferedReader in = new BufferedReader(new FileReader(fromFileName)); Surely this will not work on a binary file? A buffered reader will corrupt binary data as it gets converted into text... This isn't a cryptix issue but a plain old Java issue. You can prove it by reading the file in and writing it out again without encrypting / decrypting and check if it's the same issue... Also FileReader is for character files not binary ones... Use FileInputStream eg... FileInputStream in = new FileInputStream(new File(fromFileName)); Should now work for binary and textual files. Geetu Sandhu wrote: > Hi Friends, > I am posting for the first time, so I hope I am adhering to etiqette > of the forum. > > I am facing a problem while encrypting/ decrypting of zip and pdf files. > Is it possible to use cryptix on binary files? > > The exact problem is: > On decrypting, the zip gives me a CRC error on extracting and all > extracted files have zero size. > On decrypting, the pdf files have a size matching the original raw pdf > but it is all blank inside. > > I have tried quite a few ways of using cryptix but nothing worked (it > works on plain txt files). > Nothing on net says anything on the matter. Please help. > > > I am using stream encryption/ decryption. > My code is > > > public void encrypt(String fromFileName, String toFileName) throws > IOException, CipherException { > > try { > FileOutputStream fileos = new FileOutputStream(toFileName); > LiteralMessageOutputStream litmos = > LiteralMessageOutputStream.getInstance("OpenPGP"); > EncryptedMessageOutputStream encmos = > EncryptedMessageOutputStream.getInstance("OpenPGP"); > SecureRandom sr = new SecureRandom(); > > litmos.init(encmos, sr); // Literal writes to Encrypted > encmos.init(fileos, sr); // Encrypted writes to file > > encmos.addRecipient (publicKeyBundle); > > BufferedReader in = new BufferedReader(new > FileReader(fromFileName)); > > int ch; > while ((ch = in.read()) != -1) { > > litmos.write(ch); > } > in.close(); > > litmos.close(); > > } catch (NoSuchAlgorithmException nsae) { > throw new CipherException(noSuchAlgorithmMessage); > } catch (MessageStreamException me) { > throw new CipherException("Streaming the message failed."); > } catch (IOException ioe) { > throw ioe; > } > } > > public void decrypt(String fromFileName, String toFileName) throws > IOException, CipherException { > > FileInputStream fileInputStream=null; > FileOutputStream fileOutputStream=null; > DecodedMessageInputStream decodedInputStream=null; > try { > fileInputStream = new FileInputStream(fromFileName); > fileOutputStream = new FileOutputStream(toFileName); > decodedInputStream = > DecodedMessageInputStream.getInstance("OpenPGP"); > decodedInputStream.init(fileInputStream, this, this); > > byte[] buf = new byte[4096]; > int len = decodedInputStream.read(buf); > > while (len > 0) { > fileOutputStream.write(buf, 0, len); > len = decodedInputStream.read(buf); > } > > decodedInputStream.close(); > fileInputStream.close(); > fileOutputStream.close(); > > switch(decodedInputStream.getVerificationResult ()) { > case > DecodedMessageInputStream.VERIFICATION_GOOD_SIGNATURE: > case DecodedMessageInputStream.VERIFICATION_NOT_SIGNED: > break; > case > DecodedMessageInputStream.VERIFICATION_BAD_SIGNATURE: > throw new CipherException("Message has a BAD > signature."); > default: > throw new CipherException("Unknown verification > result"); > } > > switch(decodedInputStream.getIntegrityResult()) { > case DecodedMessageInputStream.INTEGRITY_NOT_PROTECTED: > case DecodedMessageInputStream.INTEGRITY_GOOD: > break; > case DecodedMessageInputStream.INTEGRITY_VIOLATED: > throw new CipherException("Message integrity > VIOLATED."); > default: > throw new CipherException("Unknown intregrity > result."); > } > > } catch (NoSuchAlgorithmException nsae) { > > throw new CipherException(noSuchAlgorithmMessage); > > } catch (MessageStreamException me) { > > // throw new CipherException("Decrypting through streaming > the message failed."); > > } catch (IOException ioe) { > > throw ioe; > } finally { > try { > fileInputStream.close(); > fileOutputStream.close(); > decodedInputStream.close(); > } catch (Exception e){ > > } > } > } > -- > regards, > Geetu > ------------------------------------------------------------------------ > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Splunk Inc. > Still grepping through log files to find problems? Stop. > Now Search log events and configuration files using AJAX and a browser. > Download your FREE copy of Splunk now >> http://get.splunk.com/ > ------------------------------------------------------------------------ > > _______________________________________________ > Cryptix-Users mailing list > Cry...@li... > https://lists.sourceforge.net/lists/listinfo/cryptix-users > ___________________________________________________________ First Option's outgoing email policy is at http://www.firstoption.net/emailpolicy.html, but a short summary is :- - all email/attachments are confidential; do not use, circulate or release without our consent - email is not authorised unless it is on First Option business - email is not binding unless it is from an authorised person and is signed with a digital certificate First Option Ltd. - Switchboard +44 (0) 1962 738200 Signal House, Jacklyns Lane, Alresford, Hants, SO24 9JJ, United Kingdom Registered Company No: 2453594 ___________________________________________________________ |