From: Geetu S. <gee...@gm...> - 2007-08-02 07:34:34
|
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 |