Question

In: Computer Science

Write java programs to implement CBC mode of data encryption standard?

Write java programs to implement CBC mode of data encryption standard?

Solutions

Expert Solution

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 *******************************************************


Related Solutions

Data Encryption Standard (DES) is insecure because of the size of the encryption key. Advanced Encryption...
Data Encryption Standard (DES) is insecure because of the size of the encryption key. Advanced Encryption Standard (AES) is the current NIST standard for encryption and is used in most applications. Explain the differences between the following concepts as they apply to both algorithms. Concept DES AES S-BOX Permutation Key Size
For this question, you need to implement Java code for the following Modified Ciphertext encryption and...
For this question, you need to implement Java code for the following Modified Ciphertext encryption and attack for known patterns to find the code. Please read the modified Ciphertext carefully and examples before implementing them.   Modified Ciphertext is working as follows: If a plain text (only letters ignore uppercase or lowercase) and sequences of numbers are given, it encrypts the given plain text by shifting each letter in a message the characters based on the given numbers one forward direction...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide the following inputs and outputs for each function/program: EncryptCaesar Two inputs: A string of the plaintext to encrypt A key (a number) ▪ For the Caesar cipher: This will indicate how many characters to shift (e.g. for a key=3, A=>D, B=>E, ..., X=>A, Y=>B, Z=>C). Note that the shift is circular. One output: ◦ A string of the ciphertext or codeword DecryptCaesar Two inputs:...
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
For CBC mode with DES, if there is an error in C_1, are any blocks beyond...
For CBC mode with DES, if there is an error in C_1, are any blocks beyond P_2 affected when decryption occurs? If instead there is a source error in P_1, how many ciphertext output blocks are affected?
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
Why in Cipher Block Chaining (CBC) mode, an initialization vector is needed in addition to the...
Why in Cipher Block Chaining (CBC) mode, an initialization vector is needed in addition to the Key?
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT