Si AES, es un estandar ¿ significa que es una baga descripción de lo que debería hacer y no como implementarlo.?
. Por otro lado hablar de AES es algo confuso porque ya hay dos versiones, la primera especificaba Triple DES y la última, de allá por el año 2000, Rijndael -que, a decir verdad, no sé como funciona.
package aescrypt;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;public class AESCrypt { private static final String llaveSimetrica = "holamundocruel12holamundocruel12"; public static void main(String[] args) { byte [] datosCifrados; datosCifrados = encDatos("texto a cifrar"); System.out.println("Datos Cifrados -> " + asHex(datosCifrados)); System.out.println("Datos Descifrados -> " + dencDatos(datosCifrados)); } public static byte[] encDatos(String cadena){ byte [] aError = null; SecretKeySpec key = new SecretKeySpec(llaveSimetrica.getBytes(), "AES"); Cipher cipher; try { cipher = Cipher.getInstance("AES"); //Comienzo a encriptar cipher.init(Cipher.ENCRYPT_MODE, key); byte[] datosCifrados = cipher.doFinal(cadena.getBytes()); //cadena = a texto a cifrar /* * TODO: Representar los bytes como string vía base64, así será * humanamente leíble. La otra opción es expresar como hexadecimal * * En este caso lo imprimo en pantalla como bytes. */ return datosCifrados; } catch (Exception e) { return null; } } public static String dencDatos(byte [] datosCifrados){ SecretKeySpec key = new SecretKeySpec(llaveSimetrica.getBytes(), "AES"); Cipher cipher; try { cipher = Cipher.getInstance("AES"); //Comienzo a desencriptar cipher.init(Cipher.DECRYPT_MODE, key); byte[] datosDecifrados = cipher.doFinal(datosCifrados); return new String(datosDecifrados); /* * TODO: Representar los bytes como string vía base64, así será * humanamente leíble. La otra opción es expresar como hexadecimal * * En este caso lo imprimo en pantalla como bytes. */ } catch (Exception e) { return null; } } public static String asHex (byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 2); int i; for (i = 0; i < buf.length; i++) { if (((int) buf[i] & 0xff) < 0x10) strbuf.append("0"); strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); } return strbuf.toString(); }}
TripleDES y AES son algoritmos distintos ambos vienen de DES, son versiones posteriores distintas.
package projectclase_servidor;import javax.crypto.Cipher;import javax.crypto.spec.SecretKeySpec;public class AESCrypt { private static final String llaveSimetrica = "clave de 16 caracteres o multiplo de 16"; public static String encDatos(String cadena){ byte [] aError = null; SecretKeySpec key = new SecretKeySpec(llaveSimetrica.getBytes(), "AES"); Cipher cipher; try { cipher = Cipher.getInstance("AES"); //Comienzo a encriptar cipher.init(Cipher.ENCRYPT_MODE, key); byte[] datosCifrados = cipher.doFinal(cadena.getBytes()); //cadena = a texto a cifrar /* * TODO: Representar los bytes como string vía base64, así será * humanamente leíble. La otra opción es expresar como hexadecimal * * En este caso lo imprimo en pantalla como bytes. */ return new sun.misc.BASE64Encoder().encode(datosCifrados); } catch (Exception e) { return null; } } public static String dencDatos(String datosCifrados){ SecretKeySpec key = new SecretKeySpec(llaveSimetrica.getBytes(), "AES"); Cipher cipher; try { cipher = Cipher.getInstance("AES"); //Comienzo a desencriptar cipher.init(Cipher.DECRYPT_MODE, key); byte[] datosDecifrados = cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(datosCifrados)); return new String(datosDecifrados); /* * TODO: Representar los bytes como string vía base64, así será * humanamente leíble. La otra opción es expresar como hexadecimal * * En este caso lo imprimo en pantalla como bytes. */ } catch (Exception e) { return null; } }}