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