0% found this document useful (0 votes)
17 views12 pages

Uptoex 3

The document outlines the implementation of various cryptographic algorithms in Java, including symmetric key algorithms (DES and AES), asymmetric key algorithms (RSA), key exchange algorithms (Diffie-Hellman), and digital signature schemes using RSA and SHA-256. Each section provides the aim, algorithm steps, and example Java code demonstrating the implementation and successful verification of the outputs. The document serves as a comprehensive guide to understanding and applying these cryptographic techniques.

Uploaded by

indumathisec
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)
17 views12 pages

Uptoex 3

The document outlines the implementation of various cryptographic algorithms in Java, including symmetric key algorithms (DES and AES), asymmetric key algorithms (RSA), key exchange algorithms (Diffie-Hellman), and digital signature schemes using RSA and SHA-256. Each section provides the aim, algorithm steps, and example Java code demonstrating the implementation and successful verification of the outputs. The document serves as a comprehensive guide to understanding and applying these cryptographic techniques.

Uploaded by

indumathisec
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/ 12

1.

Implement symmetric key algorithms


i. DES

AIM:
To use Data Encryption Standard (DES) Algorithm for a
practical application like User Message Encryption.

ALGORITHM:
1. Create a DES Key.
2. Create a Cipher instance from Cipher class, specify the
following information
and separated by a slash (/).
a. Algorithm name
b. Mode (optional)
c. Padding scheme (optional)
3. Convert String into Byte[] array format.
4. Make the Cipher in encryption mode, and encrypt it with
the Cipher.doFinal() method.
5. Make the Cipher in decrypt mode, and decrypt it with
the Cipher.doFinal() method.

PROGRAM:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
public class DESNetworkSecurity {
public static void main(String[] args) throws Exception {
KeyGenerator keyGen =
KeyGenerator.getInstance("DES");
keyGen.init(56);
SecretKey secretKey = keyGen.generateKey();

Cipher cipher = Cipher.getInstance("DES");

// Encryption
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String plaintext = "Secure Network Communication";
byte[] encrypted = cipher.doFinal(plaintext.getBytes());
System.out.println("Ciphertext: " +
Base64.getEncoder().encodeToString(encrypted));

// Decryption
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("Decrypted Text: " + new
String(decrypted));
}
}
RESULT:
Thus the java program for DES Algorithm has been
implemented and the output verified successfully.
1.Implement symmetric key algorithms
ii. AES
AIM:
To use Advanced Encryption Standard (AES) Algorithm for a
practical application like URL Encryption.
ALGORITHM:
1. AES is based on a design principle known as a
substitution–permutation.
2. AES does not use a Feistel network like DES, it uses a
variant of Rijndael.
3. It has a fixed block size of 128 bits, and a key size of
128, 192, or 256 bits.
4. AES operates on a 4 × 4 column-major order array of
bytes, termed the state
PROGRAM
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

public class AESExample {


public static void main(String[] args) throws Exception {
KeyGenerator keyGen =
KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();

Cipher cipher = Cipher.getInstance("AES");

// Encryption
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String plaintext = "Secure AES Communication";
byte[] encrypted = cipher.doFinal(plaintext.getBytes());
System.out.println("Ciphertext: " +
Base64.getEncoder().encodeToString(encrypted));
// Decryption
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("Decrypted Text: " + new
String(decrypted));
}
}
RESULT:
Thus the java program for AES Algorithm has been
implemented for Encryption and the output verified successfully
Implementation of Asymmetric Key Algorithm(RSA) and Key
Exchange Algorithm (Diffie-Hellman)
2)Implementation of Asymmetric Key Algorithm
i)RSA
Aim
To implement the RSA asymmetric encryption and
decryption algorithm to understand the concept of public
and private key encryption.
Algorithms

1)Key Generation: Use Java’s KeyPairGenerator to generate the RSA public and
private key pairs.

2)Encryption: Use Java's Cipher class to encrypt the plaintext using the public key.

3)Decryption: Use Java's Cipher class to decrypt the ciphertext using the private key.

Program:
import java.security.*;
import javax.crypto.*;

public class RSAExample {

// Method to generate RSA key pair


public static KeyPair generateRSAKeyPair(int keySize) throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(keySize);
return keyPairGenerator.generateKeyPair();
}

// Method to encrypt the message using RSA public key


public static byte[] encryptMessage(PublicKey publicKey, String message) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(message.getBytes());
}

// Method to decrypt the message using RSA private key


public static String decryptMessage(PrivateKey privateKey, byte[] encryptedMessage) throws
Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBytes = cipher.doFinal(encryptedMessage);
return new String(decryptedBytes);
}

public static void main(String[] args) {


try {
// Generate RSA key pair (2048 bits)
KeyPair keyPair = generateRSAKeyPair(2048);

String message = "Hello, RSA!";


System.out.println("Original Message: " + message);

// Encrypt the message using the public key


byte[] encryptedMessage = encryptMessage(keyPair.getPublic(), message);
System.out.println("Encrypted Message: " + new String(encryptedMessage));

// Decrypt the message using the private key


String decryptedMessage = decryptMessage(keyPair.getPrivate(), encryptedMessage);
System.out.println("Decrypted Message: " + decryptedMessage);

} catch (Exception e) {
e.printStackTrace();
}
}
}

RESULT:
Thus the java program for RSA Algorithm has been implemented for and the output
verified successfully

