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.asymmetric;
21  
22  import java.io.BufferedInputStream;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileOutputStream;
26  import java.security.Signature;
27  
28  import org.apache.log4j.Logger;
29  
30  public class RSAAsymmetricCipher implements IAsymmetricCipher {
31  	
32  	private static final transient Logger logger = Logger.getLogger(RSAAsymmetricCipher.class.getName());
33  	
34  	public void sign(File input, File output, base.jdbs.cryptography.asymmetric.PrivateKey privateKey) {
35  		
36  		try{
37  			
38  			/* Create a Signature object and initialize it with the private key */
39  			Signature rsa = Signature.getInstance("SHA1withRSA"); 
40  			rsa.initSign(privateKey.getValue());
41  	
42  			/* Update and sign the data */		
43  			FileInputStream fileInputStream = new FileInputStream(input);
44  			BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
45  			byte[] buffer = new byte[1024];
46  			int numRead;
47  			while (bufferedInputStream.available() != 0) {
48  				numRead = bufferedInputStream.read(buffer);
49  				rsa.update(buffer, 0, numRead);
50  			};
51  			
52  			bufferedInputStream.close();
53  			
54  			/* Now that all the data to be signed has been read in, 
55  			 generate a signature for it */
56  			byte[] realSig = rsa.sign();
57  		
58  			/* Save the signature in a file */
59  			FileOutputStream sigfos = new FileOutputStream(output);
60  			sigfos.write(realSig);
61  			sigfos.close();
62  		} catch (Exception e) {
63  			logger.error("Caught exception " + e.toString());
64  		}
65  	}
66  	
67  	public boolean checkSignature(File signedInputFile, File dataFile, PublicKey publicKey) {
68  		try{
69          /* input the signature bytes */
70          FileInputStream sigfis = new FileInputStream(signedInputFile);
71          byte[] sigToVerify = new byte[sigfis.available()]; 
72          sigfis.read(sigToVerify );
73  
74          sigfis.close();
75  
76          /* create a Signature object and initialize it with the public key */
77          Signature sig = Signature.getInstance("SHA1withRSA");
78          sig.initVerify(publicKey.getValue());
79  
80          /* Update and verify the data */
81  
82          FileInputStream datafis = new FileInputStream(dataFile);
83          BufferedInputStream bufin = new BufferedInputStream(datafis);
84  
85          byte[] buffer = new byte[1024];
86          int len;
87          while (bufin.available() != 0) {
88              len = bufin.read(buffer);
89              sig.update(buffer, 0, len);
90              };
91  
92          bufin.close();
93  
94  
95          boolean verifies = sig.verify(sigToVerify);
96  
97          System.out.println("signature verifies: " + verifies);
98  
99          	return verifies;
100 
101     } catch (Exception e) {
102         System.err.println("Caught exception " + e.toString());
103 	}
104     return false;
105 	}
106 }