Open leomont opened 1 month ago
Integrantes: Danilo Quiceno Hernandez Sofía Clavijo Zuluaga Jorge Alejandro Posada Quiñones Juan Camilo Gañan Blandon Gustavo Marín Sergio Andres Rodriguez Triana Johan Restrepo Daniel Felipe Rengifo Prieto
UML: +-----------------------+ | AESCipher | +-----------------------+ | - secretKey: SecretKey| +-----------------------+ | + generateKey(): SecretKey | | + encrypt(plainText: String, secretKey: SecretKey): String | | + decrypt(cipherText: String, secretKey: SecretKey): String | +-----------------------+ Código implementando UML en este código se pretende encriptar una cadena de caracteres usando método AES cifrado simétrico y posteriormente se pide digitar la clave para desbloquear la forma original de la cadena de caracteres package AESCipher2;
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; import java.util.Objects; import java.util.Scanner;
public class AESCipher {
private SecretKey secretKey;
public SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");/*KeyGenerator es una clase que proporciona la funcionalidad para
generar claves de cifrado. getInstance("AES") obtiene un generador de claves para el algoritmo AES.*/
keyGen.init(256); /*( Tamaño de la clave init(256); configura el generador de claves para producir claves de 256 bits.
El tamaño de la clave es crucial para la seguridad del cifrado. Cuanto mayor sea el tamaño de la clave,
más segura será la encriptación, pero también requiere más recursos computacionales*/
this.secretKey = keyGen.generateKey(); /*generateKey() genera una nueva clave AES basada en la configuración establecida previamente (en este caso, una clave de 256 bits).
La clave generada se asigna al atributo secretKey de la clase.*/
return this.secretKey; //devuelve la clave secreta recien generada
}
public String encrypt(String plainText, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public String decrypt(String cipherText, SecretKey secretKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(cipherText));
return new String(decryptedBytes);
}
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
AESCipher aesCipher = new AESCipher();
// Generar una clave AES
SecretKey secretKey = aesCipher.generateKey();
System.out.println("secretKey = " + secretKey);
System.out.println("Ingrese un texto a cifrar: ");
// Texto a cifrar
String plainText = scanner.nextLine();
// Cifrar el texto
String cipherText = aesCipher.encrypt(plainText, secretKey);
System.out.println("Encrypted Text: " + cipherText);
String cipherText2 = "";
boolean control = false;
int count2 = 3;
System.out.println("ingrese la clave para descifrar el mensaje: ");
cipherText2 = scanner.nextLine();
if (!Objects.equals(cipherText2, cipherText)){
control = true;
}
while (control) {
System.out.println("error Ingreso una clave invalida, " + "Le quedan " + count2 + " intentos, " + "ingrese la clave correcta: ");
cipherText2 = scanner.nextLine();
if (Objects.equals(cipherText2, cipherText)) {
control = false;
}
else if (count2 == 1){
System.out.println("Se le acabaron los intentos su pc explotara!.");
control = false;
}
count2--;
}
// Descifrar el texto
String decryptedText = aesCipher.decrypt(cipherText2, secretKey);
System.out.println("Decrypted Text: " + decryptedText);
}
}
https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/javax/crypto/package-summary.html
https://es.wikipedia.org/wiki/MD5 https://www.techtarget.com/searchsecurity/definition/Advanced-Encryption-Standard#:~:text=The%20Advanced%20Encryption%20Standard%20(AES)%20is%20a%20symmetric%20block%20cipher,cybersecurity%20and%20electronic%20data%20protection.
https://es.wikipedia.org/wiki/Triple_DES