23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

}<br />

catch (NoSuchAlgorithmException e) {<br />

System.err.println(e);<br />

e.printStackTrace();<br />

}<br />

catch (NoSuchPaddingException e) {System.err.println(e);}<br />

catch (BadPaddingException e) {System.err.println(e);}<br />

catch (IllegalBlockSizeException e) {System.err.println(e);}<br />

catch (<strong>IO</strong>Exception e) {System.err.println(e);}<br />

<strong>Java</strong> I/O<br />

There are a lot of different exceptions that must be caught. Except for the usual <strong>IO</strong>Exception,<br />

these are all subclasses of java.security.GeneralSecurityException. You could save<br />

some space simply by catching that. For example:<br />

catch (GeneralSecurityException e) {<br />

System.err.println(e);<br />

e.printStackTrace();<br />

}<br />

One exception I'll note in particular (because it threw me more than once while writing this<br />

chapter): if you should see a NoSuchAlgorithmException, it probably means you haven't<br />

properly installed the JCE or other provider that supports your algorithm.<br />

java.security.NoSuchAlgorithmException: Algorithm DES not available<br />

java.security.NoSuchAlgorithmException: Algorithm DES not available<br />

at javax.crypto.JceSecurity.getImpl(Compiled Code)<br />

at<br />

javax.crypto.SecretKeyFactory.getInstance(SecretKeyFactory.java:105)<br />

at FileEncryptor.main(Compiled Code)<br />

Adding the JCE classes to your class path is enough to get the program to compile, but to<br />

actually run it, you'll need to add the following line to the java.security file in the<br />

jre/lib/security directory in your JDK folder:<br />

security.provider.2=com.sun.crypto.provider.SunJCE<br />

To be honest, this is far too complicated and error-prone. For one thing, every time you<br />

upgrade your JDK, this property will get overwritten and you'll have to do it all over again.<br />

Sun really needs a better installer for the JCE that handles the setting of the necessary<br />

properties.<br />

Decrypting a file is similar, as Example 10.7 shows. The name of the input and output files<br />

and the password are read from the command line. A DES key factory converts the password<br />

to a DES secret key. Then both input and output files are opened in file streams, and a data<br />

input stream is chained to the input file. The main reason for this is to read the initialization<br />

vector. First the integer size is read, then the actual bytes of the vector. The resulting array is<br />

used to construct an IvParameterSpec object that will be used along with the key to initialize<br />

the cipher. Once the cipher has been initialized, the data is copied from input to output much<br />

as before.<br />

219

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!