Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * SignatureUtil.java | ||
3 | */ | ||
4 | package lab.scd.encrypt.signature; | ||
5 | |||
6 | import java.security.*; | ||
7 | import java.security.spec.InvalidKeySpecException; | ||
8 | import java.security.spec.X509EncodedKeySpec; | ||
9 | import java.io.*; | ||
10 | |||
11 | /** | ||
12 | * | ||
13 | * This class is part of the laborat or4_2crypto project. | ||
14 | * | ||
15 | * Clasa implementeaza functiile pentru generarea si verificarea de semnaturi | ||
16 | * digitale folosind algoritmul DES. | ||
17 | * | ||
18 | * | ||
19 | */ | ||
20 | public class SignatureUtil { | ||
21 | |||
22 | /** | ||
23 | * Metoda genereaza semnatura digitala pentru sir de bytes. | ||
24 | * @param textForSign sirul de bytes pentru care va fi generata semnatura | ||
25 | * @param signFile fisierul in care va fi salvata semnatura | ||
26 | * @param keyFile fisiserul in care va fi salvata cheia publica folosita pentru generarea semnaturii | ||
27 | * @throws NoSuchAlgorithmException | ||
28 | * @throws NoSuchProviderException | ||
29 | * @throws InvalidKeyException | ||
30 | * @throws SignatureException | ||
31 | * @throws IOException | ||
32 | */ | ||
33 | public static void generateSignature(byte[] textForSign, String signFile, String keyFile) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException, IOException | ||
34 | { | ||
35 | /* Genreaza o pereche de chie: publica\privata */ | ||
36 | |||
37 | KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN"); | ||
38 | SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); | ||
39 | |||
40 | keyGen.initialize(1024, random); | ||
41 | |||
42 | KeyPair pair = keyGen.generateKeyPair(); | ||
43 | PrivateKey priv = pair.getPrivate(); | ||
44 | PublicKey pub = pair.getPublic(); | ||
45 | |||
46 | |||
47 | /* Initializeaza obiectul de tip Signature ce fa fi utilizat pentru generarea semnaturii */ | ||
48 | |||
49 | Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); | ||
50 | |||
51 | dsa.initSign(priv); | ||
52 | |||
53 | /* Initializeaza obiectul de tip Signature cu textul ce va fi semnat */ | ||
54 | |||
55 | |||
56 | dsa.update(textForSign); | ||
57 | |||
58 | /* Genereaza semnatura */ | ||
59 | |||
60 | byte[] realSig = dsa.sign(); | ||
61 | |||
62 | |||
63 | /* Salveaza semnatura intr-un fisiser */ | ||
64 | FileOutputStream sigfos = new FileOutputStream(signFile); | ||
65 | sigfos.write(realSig); | ||
66 | |||
67 | sigfos.close(); | ||
68 | |||
69 | |||
70 | /* Salveaza cheia publica folosta pentru generarea semnaturii */ | ||
71 | byte[] key = pub.getEncoded(); | ||
72 | FileOutputStream keyfos = new FileOutputStream(keyFile); | ||
73 | keyfos.write(key); | ||
74 | |||
75 | keyfos.close(); | ||
76 | } | ||
77 | |||
78 | /** | ||
79 | * Verifica semnatura digitala pentru un sir de bytes | ||
80 | * @param keyFile fisiserul in care se gaseste cheia publica | ||
81 | * @param signature fisiserul in care se gaseste semnatura | ||
82 | * @param data sirul de bytes ce va fi verificat | ||
83 | * @return true daca sirul de bytes data nu este alterat si corespunde semnaturii digitale | ||
84 | * @throws IOException | ||
85 | * @throws NoSuchAlgorithmException | ||
86 | * @throws NoSuchProviderException | ||
87 | * @throws InvalidKeySpecException | ||
88 | * @throws InvalidKeyException | ||
89 | * @throws SignatureException | ||
90 | */ | ||
91 | public static boolean verifySignature(String keyFile, byte[] signature, byte[] data) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, InvalidKeyException, SignatureException{ | ||
92 | /* importa cheia publica necesara pentru verificarea semnaturii */ | ||
93 | |||
94 | FileInputStream keyfis = new FileInputStream(keyFile); | ||
95 | byte[] encKey = new byte[keyfis.available()]; | ||
96 | keyfis.read(encKey); | ||
97 | |||
98 | keyfis.close(); | ||
99 | |||
100 | X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); | ||
101 | |||
102 | KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN"); | ||
103 | PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); | ||
104 | |||
105 | /* Construieste obiectul de tip Signature pentru verificarea semnaturii */ | ||
106 | Signature sig = Signature.getInstance("SHA1withDSA", "SUN"); | ||
107 | sig.initVerify(pubKey); | ||
108 | |||
109 | /* pregateste datele ce vor fi verificate */ | ||
110 | sig.update(data); | ||
111 | |||
112 | /* verifica */ | ||
113 | return sig.verify(signature); | ||
114 | |||
115 | } | ||
116 | |||
117 | public static String readFile(String filename){ | ||
118 | String content = null; | ||
119 | try { | ||
120 | BufferedReader bf = new BufferedReader(new FileReader(filename)); | ||
121 | StringBuffer c = new StringBuffer(100); | ||
122 | String line = bf.readLine(); | ||
123 | while(line!= null){ | ||
124 | c.append(line); | ||
125 | line = bf.readLine(); | ||
126 | } | ||
127 | content = c.toString(); | ||
128 | } catch (Exception e) { | ||
129 | |||
130 | e.printStackTrace(); | ||
131 | System.exit(0); | ||
132 | } | ||
133 | return content; | ||
134 | } | ||
135 | |||
136 | public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException, IOException, InvalidKeySpecException { | ||
137 | |||
138 | String mesaj ="Message for testing digital signature algorithm."; | ||
139 | //SignatureUtil.generateSignature(mesaj.getBytes(), "signature.txt", "key.dat"); | ||
140 | |||
141 | boolean r = SignatureUtil.verifySignature("key.dat", SignatureUtil.readFile("signature.txt").getBytes(), mesaj.getBytes()); | ||
142 | System.out.println("Messaje is OK = "+r); | ||
143 | } | ||
144 | } |