2)ii)Implementation of Key Exchange Algorithm ii)Diffie-Hellman


Aim
To implement the Diffie-Hellman key exchange algorithm to understand how two parties can
securely share a secret over an insecure channel without directly transmitting the secret.
Algorithm:

1)Key Generation: Use Java’s KeyPairGenerator class with the DH algorithm to generate
Diffie-Hellman key pairs.
2)Shared Secret Computation: Use Java's KeyAgreement class to compute the shared secret
using the private key and the other party's public key.

Program

import java.security.*;
import javax.crypto.KeyAgreement;
import javax.crypto.spec.DHParameterSpec;

public class DiffieHellmanExample {

// Method to generate Diffie-Hellman key pair


public static KeyPair generateDHKeyPair(DHParameterSpec dhParamSpec) throws
NoSuchAlgorithmException, InvalidAlgorithmParameterException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DH");
keyPairGenerator.initialize(dhParamSpec);
return keyPairGenerator.generateKeyPair();
}

// Method to perform the Diffie-Hellman key exchange and get the shared secret
public static byte[] getSharedSecret(PrivateKey privateKey, PublicKey publicKey) throws
Exception {
KeyAgreement keyAgreement = KeyAgreement.getInstance("DH");
keyAgreement.init(privateKey);
keyAgreement.doPhase(publicKey, true);
return keyAgreement.generateSecret();
}

public static void main(String[] args) {


try {
// Define the Diffie-Hellman parameters (prime number p and generator g)
BigInteger p = new BigInteger("179");
BigInteger g = new BigInteger("2");
DHParameterSpec dhParamSpec = new DHParameterSpec(p, g);

// Generate DH key pairs for Party 1 and Party 2


KeyPair keyPair1 = generateDHKeyPair(dhParamSpec);
KeyPair keyPair2 = generateDHKeyPair(dhParamSpec);

// Get the public keys for both parties


PublicKey publicKey1 = keyPair1.getPublic();
PublicKey publicKey2 = keyPair2.getPublic();

// Perform the key exchange to obtain the shared secret


byte[] sharedSecret1 = getSharedSecret(keyPair1.getPrivate(), publicKey2);
byte[] sharedSecret2 = getSharedSecret(keyPair2.getPrivate(), publicKey1);

// Verify that both parties have the same shared secret


System.out.println("Shared Secret (Party 1): " + new String(sharedSecret1));
System.out.println("Shared Secret (Party 2): " + new String(sharedSecret2));

// Both shared secrets should be the same


assert new String(sharedSecret1).equals(new String(sharedSecret2));

} catch (Exception e) {
e.printStackTrace();
}
}
}

RESULT:
Thus the java program for Diffie-Hellman key exchange Algorithm has been
implemented for and the output verified successfully

3)Implement digital signature schemes


Aim
To implement the digital signature scheme using RSA encryption and SHA-256 hashing
algorithm in Java.
Algorithm
1)KeyPairGenerator: Generates RSA public and private keys.
2)Signature: Used to sign the message and verify the signature.
3)MessageDigest: Hashes the message using a cryptographic algorithm like SHA-256.
4)Base64 Encoding: The digital signature is encoded in Base64 for easy display and
transmission.

Program
import java.security.*;
import javax.crypto.*;
import java.util.Base64;

public class DigitalSignatureExample {

// Generate RSA Key Pair


public static KeyPair generateRSAKeyPair(int keySize) throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(keySize);
return keyPairGenerator.generateKeyPair();
}

// Method to sign the message with the private key


public static String signMessage(String message, PrivateKey privateKey) throws Exception {
// Create a MessageDigest (SHA-256)
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] messageHash = messageDigest.digest(message.getBytes());

// Create a Signature object and initialize it with the private key


Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privateKey);

// Sign the message hash


signature.update(messageHash);
byte[] digitalSignature = signature.sign();

// Convert the signature to a Base64 string for easy display


return Base64.getEncoder().encodeToString(digitalSignature);
}

// Method to verify the message with the public key


public static boolean verifySignature(String message, String signatureStr, PublicKey
publicKey) throws Exception {
// Create a MessageDigest (SHA-256)
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
byte[] messageHash = messageDigest.digest(message.getBytes());

// Convert the Base64 string back to a byte array (digital signature)


byte[] signatureBytes = Base64.getDecoder().decode(signatureStr);

// Create a Signature object and initialize it with the public key


Signature signature = Signature.getInstance("SHA256withRSA");
signature.initVerify(publicKey);

// Verify the signature


signature.update(messageHash);
return signature.verify(signatureBytes);
}

public static void main(String[] args) {


try {
// Step 1: Generate RSA Key Pair
KeyPair keyPair = generateRSAKeyPair(2048); // Use 2048-bit RSA keys for better
security

// Step 2: Sign the message using the private key


String message = "This is a secret message!";
String signedMessage = signMessage(message, keyPair.getPrivate());

System.out.println("Original Message: " + message);


System.out.println("Digital Signature (Base64): " + signedMessage);

// Step 3: Verify the signature using the public key


boolean isVerified = verifySignature(message, signedMessage, keyPair.getPublic());

System.out.println("Signature Verified: " + isVerified);

} catch (Exception e) {
e.printStackTrace();
}
}
}
RESULT:
Thus the java program for digital signature schemes algorithm has been implemented
for and the output verified successfully

You might also like