In: Computer Science
Write java programs to implement CBC mode of data encryption standard?
EncryptAndDecryptCBCMode.java
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class EncryptAndDecryptCBCMode {
private String message;
private SecretKey secretKey;
private byte[] initVector;
private byte[] encryptedText;
public EncryptAndDecryptCBCMode()
{
this.message = "";
this.secretKey = null;
this.initVector = null;
encryptedText = null;
}
public EncryptAndDecryptCBCMode(String message)throws
Exception
{
this.message = message;
this.encryptedText = null;
// generate the secret key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
// generate the secret key
SecretKey key = keyGenerator.generateKey();
this.secretKey = key;
// generate the initialization vector
this.initVector = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(this.initVector);
}
public String getMessage() { return message; }
public String encrypt() throws Exception
{
// create an instance of the encryption cipher
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// create an instance of SecretKeySpec
SecretKeySpec keySpec = new
SecretKeySpec(this.secretKey.getEncoded(), "AES");
// create an instance of IvParameterSpec
IvParameterSpec ivParamSpec = new
IvParameterSpec(this.initVector);
// initialize the cipher to ENCRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivParamSpec);
// encrypt the message
this.encryptedText = cipher.doFinal(this.message.getBytes());
return (Base64.getEncoder().encodeToString(encryptedText));
}
public String decrypt() throws Exception
{
// create an instance of the decryption cipher
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
// create an instance of SecretKeySpec
SecretKeySpec keySpec = new
SecretKeySpec(this.secretKey.getEncoded(), "AES");
// create an instance of IvParameterSpec
IvParameterSpec ivParamSpec = new
IvParameterSpec(this.initVector);
// initialize the cipher to DECRYPT_MODE
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParamSpec);
// decrypt the encryoted message
byte[] decryptedText = cipher.doFinal(this.encryptedText);
return new String(decryptedText);
}
}
EncryptDecryptTest.java (Driver class)
import java.util.Scanner;
public class EncryptDecryptTest {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a message: ");
String message = sc.nextLine().trim();
try
{
EncryptAndDecryptCBCMode encrypt = new
EncryptAndDecryptCBCMode(message);
System.out.println("\nEncrypting the message...\n"
+ "The encrypted message is: " + encrypt.encrypt());
System.out.println("\nDecrypting the encrypted message...\n"
+ "The decrypted message is: " + encrypt.decrypt());
}catch(Exception ex){
System.out.println("\nERROR: " + ex.getMessage() + "\nExiting
program..");
System.exit(0);
}
}
}
******************************************************************** SCREENSHOT *******************************************************