· 6 years ago · Feb 25, 2019, 08:30 PM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package aes;
7
8import java.io.BufferedReader;
9import java.io.FileInputStream;
10import java.io.FileNotFoundException;
11import java.io.FileOutputStream;
12import java.io.IOException;
13import java.io.RandomAccessFile;
14import java.nio.charset.StandardCharsets;
15import java.security.InvalidKeyException;
16import java.security.NoSuchAlgorithmException;
17import java.security.Provider;
18import java.security.SecureRandom;
19import java.security.Security;
20import java.security.spec.InvalidKeySpecException;
21import java.util.Arrays;
22import java.util.Scanner;
23import java.util.logging.Level;
24import java.util.logging.Logger;
25import javax.crypto.BadPaddingException;
26import javax.crypto.Cipher;
27import javax.crypto.IllegalBlockSizeException;
28import javax.crypto.KeyGenerator;
29import javax.crypto.NoSuchPaddingException;
30import javax.crypto.SecretKey;
31import javax.crypto.SecretKeyFactory;
32import javax.crypto.spec.SecretKeySpec;
33
34/**
35 *
36 * @author shoke
37 */
38public class CifradoAES {
39
40
41 private final static String ALGORITMO = "AES/ECB/PKCS5Padding";
42 private final static String FICHERO = "fichero.cifrado";
43
44 /**
45 * @param args the command line arguments
46 */
47 public static void main(String[] args) {
48 // TODO code application logic here
49 TextoRandom tr=null;
50 Scanner scn = new Scanner(System.in);
51 boolean encript = false;
52 String usuario;
53 String contrasena;
54 FileInputStream fe;
55 FileOutputStream fs;
56 String textoDescifrado;
57
58 if (args.length!=1){
59 System.err.println("Debe recibir 1 parámetro, debe indicar \"-c\" para cifrar y \"-d\" para descifrar.");
60 return;
61 }
62
63 switch (args[0]) {
64 case "-c":
65 encript = true;
66 tr = new TextoRandom();
67 break;
68 case "-d":
69 break;
70 default:
71 System.err.println("Parámetro incorrecto debe indicar \"-c\" para cifrar y \"-d\" para descifrar.");
72 return;
73 }
74
75
76 //System.out.println(tr.getTexto());
77
78 System.out.println("Introduzca el nombre de usuario: ");
79 usuario = scn.nextLine();
80
81 System.out.println("Introduzca la contraseña");
82 contrasena = scn.nextLine();
83
84 Provider[] prov = Security.getProviders();
85 System.err.println("");
86
87 try{
88 Cipher cipher = Cipher.getInstance(ALGORITMO);
89 //SecretKeyFactory skf = SecretKeyFactory.getInstance("AES");
90 //KeyGenerator kg = KeyGenerator.//KeyGenerator.getInstance("AES");
91 //kg.init(128);
92 SecureRandom sr = new SecureRandom();
93 sr.setSeed((usuario+contrasena).getBytes());
94
95 SecretKeySpec sks = new SecretKeySpec(sr.generateSeed(16),"AES");
96
97 SecretKey secretKey = sks;
98
99
100 if (encript){
101 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
102 }else{
103 cipher.init(Cipher.DECRYPT_MODE, secretKey);
104 }
105
106
107
108 //byte[] buffer = new byte[1024];
109 byte[] bloque_cifrado=null;
110 byte[] bloque_descifrado = null;
111
112 if (encript){
113 bloque_cifrado = cipher.doFinal(tr.getTexto().getBytes());
114 //textoCifrado= Arrays.toString(bloque_cifrado);
115 try {
116 fs = new FileOutputStream(FICHERO);
117 fs.write(bloque_cifrado);
118 } catch (FileNotFoundException ex) {
119 ex.printStackTrace(System.out);
120 } catch (IOException ex) {
121 ex.printStackTrace(System.out);
122 }
123 //System.out.println(textoCifrado);
124 }else{
125 try{
126 textoDescifrado="";
127 bloque_cifrado = new byte[1024];
128 byte[] texto_cifrado = new byte[0];
129 fe = new FileInputStream(FICHERO);
130 int bytesLeidos = fe.read(bloque_cifrado,0,1024);
131 while (bytesLeidos != -1){
132 //texto_cifrado = Arrays.copyOf(texto_cifrado, texto_cifrado.length+bytesLeidos);
133 bloque_descifrado = cipher.update(bloque_cifrado, 0, bytesLeidos);
134 //texto_cifrado = Arrays.copyOfRange(bloque_cifrado, 0, bytesLeidos);
135 textoDescifrado += (new String(bloque_cifrado));
136 bytesLeidos = fe.read(bloque_cifrado,0,1024);
137 }
138 //bloque_descifrado = cipher.doFinal(texto_cifrado);
139 //textoDescifrado = new String(bloque_descifrado);
140 System.out.println(textoDescifrado);
141
142
143 } catch (FileNotFoundException ex) {
144 ex.printStackTrace(System.out);
145 } catch (IOException ex) {
146 ex.printStackTrace(System.out);
147 }
148 }
149 } catch (NoSuchAlgorithmException|NoSuchPaddingException|InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
150 ex.printStackTrace(System.out);
151 }
152
153
154
155
156
157 }
158
159}
160
161final class TextoRandom {
162 private final String texto;
163
164 public TextoRandom() {
165 texto = generaTexto();
166 }
167
168 public String getTexto(){
169 return this.texto;
170 }
171
172 public String generaTexto(){
173 String textoGen;
174 String patron = "ABCDEFGHIIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz0123456789";
175 char[] cadena = new char[(int)(Math.random()*(100-50)+1)+50];
176
177 for (int i= 0;i<cadena.length;i++){
178 cadena[i]=patron.charAt((int)(Math.random()*(patron.length())));
179 }
180 textoGen = String.valueOf(cadena);
181 return textoGen;
182 }
183}