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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

if (password.length() < 8 ) {<br />

System.err.println("Password must be at least eight characters<br />

long");<br />

}<br />

try {<br />

FileInputStream fin = new FileInputStream(filename);<br />

FileOutputStream fout = new FileOutputStream(filename +".des");<br />

FileOutputStream digest = new FileOutputStream(filename +<br />

".des.digest");<br />

}<br />

}<br />

// Create the key.<br />

byte[] desKeyData = password.getBytes();<br />

DESKeySpec desKeySpec = new DESKeySpec(desKeyData);<br />

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");<br />

SecretKey desKey = keyFactory.generateSecret(desKeySpec);<br />

// Use Data Encryption Standard.<br />

Cipher des = Cipher.getInstance("DES/ECB/PKCS5Padding");<br />

des.init(Cipher.ENCRYPT_MODE, desKey);<br />

CipherInputStream cin = new CipherInputStream(fin, des);<br />

// Use SHA digest algorithm.<br />

MessageDigest sha = MessageDigest.getInstance("SHA");<br />

DigestInputStream din = new DigestInputStream(cin, sha);<br />

byte[] input = new byte[64];<br />

while (true) {<br />

int bytesRead = din.read(input);<br />

if (bytesRead == -1) break;<br />

fout.write(input, 0, bytesRead);<br />

}<br />

digest.write(sha.digest());<br />

digest.close();<br />

din.close();<br />

fout.flush();<br />

fout.close();<br />

}<br />

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

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

catch (NoSuchAlgorithmException e) {<br />

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

e.printStackTrace();<br />

}<br />

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

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

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

The file is read with a file input stream chained to a cipher input stream chained to a digest<br />

input stream. As the file is read, encrypted, and digested, it's written into an output file. After<br />

the file has been completely read, the digest is written into another file so it can later be<br />

compared with the first file. Because the cipher input stream appears before the digest input<br />

stream in the chain, the digest is of the ciphertext, not the plaintext. If you read the file with a<br />

file input stream chained to a digest input stream chained to a cipher input stream, you would<br />

digest the plaintext. In fact, you could even use a file input stream chained to a digest input<br />

stream chained to a cipher input stream chained to a second digest input stream to get digests<br />

of both the plain- and ciphertext, though I won't do that here.<br />

228

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

Saved successfully!

Ooh no, something went wrong!