Uptoex 3
Uptoex 3
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();
// 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;
// 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.*;
} catch (Exception e) {
e.printStackTrace();
}
}
}
RESULT:
Thus the java program for RSA Algorithm has been implemented for and the output
verified successfully
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;
// 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();
}
} 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
Program
import java.security.*;
import javax.crypto.*;
import java.util.Base64;
} catch (Exception e) {
e.printStackTrace();
}
}
}
RESULT:
Thus the java program for digital signature schemes algorithm has been implemented
for and the output verified successfully