Biblioteca Java - Blame information for rev 3
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
3 | mihai | 1 | /* |
2 | * DESEncDec.java | ||
3 | */ | ||
4 | package lab.scd.encrypt.des; | ||
5 | |||
6 | import java.io.UnsupportedEncodingException; | ||
7 | import java.security.NoSuchAlgorithmException; | ||
8 | |||
9 | import javax.crypto.*; | ||
10 | import javax.crypto.spec.SecretKeySpec; | ||
11 | |||
12 | /** | ||
13 | * Class created by @author Mihai HULEA. | ||
14 | * | ||
15 | * This class is part of the laborator4_2crypto project. | ||
16 | * | ||
17 | * Aceasta clasa implementeaza mecanismele pentru criptarea si decriptarea de | ||
18 | * string-uri folosind algoritmul DES cu cheie simetricai. | ||
19 | */ | ||
20 | public class DESEncDec { | ||
21 | Cipher ecipher; | ||
22 | Cipher dcipher; | ||
23 | SecretKey key; | ||
24 | |||
25 | /** | ||
26 | * In cadrul construictorului sunt initializate componentele necesare pentru | ||
27 | * criptarea decriptarea datelor. In cadrul constructorului se genereaza cheia | ||
28 | * folosita pentru criptare\decriptare. | ||
29 | * Este folosit aloritmul DES cu cheie simetrica. | ||
30 | * | ||
31 | */ | ||
32 | DESEncDec(){ | ||
33 | try { | ||
34 | |||
35 | //genereaza cheia ce va fi folosita in cadrul procesului de criptare decriptare | ||
36 | key = DESEncDec.generateKey(); | ||
37 | |||
38 | //initializeaza obiectul folosy pentru criptarea datelor | ||
39 | ecipher = Cipher.getInstance("DES"); | ||
40 | ecipher.init(Cipher.ENCRYPT_MODE, key); | ||
41 | |||
42 | //intilizeaza obiectul folosit pentru decriptarea datelor | ||
43 | dcipher = Cipher.getInstance("DES"); | ||
44 | dcipher.init(Cipher.DECRYPT_MODE, key); | ||
45 | |||
46 | } catch (javax.crypto.NoSuchPaddingException e) { | ||
47 | } catch (java.security.NoSuchAlgorithmException e) { | ||
48 | } catch (java.security.InvalidKeyException e) { | ||
49 | } | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * In cadrul construictorului sunt initializate componentele necesare pentru | ||
54 | * criptarea decriptarea datelor. | ||
55 | * @param key cheia folosta pentru criptare decriptare | ||
56 | */ DESEncDec(SecretKey key) { | ||
57 | try { | ||
58 | this.key = key; | ||
59 | System.out.println("Algorithm used to generate key:"+key.getAlgorithm()+"."); | ||
60 | ecipher = Cipher.getInstance("DES"); | ||
61 | dcipher = Cipher.getInstance("DES"); | ||
62 | ecipher.init(Cipher.ENCRYPT_MODE, key); | ||
63 | dcipher.init(Cipher.DECRYPT_MODE, key); | ||
64 | |||
65 | } catch (javax.crypto.NoSuchPaddingException e) { | ||
66 | e.printStackTrace(); | ||
67 | } catch (java.security.NoSuchAlgorithmException e) { | ||
68 | e.printStackTrace(); | ||
69 | } catch (java.security.InvalidKeyException e) { | ||
70 | e.printStackTrace(); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | |||
75 | /** | ||
76 | * Metoda realizeaza criptarea unui sir. | ||
77 | * @param str sirul ce trebuie criptat | ||
78 | * @return sirul criptat | ||
79 | */ | ||
80 | public String encrypt(String str) { | ||
81 | try { | ||
82 | // Encode the string into bytes using utf-8 | ||
83 | byte[] utf8 = str.getBytes("UTF8"); | ||
84 | |||
85 | // Encrypt | ||
86 | byte[] enc = ecipher.doFinal(utf8); | ||
87 | |||
88 | // Encode bytes to base64 to get a string | ||
89 | return new sun.misc.BASE64Encoder().encode(enc); | ||
90 | } catch (javax.crypto.BadPaddingException e) { | ||
91 | } catch (IllegalBlockSizeException e) { | ||
92 | } catch (UnsupportedEncodingException e) { | ||
93 | } catch (java.io.IOException e) { | ||
94 | } | ||
95 | return null; | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * Metoda decripteaza un sir | ||
100 | * @param str sirul ce trebuie decriptat | ||
101 | * @return sirul decriptat | ||
102 | */ | ||
103 | public String decrypt(String str) { | ||
104 | try { | ||
105 | // Decode base64 to get bytes | ||
106 | byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str); | ||
107 | |||
108 | // Decrypt | ||
109 | byte[] utf8 = dcipher.doFinal(dec); | ||
110 | |||
111 | // Decode using utf-8 | ||
112 | return new String(utf8, "UTF8"); | ||
113 | } catch (javax.crypto.BadPaddingException e) { | ||
114 | } catch (IllegalBlockSizeException e) { | ||
115 | } catch (UnsupportedEncodingException e) { | ||
116 | } catch (java.io.IOException e) { | ||
117 | } | ||
118 | return null; | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * Returneaza cheia folosta pentru criptare\decriptare ca un sir de bytes | ||
123 | * @return | ||
124 | */ | ||
125 | public byte[] getKeyBytes(){ | ||
126 | return key.getEncoded(); | ||
127 | } | ||
128 | |||
129 | /** | ||
130 | * Genereaza o cheie. | ||
131 | * @return | ||
132 | */ | ||
133 | public static SecretKey generateKey(){ | ||
134 | SecretKey key=null; | ||
135 | try { | ||
136 | |||
137 | key = KeyGenerator.getInstance("DES").generateKey(); | ||
138 | System.out.println("Key generated with algorithm:"+key.getAlgorithm()); | ||
139 | } catch (NoSuchAlgorithmException e) { | ||
140 | |||
141 | e.printStackTrace(); | ||
142 | System.exit(0); | ||
143 | } | ||
144 | return key; | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Construieste un obiect de tip cheie be baza unui sir de bytes. In cadrul | ||
149 | * acestei aplicatii aceasta metoda este folosita pentru incarcarea unei chei | ||
150 | * dintr-un fisier. Cheia este citita din fisier ca un sir de bytes dupa care | ||
151 | * este apelata aceasta metoda pentru a genera obiectul de tip SecretKey. | ||
152 | * @param keyBytes | ||
153 | * @return | ||
154 | */ | ||
155 | public static SecretKey loadKey(byte[] keyBytes){ | ||
156 | SecretKey key = new SecretKeySpec(keyBytes, "DES"); | ||
157 | return key; | ||
158 | } | ||
159 | } |