· 9 years ago · Nov 09, 2016, 04:20 PM
1package m9.uf1.e2.abeltorres;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.security.MessageDigest;
10import java.security.NoSuchAlgorithmException;
11import java.util.Arrays;
12import java.util.Scanner;
13import javax.crypto.BadPaddingException;
14import javax.crypto.Cipher;
15import javax.crypto.IllegalBlockSizeException;
16import javax.crypto.NoSuchPaddingException;
17import javax.crypto.SecretKey;
18import javax.crypto.spec.SecretKeySpec;
19
20/**
21 *
22 * @author Abel Torres
23 *
24 */
25
26public class A08_XifrarDesxifrarFitxerAES {
27
28 public static void main(String[] args) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
29 Scanner sc = new Scanner(System.in);
30
31 String fitxer = "", clau = "";
32 SecretKey sKey;
33
34 System.out.print("Entra el nom del fitxer: ");
35 fitxer = sc.nextLine();
36
37 InputStream arxiu = new FileInputStream( "" );
38 OutputStream arxiu_sortida = new FileOutputStream ( "" );
39 File file = new File (fitxer);
40
41
42 if(file.exists()){
43 System.out.print("Entra una clau: ");
44 clau = sc.nextLine();
45
46 sKey = contrasenya(clau);
47
48 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
49
50 if ( fitxer.endsWith(".aes") ){
51
52
53
54 }
55 else{
56
57 byte[] buffer = new byte [1024];
58 byte[] bloc;
59 String textXifrat = "";
60 int bytesLlegits = 0;
61
62 bytesLlegits = arxiu.read(buffer);
63
64 while ( bytesLlegits != -1 ){
65 bloc = cipher.update(buffer, 0, bytesLlegits);
66 textXifrat = textXifrat + bloc;
67 bytesLlegits = arxiu.read(buffer);
68 arxiu_sortida.write(textXifrat.getBytes());
69 }
70
71 arxiu.close();
72 arxiu_sortida.close();
73 }
74 }
75 else{
76 System.out.println("No existeix cap fitxer amb aquest nom.");
77 }
78
79 sc.close();
80 }