File Encryption and Decryption using PBE
Password-Based Encryption (PBE) derives an encryption key from a password. In order to
make the task of getting from password to key very time-consuming for an attacker, most PBE
implementations (as the one shown below) will mix in a random number, known as a salt, to
create the key.
PBE algorithm supported in j2se1.4 (Other algorithms are supported in later versions of the
SunJCE and other providers)
• PBEwithMD5andDES
[Link]
01: /**
02: * This demonstrates password-based encryption (PBE) using the
03: * DES algorithm. As mentioned before, DES is not very secure,
04: * but the SDK version used for this example does not support
05: * other (stronger) PBE algorithms
06: *
07: * The text file "[Link]" will be read, encrypted and written back
08: * as "[Link]".
09: *
10: * I've done all the "hardcoding" of the file names and password for
11: * simplicity of discussion.
12: *
13: */
14:
15: import [Link].*;
16: import [Link].*;
17: import [Link].*;
18: import [Link].*;
19: import [Link].*;
20:
21: public class FileEncryptor
22: {
23: private static String filename;
24: private static String password;
25: private static FileInputStream inFile;
26: private static FileOutputStream outFile;
27:
28: /**
29: * Note: As with the pervious example, all kinds of exceptions
30: * can be thrown in main. See the API documentation for each
31: * method used.
32: */
33:
34: public static void main(String[] args) throws Exception
35: {
36:
37: // File to encrypt. It does not have to be a text file!
38:
39: filename = "[Link]";
40:
41: // Password must be at least 8 characters (bytes) long
42:
43: String password = "super_secret";
44:
45: inFile = new FileInputStream(filename);
46: outFile = new FileOutputStream(filename + ".des");
47:
48: // Use PBEKeySpec to create a key based on a password.
49: // The password is passed as a character array
50:
51: PBEKeySpec keySpec = new PBEKeySpec([Link]());
52: SecretKeyFactory keyFactory =
53: [Link]("PBEWithMD5AndDES");
54: SecretKey passwordKey = [Link](keySpec);
55:
56: // PBE = hashing + symmetric encryption. A 64 bit random
57: // number (the salt) is added to the password and hashed
58: // using a Message Digest Algorithm (MD5 in this example.).
59: // The number of times the password is hashed is determined
60: // by the interation count. Adding a random number and
61: // hashing multiple times enlarges the key space.
62:
63: byte[] salt = new byte[8];
64: Random rnd = new Random();
65: [Link](salt);
66: int iterations = 100;
67:
68: //Create the parameter spec for this salt and interation count
69:
70: PBEParameterSpec parameterSpec = new PBEParameterSpec(salt,
iterations);
71:
72: // Create the cipher and initialize it for encryption.
73:
74: Cipher cipher = [Link]("PBEWithMD5AndDES");
75: [Link](Cipher.ENCRYPT_MODE, passwordKey, parameterSpec);
76:
77: // Need to write the salt to the (encrypted) file. The
78: // salt is needed when reconstructing the key for decryption.
79:
80: [Link](salt);
81:
82: // Read the file and encrypt its bytes.
83:
84: byte[] input = new byte[64];
85: int bytesRead;
86: while ((bytesRead = [Link](input)) != -1)
87: {
88: byte[] output = [Link](input, 0, bytesRead);
89: if (output != null) [Link](output);
90: }
91:
92: byte[] output = [Link]();
93: if (output != null) [Link](output);
94:
95: [Link]();
96: [Link]();
97: [Link]();
98:
99: }
100: }
[Link]
01: /**
02: * Simple modification of "[Link]". The
03: * previously encrypted "[Link]" is read, decrypted
04: * and written back as "[Link]"
05: */
06:
07: import [Link].*;
08: import [Link].*;
09: import [Link].*;
10: import [Link].*;
11: import [Link].*;
12:
13: public class FileDecryptor
14: {
15: private static String filename;
16: private static String password;
17: private static FileInputStream inFile;
18: private static FileOutputStream outFile;
19:
20: /**
21: * Note: All kinds of exceptions can be thrown in main.
22: * See the API documentation for each method used.
23: */
24:
25: public static void main(String[] args) throws Exception
26: {
27:
28: // File to decrypt.
29:
30: filename = "[Link]";
31:
32: String password = "super_secret_password";
33:
34: inFile = new FileInputStream(filename);
35: outFile = new FileOutputStream(filename + ".dcr");
36:
37: PBEKeySpec keySpec = new PBEKeySpec([Link]());
38: SecretKeyFactory keyFactory =
39: [Link]("PBEWithMD5AndDES");
40: SecretKey passwordKey = [Link](keySpec);
41:
42: // Read in the previouly stored salt and set the iteration count.
43:
44: byte[] salt = new byte[8];
45: [Link](salt);
46: int iterations = 100;
47:
48: PBEParameterSpec parameterSpec = new PBEParameterSpec(salt,
iterations);
49:
50: // Create the cipher and initialize it for decryption.
51:
52: Cipher cipher = [Link]("PBEWithMD5AndDES");
53: [Link](Cipher.DECRYPT_MODE, passwordKey, parameterSpec);
54:
55:
56: byte[] input = new byte[64];
57: int bytesRead;
58: while ((bytesRead = [Link](input)) != -1)
59: {
60: byte[] output = [Link](input, 0, bytesRead);
61: if (output != null)
62: [Link](output);
63: }
64:
65: byte[] output = [Link]();
66: if (output != null)
67: [Link](output);
68:
69: [Link]();
70: [Link]();
71: [Link]();
72: }
73: }