· 7 years ago · Feb 21, 2018, 12:00 PM
1public class Cifrar {
2
3// Definición del tipo de algoritmo a utilizar (AES, DES, RSA)
4private final static String alg = "AES";
5private final static int keyLength=128;
6private final static int ivLength=128;
7
8// Definición del modo de cifrado a utilizar
9private final static String cI = "AES/CBC/PKCS5Padding";
10
11
12
13// Aqui generamos la clave aleatoria AES
14public static byte [] ClaveAleatoria() throws Exception {
15 //Generamos la clave aleatoria.
16 KeyGenerator keyGen = KeyGenerator.getInstance(alg);
17 keyGen.init(keyLength, new SecureRandom());
18 SecretKey aesKey = keyGen.generateKey();
19 byte[] aesKeyBytes = aesKey.getEncoded();
20 return aesKeyBytes;
21
22}
23
24
25
26 public static byte[] VectorInicialAleatorio() throws Exception {
27 byte [] iv = SecureRandom.getSeed(ivLength/8);
28 return iv;
29
30 }
31
32
33
34
35
36public static byte[] encrypt( byte [] aesKeyBytes, byte[] iv , String cleartext) throws Exception {
37 Cipher cipher = Cipher.getInstance(cI);
38 //SecretKeySpec skeySpec = new SecretKeySpec(aesKeyBytes.getBytes(), alg);
39 SecretKeySpec skeySpec = new SecretKeySpec(aesKeyBytes, alg);
40 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
41 //System.out.println(ivParameterSpec);
42 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
43 byte[] encrypted = cipher.doFinal(cleartext.getBytes());
44 //return new String(encodeBase64(encrypted));
45 //return new String(Base64.encode(encrypted));
46 return encrypted;
47}
48
49
50
51
52
53
54
55 //AQUI ESTA EL PROBLEMAA- PARECE QUE LE FALTAN BYTES
56
57 public static String decrypt(byte [] aesKeyBytes, byte[] iv, byte[] encrypted) throws Exception {
58 Cipher cipher = Cipher.getInstance(cI);
59 SecretKeySpec skeySpec = new SecretKeySpec(aesKeyBytes, alg);
60 IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
61 //byte[] enc = Base64.encode(encrypted);
62 System.out.println(encrypted+"aqui despues viene el fallo");
63 //byte[] enc = Base64.decode(encrypted);
64
65 cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
66 //byte[] decrypted = cipher.doFinal(enc);
67 byte[] decrypted = cipher.doFinal(encrypted);
68 return new String(decrypted);
69}
70
71 }
72
73public class main {
74
75 public static void main(String[] args) throws Exception {
76
77 String cleartext = "hola";
78
79 byte[] textocifrado=
80
81
82
83 crypto.Cifrar.encrypt(crypto.Cifrar.ClaveAleatoria(),crypto.Cifrar.VectorInicialAleatorio(),cleartext);
84
85 System.out.println("Texto encriptado: "+textocifrado);
86 System.out.println("Texto desencriptado: "+crypto.Cifrar.decrypt(crypto.Cifrar.ClaveAleatoria(),crypto.Cifrar.VectorInicialAleatorio(),textocifrado));
87
88Texto encriptado: ����ß�Ϻ��Qθ�
89Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
90����ß�Ϻ��Qθ�aqui despues viene el fallo
91 at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
92 at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
93 at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
94 at javax.crypto.Cipher.doFinal(Cipher.java:2087)
95 at crypto.Cifrar.decrypt(Cifrar.java:129)
96 at crypto.main.main(main.java:24)
97C:UsersJurparkAppDataLocalNetBeansCache8.1executor-snippetsrun.xml:53: Java returned: 1
98BUILD FAILED (total time: 1 second)
99
100new String("hola".getBytes()); // te devuelve un nuevo String con el texto "hola".
101
102crypto.Cifrar.encrypt(
103 crypto.Cifrar.ClaveAleatoria(),
104 crypto.Cifrar.VectorInicialAleatorio(),
105 cleartext);
106
107crypto.Cifrar.decrypt(
108 crypto.Cifrar.ClaveAleatoria(),
109 crypto.Cifrar.VectorInicialAleatorio(),
110 textocifrado))