0% found this document useful (0 votes)
19 views

Activity 2 - Encrypt & Decrypt - Docs

g

Uploaded by

Estrella Rosete
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Activity 2 - Encrypt & Decrypt - Docs

g

Uploaded by

Estrella Rosete
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Running Code for Assignment String decryptedMiddlename =

decrypt(middlenameText, secretKey);
import java.util.*;
String decryptedLastname =
import javax.crypto.Cipher; decrypt(lastnameCipherText, secretKey);
import javax.crypto.KeyGenerator; System.out.println("Decrypted Middlename: " +
import javax.crypto.SecretKey; decryptedMiddlename);

import java.util.Base64; System.out.println("Decrypted Lastname: " +


decryptedLastname);

}
public class EncryptionDecryptionAES{

static Cipher cipher;


public static byte[] encrypt(String plainText, SecretKey
secretKey) throws Exception {

public static void main(String[] args) throws Exception cipher = Cipher.getInstance("AES");


{
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String firstname = "";
byte[] plainTextByte = plainText.getBytes();
String middlename = "";
byte[] encryptedByte =
String lastname = ""; cipher.doFinal(plainTextByte);

Base64.Encoder encoder = Base64.getEncoder();

Scanner Manching=new Scanner (System.in); String encryptedText =


encoder.encodeToString(encryptedByte);
System.out.print("Enter Firstname: ");
return encryptedByte;
firstname=Manching.nextLine();
}

System.out.print("Enter Middlename: ");


public static String decrypt(byte[] cipherText,
middlename=Manching.nextLine();
SecretKey secretKey) throws Exception {

cipher.init(Cipher.DECRYPT_MODE, secretKey);
System.out.print("Enter Lastname: ");
byte[] decryptedByte = cipher.doFinal(cipherText);
lastname=Manching.nextLine();
String decryptedText = new String(decryptedByte);

return decryptedText;
SecretKey secretKey =
}
KeyGenerator.getInstance("AES").generateKey();
}
byte[] middlenameText = encrypt(middlename,
secretKey);

byte[] lastnameCipherText = encrypt(lastname,


secretKey);

System.out.println("Encrypted Middlename: " +


new String(middlenameText));

System.out.println("Encrypted Lastname: " + new


String(lastnameCipherText));

You might also like