· 9 years ago · Nov 30, 2016, 10:42 AM
1public static void main(String[] args) throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException {
2 final byte[] IV_PARAM = { 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x01,
3 0x02, 0x03, 0x04, 0x05, };
4
5
6 // MAGATZEM DE CLAUS
7 String magatzemClaus = null;
8 String contraMagatzemClaus = null;
9 String alias = null;
10 String contraAlias = null;
11 String text=null;
12 String concatenat=null;
13
14
15 int opcio=0;
16
17 KeyStore keystore = null;
18 SecretKey secretkey= null;
19 PublicKey publickey=null;
20 PrivateKey privateKey=null;
21
22
23
24 byte[] missatgeEncriptat = null;
25 byte[] clauEncriptada = null;
26 byte[] clauMissatgeEncriptat=null;
27 byte[] missatgeADesencriptar = null;
28 byte[] missatgeDesencriptat= null;
29 byte[] clau=null;
30
31
32 // SCANNER
33 Scanner teclat = new Scanner(System.in);
34
35
36 // DEMANEM EL TEXT A ENCRIPTAR
37 System.out.print("Introdueix el text: ");
38 text = teclat.next();
39
40
41 // DEMANEM MAGATZEM DE CLAUS
42 System.out.print("MAGATZEM DE CLAUS: QuiMatA3n");
43 magatzemClaus = "QuiMatA3";
44
45
46 // DEMANEM CONTRASENYA PER AL MAGATZEM DE CLAUS
47 System.out.print("CONTRASENYA DE [" + magatzemClaus + "]: QuiMatA3n");
48 contraMagatzemClaus = "QuiMatA3";
49
50 //DEMANEM OPCIO
51 System.out.println("Transformaci� a realitzar [1] xifrar [2] desxifrar.");
52 opcio= teclat.nextInt();
53
54 //////////////////////////////////////////////
55 // KEYSTORE
56 //////////////////////////////////////////////
57
58
59 try {
60 keystore = KeyStore.getInstance("JKS");
61 File arxiu = new File(magatzemClaus + ".jks");
62 if (arxiu.isFile()) {
63 FileInputStream llegeix = new FileInputStream(arxiu);
64 keystore.load(llegeix, contraMagatzemClaus.toCharArray());
65 }
66 } catch (KeyStoreException e) {
67 System.err.println("error keystore: "+ e.toString());
68 } catch (FileNotFoundException e) {
69 System.err.println("Error fileinput: " +e.toString());
70 } catch (Exception e){
71 System.err.println("error al carregar: "+ e.toString());
72 }
73
74 switch (opcio) {
75 case 1:
76
77 ///////////////////////////////////////////
78 // GENEREM SECRET KEY
79 ///////////////////////////////////////////
80
81 try {
82
83 //GENEREM LA CLAU SECRETA
84 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
85 keyGenerator.init(192);
86 secretkey= keyGenerator.generateKey();
87
88
89 // ALIAS DE LA CLAU
90 System.out.println("ALIAS DE LA CLAU [" + contraMagatzemClaus + "]: QuiMatn");
91 alias = "QuiMat";
92
93
94 //////////////////////////////////
95 // ENCRIPTAR
96 /////////////////////////////////
97 Cipher xifrar= Cipher.getInstance("AES/CBC/PKCS5Padding");
98 IvParameterSpec ivParam = new IvParameterSpec(IV_PARAM);
99 xifrar.init(Cipher.ENCRYPT_MODE, secretkey, ivParam);
100 missatgeEncriptat = xifrar.doFinal(text.getBytes());
101
102 } catch (Exception e){
103 System.err.println("Error al generar clau secreta o al xifrar!" + e.toString());
104 }
105
106
107 //GENEREM LA CLAU PUBLICA
108
109 try {
110 publickey = keystore.getCertificate(alias).getPublicKey();
111 } catch (KeyStoreException e) {
112 System.err.println("Error al generar clau p�blica! "+e.toString() );
113 }
114
115
116 //ENCRPITEM CLAU SIM�TRICA AMB LA CLAU P�BLICA
117 // CLAU ENCRIPTADA
118 Cipher xifrar;
119 try {
120 xifrar = Cipher.getInstance("RSA/ECB/PKCS1Padding");
121 xifrar.init(Cipher.ENCRYPT_MODE, publickey);
122 clauEncriptada = xifrar.doFinal(secretkey.getEncoded());
123 } catch (Exception e){
124 System.err.println("Error al encriptar la clau sim�trica: "+ e.toString());
125 }
126
127 // CONCATENEM
128 concatenat = MadMarConverter.byteArrayToHexString(missatgeEncriptat)
129 + MadMarConverter.byteArrayToHexString(clauEncriptada);
130 System.out.println("Concatenaci� clau i text: "+concatenat);
131
132 break;
133
134
135
136
137
138
139 case 2:
140
141 ////////////////////////////////////////////////////////////////////
142 //////////// DESENCRIPTAR
143 ////////////////////////////////////////////////////////////////////
144
145 // ALIAS DE LA CLAU
146 System.out.println("ALIAS DE LA CLAU [" + contraMagatzemClaus + "]: QuiMatn");
147 alias = "QuiMat";
148 //teclat.next();
149
150 // CONTRASENYA ALIAS
151 System.out.println("CONTRASENYA DE L'ALIAS [" + alias + "]:QuiMatPn");
152 contraAlias = "QuiMatP";
153
154 clauMissatgeEncriptat= MadMarConverter.hexStringToByteArray(text);
155
156
157 //SEPAREM CLAU SIMETRICA DE MISSATGE ENCRIPTAT
158 clau = Arrays.copyOfRange(clauMissatgeEncriptat, 0, 256); //GUARDEM CLAU EN UN ARRAY
159 missatgeADesencriptar= Arrays.copyOfRange(clauMissatgeEncriptat, 256, clauMissatgeEncriptat.length); //MISSATGE EN UN ARRAY
160
161 //CLAU PRIVADA
162
163 privateKey = (PrivateKey) keystore.getKey(alias, contraAlias.toCharArray());
164
165
166
167 /////////////////////////////////////
168 // DESXIFRAT CLAU SIMETRICA
169 /////////////////////////////////////
170 byte[] clauDesencriptada = null;
171
172 try {
173
174 Cipher desxifrar = Cipher.getInstance("RSA/ECB/PKCS1Padding");
175 desxifrar.init(Cipher.DECRYPT_MODE, privateKey);
176 clauDesencriptada = desxifrar.doFinal(clau);
177 } catch (Exception e){
178 System.err.println("Error al desencriptar clau: "+e.toString());
179 }
180
181 secretkey= new SecretKeySpec(clauDesencriptada, 0, clauDesencriptada.length, "AES");
182
183
184 /////////////////////////////////////////////////////
185 // DESXIFRAR MISSATGE
186 /////////////////////////////////////////////////////
187 try {
188 Cipher desxifrar = Cipher.getInstance("AES/CBC/PKCS5Padding");
189 IvParameterSpec ivParam = new IvParameterSpec(IV_PARAM);
190 desxifrar.init(Cipher.DECRYPT_MODE, secretkey, ivParam);
191 missatgeDesencriptat = desxifrar.doFinal(missatgeADesencriptar);
192 } catch (Exception e){
193 System.err.println("Error al desencriptar missatge: "+e.toString());
194 }
195
196
197 System.out.println("Miisatge desencriptat: " +new String(missatgeDesencriptat));
198 break;
199 case 0:
200 System.out.println("deu!");
201 break;
202 }
203
204
205
206
207
208 teclat.close();
209}