• EnglishSpanishGermanFrenchPolishChinese (Traditional)


EnglishSpanishGermanFrenchPolishChinese (Traditional)

Operating systems, scripting, PowerShell and security

Operating systems, software development, scripting, PowerShell tips, network and security

Menú principal
  • Categorías
  • Cursos
  • Libro de PowerShell
  • Lo mejor
  • Lo último
  • Proyectos
  • Contactar
Ir al contenido

Cifrar y descifrar con AES desde Java (con clave aleatoria)

Java
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
import javax.crypto.*;    
import java.security.*;  
 
public class Java {
 
private static SecretKey key = null;        
private static Cipher cipher = null;
 
public static void main(String[] args) throws Exception
{
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
cipher = Cipher.getInstance("AES");
 
String clearText = "hola";
byte[] clearTextBytes = clearText.getBytes("UTF8");
System.out.println("Texto en claro: " + clearText);
System.out.println("Texto en Bytes claro: " + clearTextBytes);
 
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] cipherBytes = cipher.doFinal(clearTextBytes);
String cipherText = new String(cipherBytes, "UTF8");
System.out.println("Texto cifrado: " + cipherText);
System.out.println("Texto en Bytes cifrado: " + cipherBytes);
 
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(cipherBytes);
String decryptedText = new String(decryptedBytes, "UTF8");
System.out.println("Texto descifrado: " + decryptedText);
System.out.println("Texto en Bytes cifrado: " + decryptedBytes);
}
}

Publicado el día 5 de febrero de 2021

CATEGORÍAS

Java, Seguridad

ETIQUETAS

Cifrado, cipher, Descifrar, Java, Jesús Niño, Jesús Niño Camazón, System.out.println

MÁS

  • Cifrar y descifrar con RSA utilizando ECB desde Java (generando clave privada y clave pública)
  • Mostrar un documento del servidor eXist-db y se imprime en la salida estándar
  • Mostrar propiedades (java.class.path", "java.home", "java.vendor", "java.version", "os.name", "os.version", "user.dir", "user.home", "user.name") utilizando y sin utilizar el gestor de…
  • Firmar un mensaje y comprobar que se ha firmado correctamente en Java
  • Ejecutar un programa desde un lenguaje de programación
  • Ejercicios de seguridad: práctica sobre virus