View Javadoc

1   /********************************************************************************
2    *InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package base.jdbs.cryptography.symmetric;
21  
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.security.spec.AlgorithmParameterSpec;
26  import java.security.spec.KeySpec;
27  
28  import javax.crypto.Cipher;
29  import javax.crypto.CipherInputStream;
30  import javax.crypto.SecretKey;
31  import javax.crypto.SecretKeyFactory;
32  import javax.crypto.spec.PBEKeySpec;
33  import javax.crypto.spec.PBEParameterSpec;
34  
35  import org.apache.log4j.Logger;
36  
37  public class DESSymmetricCipher extends SymmetricCipher{
38  	
39  	private static final transient Logger logger = Logger.getLogger(DESSymmetricCipher.class.getName());
40  
41  	/***The 8-byte salt to use to instantiate ciphers.**/
42  	byte[] salt = {
43  			(byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
44  			(byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
45  	};
46  	
47  	/***The number of iteration to use in DES**/
48  	int iterationCount = 19;
49  	
50  	public DESSymmetricCipher(SymmetricKey passPhrase){
51  		try {
52  			// Create the key
53  			KeySpec keySpec = new PBEKeySpec(passPhrase.getValue().toCharArray(), salt, iterationCount);
54  			SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
55  			encryptionCipher = Cipher.getInstance(key.getAlgorithm());
56  			decryptionCipher = Cipher.getInstance(key.getAlgorithm());
57  			
58  			// Prepare the parameter to the ciphers
59  			AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
60  			
61  			// Create the ciphers
62  			encryptionCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
63  			decryptionCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
64  		} catch (Exception ex) {
65  			logger.error(ex.getMessage());
66  			ex.printStackTrace();
67  		}
68  	}
69  	
70  	public void encrypt(File inputFile, File outputFile){
71  		try {
72  			// Bytes read from in will be decrypted
73  			CipherInputStream inputStream = new CipherInputStream(new FileInputStream(inputFile), encryptionCipher);
74  			FileOutputStream outputStream = new FileOutputStream(outputFile);
75  			byte[] buffer = new byte[1024];
76  			// Read in the decrypted bytes and write the cleartext to out
77  			int numRead = 0;
78  			while ((numRead = inputStream.read(buffer)) >= 0) {
79  				outputStream.write(buffer, 0, numRead);
80  			}
81  			inputStream.close();
82  			outputStream.close();
83  		} catch (java.io.IOException ex) {
84  			logger.error(ex.getMessage());
85  			ex.printStackTrace();
86  		}
87  	}
88  	
89  	public void decrypt(File inputFile, File outputFile){
90  		try {
91  			// Bytes read from in will be decrypted
92  			CipherInputStream inputStream = new CipherInputStream(new FileInputStream(inputFile), decryptionCipher);
93  			FileOutputStream outputStream = new FileOutputStream(outputFile);
94  			byte[] buffer = new byte[1024];
95  			// Read in the decrypted bytes and write the cleartext to out
96  			int numRead = 0;
97  			while ((numRead = inputStream.read(buffer)) >= 0) {
98  				outputStream.write(buffer, 0, numRead);
99  			}
100 			inputStream.close();
101 			outputStream.close();
102 		} catch (java.io.IOException ex) {
103 			logger.error(ex.getMessage());
104 			ex.printStackTrace();
105 		}
106 	}
107 }