
Actualización Programa En/Decrypt
Publicado por Teo (9 intervenciones) el 28/05/2018 12:14:59
Buenas mi programa encripta un texto y lo vuelve a desencriptar, pero lo que yo necesito es que ese texto que se quiere trabajar, esté en un fichero de texto.
Os lo dejo por aqui:
Os lo dejo por aqui:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package exercici1;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
public class Prova {
private static final String ALGO = "AES/ECB/PKCS5Padding";
private static KeyGenerator kg = null;
private static SecretKey keyValue = null;
private static String text = "Hoy va a ser un gran día, mis amigos programadores me van a ayudar con este tema.";
public static void main(String[] args) throws Exception {
kg = KeyGenerator.getInstance("AES");
keyValue = generarLlaveSecreta(kg);
String textEncriptat = encrypt(text);
System.out.println(textEncriptat);
String textDesencriptat = decrypt(textEncriptat);
System.out.println(textDesencriptat);
}
public static String encrypt(String data) throws Exception {
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, keyValue);
byte[] encVal = c.doFinal(data.getBytes());
return Base64.getEncoder().encodeToString(encVal);
}
public static String decrypt(String encryptedData) throws Exception {
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, keyValue);
byte[] decordedValue = Base64.getDecoder().decode(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
return new String(decValue);
}
public static SecretKey generarLlaveSecreta(KeyGenerator kg) {
try {
kg.init(128);
SecretKey clau = kg.generateKey();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("Clau.secreta"));
out.writeObject(clau);
out.close();
return clau;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Valora esta pregunta


0