0% found this document useful (0 votes)
9 views1 page

Name: Abid Mahat Roll No B14 Prn:-2122000218 AES Implementation

The document contains code for encrypting and decrypting strings using AES encryption. It takes in a string to encrypt, encrypts it with a secret key, then decrypts it and outputs the original string.

Uploaded by

aabidmahat144
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views1 page

Name: Abid Mahat Roll No B14 Prn:-2122000218 AES Implementation

The document contains code for encrypting and decrypting strings using AES encryption. It takes in a string to encrypt, encrypts it with a secret key, then decrypts it and outputs the original string.

Uploaded by

aabidmahat144
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Name : Abid Mahat

Roll No B14
Prn :- 2122000218

AES Implementation

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.*;

public class AES {


public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the string:");
String originalString = sc.nextLine();
String key = "ThisIsASecretKey"; // 128 bit key
String encryptedString = encrypt(originalString, key);
System.out.println("Encrypted String: " + encryptedString);
String decryptedString = decrypt(encryptedString, key);
System.out.println("Decrypted String: " + decryptedString);
sc.close();
}
public static String encrypt(String strToEncrypt, String secret) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), "AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(strToEncrypt.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
public static String decrypt(String strToDecrypt, String secret) throws Exception {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), "AES");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(strToDecrypt));
return new String(decryptedBytes);
}
}

Output:-

You might also like