| Line | Hits | Source |
|---|---|---|
| 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 | 2 | public class DESSymmetricCipher extends SymmetricCipher{ |
| 38 | ||
| 39 | 2 | private static final transient Logger logger = Logger.getLogger(DESSymmetricCipher.class.getName()); |
| 40 | ||
| 41 | /**The 8-byte salt to use to instantiate ciphers.**/ | |
| 42 | 6 | byte[] salt = { |
| 43 | 3 | (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32, |
| 44 | 3 | (byte)0x56, (byte)0x35, (byte)0xE3, (byte)0x03 |
| 45 | }; | |
| 46 | ||
| 47 | /**The number of iteration to use in DES**/ | |
| 48 | 3 | int iterationCount = 19; |
| 49 | ||
| 50 | 3 | public DESSymmetricCipher(SymmetricKey passPhrase){ |
| 51 | try { | |
| 52 | // Create the key | |
| 53 | 3 | KeySpec keySpec = new PBEKeySpec(passPhrase.getValue().toCharArray(), salt, iterationCount); |
| 54 | 3 | SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); |
| 55 | 3 | encryptionCipher = Cipher.getInstance(key.getAlgorithm()); |
| 56 | 3 | decryptionCipher = Cipher.getInstance(key.getAlgorithm()); |
| 57 | ||
| 58 | // Prepare the parameter to the ciphers | |
| 59 | 3 | AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount); |
| 60 | ||
| 61 | // Create the ciphers | |
| 62 | 3 | encryptionCipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); |
| 63 | 3 | decryptionCipher.init(Cipher.DECRYPT_MODE, key, paramSpec); |
| 64 | 0 | } catch (Exception ex) { |
| 65 | 0 | logger.error(ex.getMessage()); |
| 66 | 0 | ex.printStackTrace(); |
| 67 | 0 | } |
| 68 | 3 | } |
| 69 | ||
| 70 | public void encrypt(File inputFile, File outputFile){ | |
| 71 | try { | |
| 72 | // Bytes read from in will be decrypted | |
| 73 | 1 | CipherInputStream inputStream = new CipherInputStream(new FileInputStream(inputFile), encryptionCipher); |
| 74 | 1 | FileOutputStream outputStream = new FileOutputStream(outputFile); |
| 75 | 1 | byte[] buffer = new byte[1024]; |
| 76 | 0 | // Read in the decrypted bytes and write the cleartext to out |
| 77 | 1 | int numRead = 0; |
| 78 | 37 | while ((numRead = inputStream.read(buffer)) >= 0) { |
| 79 | 35 | outputStream.write(buffer, 0, numRead); |
| 80 | 0 | } |
| 81 | 1 | inputStream.close(); |
| 82 | 1 | outputStream.close(); |
| 83 | 0 | } catch (java.io.IOException ex) { |
| 84 | 0 | logger.error(ex.getMessage()); |
| 85 | 0 | ex.printStackTrace(); |
| 86 | 0 | } |
| 87 | 1 | } |
| 88 | ||
| 89 | public void decrypt(File inputFile, File outputFile){ | |
| 90 | try { | |
| 91 | // Bytes read from in will be decrypted | |
| 92 | 1 | CipherInputStream inputStream = new CipherInputStream(new FileInputStream(inputFile), decryptionCipher); |
| 93 | 1 | FileOutputStream outputStream = new FileOutputStream(outputFile); |
| 94 | 1 | byte[] buffer = new byte[1024]; |
| 95 | 0 | // Read in the decrypted bytes and write the cleartext to out |
| 96 | 1 | int numRead = 0; |
| 97 | 37 | while ((numRead = inputStream.read(buffer)) >= 0) { |
| 98 | 35 | outputStream.write(buffer, 0, numRead); |
| 99 | 0 | } |
| 100 | 1 | inputStream.close(); |
| 101 | 1 | outputStream.close(); |
| 102 | 0 | } catch (java.io.IOException ex) { |
| 103 | 0 | logger.error(ex.getMessage()); |
| 104 | 0 | ex.printStackTrace(); |
| 105 | 0 | } |
| 106 | 1 | } |
| 107 | 0 | } |
|
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |