Coverage details for base.jdbs.cryptography.symmetric.DESSymmetricCipher

LineHitsSource
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  
372public class DESSymmetricCipher extends SymmetricCipher{
38     
392    private static final transient Logger logger = Logger.getLogger(DESSymmetricCipher.class.getName());
40  
41     /**The 8-byte salt to use to instantiate ciphers.**/
426    byte[] salt = {
433            (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
443            (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03
45     };
46     
47     /**The number of iteration to use in DES**/
483    int iterationCount = 19;
49     
503    public DESSymmetricCipher(SymmetricKey passPhrase){
51         try {
52             // Create the key
533            KeySpec keySpec = new PBEKeySpec(passPhrase.getValue().toCharArray(), salt, iterationCount);
543            SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
553            encryptionCipher = Cipher.getInstance(key.getAlgorithm());
563            decryptionCipher = Cipher.getInstance(key.getAlgorithm());
57             
58             // Prepare the parameter to the ciphers
593            AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
60             
61             // Create the ciphers
623            encryptionCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
633            decryptionCipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
640        } catch (Exception ex) {
650            logger.error(ex.getMessage());
660            ex.printStackTrace();
670        }
683    }
69     
70     public void encrypt(File inputFile, File outputFile){
71         try {
72             // Bytes read from in will be decrypted
731            CipherInputStream inputStream = new CipherInputStream(new FileInputStream(inputFile), encryptionCipher);
741            FileOutputStream outputStream = new FileOutputStream(outputFile);
751            byte[] buffer = new byte[1024];
760            // Read in the decrypted bytes and write the cleartext to out
771            int numRead = 0;
7837            while ((numRead = inputStream.read(buffer)) >= 0) {
7935                outputStream.write(buffer, 0, numRead);
800            }
811            inputStream.close();
821            outputStream.close();
830        } catch (java.io.IOException ex) {
840            logger.error(ex.getMessage());
850            ex.printStackTrace();
860        }
871    }
88     
89     public void decrypt(File inputFile, File outputFile){
90         try {
91             // Bytes read from in will be decrypted
921            CipherInputStream inputStream = new CipherInputStream(new FileInputStream(inputFile), decryptionCipher);
931            FileOutputStream outputStream = new FileOutputStream(outputFile);
941            byte[] buffer = new byte[1024];
950            // Read in the decrypted bytes and write the cleartext to out
961            int numRead = 0;
9737            while ((numRead = inputStream.read(buffer)) >= 0) {
9835                outputStream.write(buffer, 0, numRead);
990            }
1001            inputStream.close();
1011            outputStream.close();
1020        } catch (java.io.IOException ex) {
1030            logger.error(ex.getMessage());
1040            ex.printStackTrace();
1050        }
1061    }
1070}

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.