· 5 years ago · Aug 20, 2020, 10:06 PM
1package me.DavidLake.Entrega1;
2
3import javax.crypto.Cipher;
4import javax.crypto.spec.SecretKeySpec;
5import java.nio.charset.StandardCharsets;
6import java.nio.file.Files;
7import java.nio.file.Paths;
8import java.nio.file.Path;
9
10public class AdvancedEncryptionStandard{
11
12 public static byte[] decrypt(byte[] cipherText, String password){
13
14 try{
15
16 String key = "MZygpewJsCpR"+password;
17 byte[] keyInBytes = key.getBytes(StandardCharsets.UTF_8);
18 SecretKeySpec secretKey = new SecretKeySpec(keyInBytes, "AES");
19 Cipher cipher = Cipher.getInstance("AES");
20 cipher.init(Cipher.DECRYPT_MODE, secretKey);
21 return cipher.doFinal(cipherText);
22 }
23 catch(Exception e)
24 {
25 return new byte[0];
26 }
27 }
28
29 /**
30 * Desencripta el archivo archivoEncriptado.txt con el password. Si el password falla retorna una cadena vacia
31 *
32 * @param password El password para desencriptar el archivo es una permutacion de la cadena abcd
33 * @return Retorna una cadena con el contenido del archivo desencriptado
34 */
35 public static String desencriptarArchivo(String password)
36 {
37 try{
38 Path path = Paths.get("archivoEncriptado.txt");
39 byte[] archivoEncriptado = Files.readAllBytes(path);
40 byte[] decryptedCipherText = AdvancedEncryptionStandard.decrypt(archivoEncriptado,password);
41 return new String(decryptedCipherText);
42 }
43 catch(Exception e)
44 {
45 e.printStackTrace(System.out);
46 return "";
47 }
48 }
49}