· 7 years ago · Apr 22, 2018, 05:44 PM
1public String DoMAC(String passwd) throws NoSuchAlgorithmException, InvalidKeyException, IOException {
2
3 Mac mac = Mac.getInstance("HmacSHA256");
4 byte [] pass = passwd.getBytes();
5 SecretKey key = new SecretKeySpec(pass, "HmacSHA256");
6 mac.init(key);
7 return DatatypeConverter.printHexBinary(mac.doFinal());
8 }
9
10 public static void AssinaturaDigital(File filename,String password, String alias) throws Exception {
11 byte[] bFile = Files.readAllBytes(Paths.get(filename.getName()));
12 FileOutputStream fos = new FileOutputStream(filename.getName().split("\\.(?=[^\\.]+$)")[0]+".sig");
13 ObjectOutputStream oos = new ObjectOutputStream(fos);
14
15 FileInputStream fileInputStream = new FileInputStream(alias+".file");
16 KeyStore keyStore = KeyStore.getInstance("JKS");
17 keyStore.load(fileInputStream, password.toCharArray());
18 Key privateKey = keyStore.getKey(alias, password.toCharArray());
19
20 Signature s = Signature.getInstance("SHA256withRSA");
21 s.initSign((PrivateKey) privateKey);
22 s.update(bFile);
23 oos.writeObject(bFile);
24 oos.writeObject(s.sign( ));
25 fos.close();
26 }
27
28
29 public static void CifrarFile(String filename,String alias) throws Exception {
30 KeyGenerator kg = KeyGenerator.getInstance("AES");
31 kg.init(128);
32 SecretKey key = kg.generateKey();
33 Cipher c = Cipher.getInstance("AES");
34 c.init(Cipher.ENCRYPT_MODE, key);
35
36 FileInputStream fis;
37 FileOutputStream fos;
38 CipherOutputStream cos;
39
40 fis = new FileInputStream(filename);
41 fos = new FileOutputStream(filename.split("\\.(?=[^\\.]+$)")[0]+".cif");
42
43 cos = new CipherOutputStream(fos, c);
44 byte[] b = new byte[16];
45 int i = fis.read(b);
46 while (i != -1) {
47 cos.write(b, 0, i);
48 i = fis.read(b);
49 }
50 cos.close();
51 fos.close();
52 fis.close();
53
54 FileOutputStream kos = new FileOutputStream(filename.split("\\.(?=[^\\.]+$)")[0]+".key"); //
55 kos.write(key.getEncoded());
56 kos.close();
57
58
59 }
60
61
62
63 public static void DecifraFile(byte[] chave,String alias,String password,String filename) throws Exception {
64
65 int i=chave.length;
66 SecretKeySpec key = new SecretKeySpec(chave, "AES");
67 Cipher c = Cipher.getInstance("AES");
68
69 c.init(Cipher.DECRYPT_MODE, key);
70 System.out.println(filename);
71 String novonome= filename.split("/")[1];
72 FileInputStream cos = new FileInputStream(novonome.split("\\.(?=[^\\.]+$)")[0]+".cif");
73
74 CipherInputStream asduos = new CipherInputStream(cos, c);
75 String novonome1= filename.split("/")[1];
76 File file2 = new File(novonome1.split("\\.(?=[^\\.]+$)")[0]+".txt");
77 FileOutputStream fileOutputStream = new FileOutputStream(file2);
78 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
79
80 byte[] byt = new byte[16];
81 int size = asduos.read(chave);
82
83 while (i != -1) {
84 bufferedOutputStream.write(chave, 0, i);
85 bufferedOutputStream.flush();
86 i = asduos.read(chave);
87 }
88 asduos.close();
89 bufferedOutputStream.close();
90 fileOutputStream.close();
91 cos.close();
92
93
94 }
95
96
97public static void VerificarAssinatura(File filename,String password, String alias) throws Exception {
98 String novonome = filename.getName().split("\\.(?=[^\\.]+$)")[0]+".sig";
99 FileInputStream fis = new FileInputStream(novonome);
100 ObjectInputStream ois = new ObjectInputStream(fis);
101 byte [] data = (byte []) ois.readObject( ); //não fiz verificação de erro
102
103 byte signature[] = (byte[]) ois.readObject( ); //não fiz verificação de erro
104
105
106
107
108 FileInputStream fileInputStream = new FileInputStream(alias+".file");
109 KeyStore keyStore = KeyStore.getInstance("JKS");
110 keyStore.load(fileInputStream, password.toCharArray());
111 Key privateKey = keyStore.getKey(alias, password.toCharArray());
112 Certificate certificate = keyStore.getCertificate(alias);
113
114 PublicKey pk = certificate.getPublicKey( );
115
116 Signature s = Signature.getInstance("SHA256withRSA");
117 s.initVerify(pk);
118 s.update(data);
119 boolean verificado =s.verify(signature);
120 if (verificado) {
121 System.out.println("Ficheiro está valido e é seu");
122 }
123 else {
124 System.out.println("Ficheiro inválido , não é seu");
125 }
126 File fichCIF =new File(filename.getName().split("\\.(?=[^\\.]+$)")[0]+".cif");
127 File fichSIG =new File(filename.getName().split("\\.(?=[^\\.]+$)")[0]+".sig");
128 fichSIG.delete();
129 fichCIF.delete();
130 fis.close();
131 }
132
133}