0% found this document useful (0 votes)
16 views6 pages

Acm

Uploaded by

qsousdbiuqef
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)
16 views6 pages

Acm

Uploaded by

qsousdbiuqef
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/ 6

LAB 1(DES)

import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
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 = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
public static byte[] generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
keyGen.init(56); // 56-bit key
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}
public static void main(String[] args) throws Exception {
// Generate a random key
byte[] key = generateKey();
// Data to encrypt
String data = "ManojM";
byte[] dataBytes = data.getBytes();
// Encrypt the data
byte[] encryptedData = encrypt(dataBytes, key);
System.out.println("Encrypted Data: " + Base64.getEncoder().encodeToString(encryptedData));

// Decrypt the data


byte[] decryptedData = decrypt(encryptedData, key);
System.out.println("Decrypted Data: " + new String(decryptedData));
}
}

OUTPUT :
LAB2(AES)

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Scanner; public class
Lab2_AES {
// Method to generate an AES key
public static SecretKey generateKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128); // AES supports 128, 192, and 256-bit keys
return keyGen.generateKey();
}
// Method to create AES key from a given key string
public static SecretKey getKeyFromString(String keyString) throws Exception {
if (keyString.length() != 16) {
throw new IllegalArgumentException("Key must be exactly 16 characters long.");
}
byte[] keyBytes = keyString.getBytes();
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 = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().encodeToString(encryptedBytes);
}
// Method to decrypt the encrypted text
public static String decrypt(String encryptedText, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(decodedBytes);
return new String(decryptedBytes);
}
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
// Input key from user
System.out.print("Enter a 16-character key for AES: ");
String keyString = scanner.nextLine();
// Generate AES key from the input string
SecretKey key = getKeyFromString(keyString);
// Input plain text from user
System.out.print("Enter the text to encrypt: ");
String plainText = scanner.nextLine();
// Encrypt the input text
String encryptedText = encrypt(plainText, key);
System.out.println("Encrypted Text: " + encryptedText);
// Decrypt the encrypted text
String decryptedText = decrypt(encryptedText, key);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}

OUTPUT:
LAB3(SHA-1)

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
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 = MessageDigest.getInstance("SHA-256");
// Update the digest with the byte array of the input string
byte[] hashBytes = sha256Digest.digest(input.getBytes());
// Convert the byte array into hexadecimal format
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b); // Convert each byte to hex
if (hex.length() == 1) hexString.append('0'); // Add leading zero if needed
hexString.append(hex);
}
return hexString.toString();
}
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
// Input plain text from user
System.out.print("Enter the text to hash using SHA-256: ");
String input = scanner.nextLine();
// Validate input
if (input.isEmpty()) {
System.out.println("Error: Input cannot be empty.");
return; }
// Generate the SHA-256 hash of the input text
String sha256Hash = generateSHA256Hash(input);
System.out.println("SHA-256 Hash: " + sha256Hash);
} catch (NoSuchAlgorithmException e) {
System.out.println("Error: SHA-256 algorithm not found.");
}
}
}

OUTPUT:
LAB4(RSA)

import javax.crypto.Cipher;
import java.security.*;
import java.util.Base64;
import java.util.Scanner;

public class Lab4_RSA {

// Method to generate RSA key pair (public and private keys)


public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
keyPairGen.initialize(2048); // Set key size (2048 bits for good security)
return keyPairGen.generateKeyPair();
}

// Method to encrypt plain text using public key


public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
return Base64.getEncoder().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 = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decodedBytes = Base64.getDecoder().decode(encryptedText);
byte[] decryptedBytes = cipher.doFinal(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 = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();

// Take input from user


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the text to encrypt: ");
String plainText = scanner.nextLine();

// Validate input
if (plainText.isEmpty()) {
System.out.println("Error: Input cannot be empty.");
return;
}

// Encrypt the input text


String encryptedText = encrypt(plainText, publicKey);
System.out.println("Encrypted Text: " + encryptedText);

// Decrypt the encrypted text


String decryptedText = decrypt(encryptedText, privateKey);
System.out.println("Decrypted Text: " + decryptedText);
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}

OUTPUT:

You might also like