Cryptography Streams: Digest Decryptor, Java example Java code example - Click here to copy ->>>
Can't find what you're looking for? Try our search:
|
|
Really working examples categorized by API, package, class. You can compile and run our examples right away!
Not from source code for Java projects - only working examples! Copy, compile and run!
|
------------------
/* Title: Hardcore Java
* Title: Java I/O
* Second Edition: May 2006
* ISBN 10: 0-596-52750-0
* ISBN 13: 9780596527501
*/
| Code: |
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class DigestDecryptor {
public static void main(String[] args)
throws IOException, GeneralSecurityException {
if (args.length != 3) {
System.err.println("Usage: java DigestDecryptor infile outfile password");
return;
}
String infile = args[0];
String outfile = args[1];
String password = args[2];
if (password.length() < 8 ) {
System.err.println("Password must be at least eight characters long");
}
FileInputStream fin = new FileInputStream(infile);
FileOutputStream fout = new FileOutputStream(outfile);
// Get the digest.
FileInputStream digestIn = new FileInputStream(infile + ".digest");
DataInputStream dataIn = new DataInputStream(digestIn);
// SHA digests are always 20 bytes long .
byte[] oldDigest = new byte[20];
dataIn.readFully(oldDigest);
dataIn.close();
// Create a key.
byte[] desKeyData = password.getBytes();
DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey desKey = keyFactory.generateSecret(desKeySpec);
// Use Data Encryption Standard.
Cipher des = Cipher.getInstance("DES/ECB/PKCS5Padding");
des.init(Cipher.DECRYPT_MODE, desKey);
CipherOutputStream cout = new CipherOutputStream(fout, des);
// Use SHA digest algorithm.
MessageDigest sha = MessageDigest.getInstance("SHA");
DigestInputStream din = new DigestInputStream(fin, sha);
byte[] input = new byte[64];
while (true) {
int bytesRead = din.read(input);
if (bytesRead == -1) break;
cout.write(input, 0, bytesRead);
}
byte[] newDigest = sha.digest();
if (!MessageDigest.isEqual(newDigest, oldDigest)) {
System.out.println("Input file appears to be corrupt!");
}
din.close();
cout.flush();
cout.close();
}
}
|
|
|
References.
The list of classes which were used on this page you can find below. The
links to Java API contain official SUN documentation about all used classes.
[ Go Back ]
|