From: Stefania V. <ste...@en...> - 2007-10-04 12:29:38
|
=20 Hello, I have written a java class using the cryptix libraries for the = signature/encryption and decryption/verification of files. I used these classes for the streams and everything worked properly as = long as the files were not to large. =20 However, when I tried to decrypt a 400MB file it took about 30 minutes. I used the example of the "StreamDecryptVerify" libraries to implement = my class but I would like to know if something is out of place or if it = is possible to improve the efficiency of the class. My code: java.security.Security.addProvider( new = cryptix.jce.provider.CryptixCrypto() ); java.security.Security.addProvider( new = cryptix.openpgp.provider.CryptixOpenPGP() ); = //********************************************************************** // First read the keys. = //********************************************************************** try { FileInputStream in; Collection msgs; KeyBundleMessage kbm; MessageFactory mf =3D MessageFactory.getInstance("OpenPGP"); in =3D new FileInputStream("privatekey.pgp"); msgs =3D mf.generateMessages(in); kbm =3D (KeyBundleMessage)msgs.iterator().next(); secretBob =3D (PGPKeyBundle)kbm.getKeyBundle(); in.close(); in =3D new FileInputStream("pubkey.pgp"); msgs =3D mf.generateMessages(in); kbm =3D (KeyBundleMessage)msgs.iterator().next(); publicAlice =3D (PGPKeyBundle)kbm.getKeyBundle(); in.close(); } catch (IOException ioe) { System.err.println("IOException... You did remember to run = the "+ "GenerateAndWriteKey example first, right?"); ioe.printStackTrace(); System.exit(-1); } catch (NoSuchAlgorithmException nsae) { System.err.println("Cannot find the OpenPGP MessageFactory. = "+ "This usually means that the Cryptix OpenPGP provider is = not "+ "installed correctly."); nsae.printStackTrace(); System.exit(-1); } catch (MessageException me) { System.err.println("Reading keybundle failed."); me.printStackTrace(); System.exit(-1); } = //********************************************************************** // The actual stream decryption and verification. = //********************************************************************** try { FileInputStream fileInputStream =3D new FileInputStream(fileToDecrypt); FileOutputStream fileOutputStream =3D new FileOutputStream(fileDecrypted); DecodedMessageInputStream decodedInputStream =3D DecodedMessageInputStream.getInstance("OpenPGP"); decodedInputStream.init(fileInputStream, this, this); System.out.println("Decoding message..."); long inizio =3D System.currentTimeMillis(); System.out.println("inizio=3D"+inizio); byte[] buf =3D new byte[4096]; int len =3D decodedInputStream.read(buf); while (len > 0) { fileOutputStream.write(buf, 0, len); len =3D decodedInputStream.read(buf); } =20 long fine =3D System.currentTimeMillis(); System.out.println("fine=3D"+fine); System.out.println("Tempo impiegato =3D = "+(fine-inizio)+"millisecondi"); System.out.println("Tempo impiegato =3D = "+((fine-inizio)/1000)+" secondi --> ("+(((fine-inizio)/1000)/60)+" = minuti)"); decodedInputStream.close(); fileInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); System.out.println("Decoding done."); switch(decodedInputStream.getVerificationResult()) { case DecodedMessageInputStream.VERIFICATION_NOT_SIGNED: System.out.println("Message was not signed."); break; case = DecodedMessageInputStream.VERIFICATION_GOOD_SIGNATURE: System.out.println("Message has a GOOD signature."); break; case = DecodedMessageInputStream.VERIFICATION_BAD_SIGNATURE: System.out.println("Message has a BAD signature."); break; default: System.out.println("Unknown verification result."); break; } switch(decodedInputStream.getIntegrityResult()) { case DecodedMessageInputStream.INTEGRITY_NOT_PROTECTED: System.out.println("Message was not integrity = protected."); break; case DecodedMessageInputStream.INTEGRITY_GOOD: System.out.println("Message integrity preserved."); break; case DecodedMessageInputStream.INTEGRITY_VIOLATED: System.out.println("Message integrity VIOLATED."); break; default: System.out.println("Unknown intregrity result."); break; } } catch (NoSuchAlgorithmException nsae) { System.err.println("Cannot find OpenPGP implementation."+ " This usually means that the Cryptix OpenPGP provider = is not "+ "installed correctly."); nsae.printStackTrace(); System.exit(-1); } catch (MessageStreamException me) { System.err.println("Streaming the message failed."); me.printStackTrace(); System.exit(-1); } catch (IOException ioe) { System.err.println("IO error."); ioe.printStackTrace(); System.exit(-1); } Thanks again, Stefania |