LAB 1(DES)
import [Link];
import [Link].Base64;
import [Link];
import [Link];
import [Link];
import [Link];
public class Lab1_DES {
private static final String ALGORITHM = "DES";
private static final String TRANSFORMATION = "DES/ECB/PKCS5Padding";
public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = [Link](TRANSFORMATION);
[Link](Cipher.ENCRYPT_MODE, secretKey);
return [Link](data);
}
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = [Link](TRANSFORMATION);
[Link](Cipher.DECRYPT_MODE, secretKey);
return [Link](data);
}
public static byte[] generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGen = [Link](ALGORITHM);
[Link](56); // 56-bit key
SecretKey secretKey = [Link]();
return [Link]();
}
public static void main(String[] args) throws Exception {
// Generate a random key
byte[] key = generateKey();
// Data to encrypt
String data = "ManojM";
byte[] dataBytes = [Link]();
// Encrypt the data
byte[] encryptedData = encrypt(dataBytes, key);
[Link]("Encrypted Data: " + [Link]().encodeToString(encryptedData));
// Decrypt the data
byte[] decryptedData = decrypt(encryptedData, key);
[Link]("Decrypted Data: " + new String(decryptedData));
}
}
OUTPUT :
LAB2(AES)
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].Base64;
import [Link]; public class
Lab2_AES {
// Method to generate an AES key
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = [Link]("AES");
[Link](128); // AES supports 128, 192, and 256-bit keys
return [Link]();
}
// Method to create AES key from a given key string
public static SecretKey getKeyFromString(String keyString) throws Exception {
if ([Link]() != 16) {
throw new IllegalArgumentException("Key must be exactly 16 characters long.");
}
byte[] keyBytes = [Link]();
return new SecretKeySpec(keyBytes, 0, 16, "AES"); // 16 bytes = 128 bits
}
// Method to encrypt the plain text
public static String encrypt(String plainText, SecretKey key) throws Exception {
Cipher cipher = [Link]("AES");
[Link](Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = [Link]([Link]());
return [Link]().encodeToString(encryptedBytes);
}
// Method to decrypt the encrypted text
public static String decrypt(String encryptedText, SecretKey key) throws Exception {
Cipher cipher = [Link]("AES");
[Link](Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = [Link]().decode(encryptedText);
byte[] decryptedBytes = [Link](decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) {
try (Scanner scanner = new Scanner([Link])) {
// Input key from user
[Link]("Enter a 16-character key for AES: ");
String keyString = [Link]();
// Generate AES key from the input string
SecretKey key = getKeyFromString(keyString);
// Input plain text from user
[Link]("Enter the text to encrypt: ");
String plainText = [Link]();
// Encrypt the input text
String encryptedText = encrypt(plainText, key);
[Link]("Encrypted Text: " + encryptedText);
// Decrypt the encrypted text
String decryptedText = decrypt(encryptedText, key);
[Link]("Decrypted Text: " + decryptedText);
} catch (Exception e) {
[Link]("An error occurred: " + [Link]());
}
}
}
OUTPUT:
LAB3(SHA-1)
import [Link];
import [Link];
import [Link];
public class Lab3_SHA1 {
// Method to generate SHA-256 hash of the input string
public static String generateSHA256Hash(String input) throws NoSuchAlgorithmException {
// Create an instance of MessageDigest with SHA-256 algorithm
MessageDigest sha256Digest = [Link]("SHA-256");
// Update the digest with the byte array of the input string
byte[] hashBytes = [Link]([Link]());
// Convert the byte array into hexadecimal format
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = [Link](0xff & b); // Convert each byte to hex
if ([Link]() == 1) [Link]('0'); // Add leading zero if needed
[Link](hex);
}
return [Link]();
}
public static void main(String[] args) {
try (Scanner scanner = new Scanner([Link])) {
// Input plain text from user
[Link]("Enter the text to hash using SHA-256: ");
String input = [Link]();
// Validate input
if ([Link]()) {
[Link]("Error: Input cannot be empty.");
return; }
// Generate the SHA-256 hash of the input text
String sha256Hash = generateSHA256Hash(input);
[Link]("SHA-256 Hash: " + sha256Hash);
} catch (NoSuchAlgorithmException e) {
[Link]("Error: SHA-256 algorithm not found.");
}
}
}
OUTPUT:
LAB4(RSA)
import [Link];
import [Link].*;
import [Link].Base64;
import [Link];
public class Lab4_RSA {
// Method to generate RSA key pair (public and private keys)
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = [Link]("RSA");
[Link](2048); // Set key size (2048 bits for good security)
return [Link]();
}
// Method to encrypt plain text using public key
public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
Cipher cipher = [Link]("RSA");
[Link](Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = [Link]([Link]());
return [Link]().encodeToString(encryptedBytes); // Convert to Base64 for readability
}
// Method to decrypt encrypted text using private key
public static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
Cipher cipher = [Link]("RSA");
[Link](Cipher.DECRYPT_MODE, privateKey);
byte[] decodedBytes = [Link]().decode(encryptedText);
byte[] decryptedBytes = [Link](decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) {
try {
// Generate RSA key pair (public and private keys)
KeyPair keyPair = generateKeyPair();
PublicKey publicKey = [Link]();
PrivateKey privateKey = [Link]();
// Take input from user
Scanner scanner = new Scanner([Link]);
[Link]("Enter the text to encrypt: ");
String plainText = [Link]();
// Validate input
if ([Link]()) {
[Link]("Error: Input cannot be empty.");
return;
}
// Encrypt the input text
String encryptedText = encrypt(plainText, publicKey);
[Link]("Encrypted Text: " + encryptedText);
// Decrypt the encrypted text
String decryptedText = decrypt(encryptedText, privateKey);
[Link]("Decrypted Text: " + decryptedText);
} catch (Exception e) {
[Link]("An error occurred: " + [Link]());
}
}
}
OUTPUT: