Write a java program to encrypt and decrypt a message. The user should be able to choose between a Caesar cipher or a Vigenere cipher for their message. Use JOptionPane for user input.
Code
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class EncryptDecrypt
{
public static void main(String[] args)
{
int userChoice,shift,enryptDecryptChoice;
String plainText,cipherText,msg;
String vignerkey;
JFrame f;
f=new JFrame();
do
{
userChoice=getUserInput("1. Ceaser Cipher 2. Vigenere cipher 3.
Exit");
switch(userChoice)
{
case 1:
enryptDecryptChoice=getUserInput("1. Encryption 2. Decryption
");
if(enryptDecryptChoice==1)
{
shift=getUserInput("Enter the shift: ");
plainText=JOptionPane.showInputDialog("Enter the plain Text.
");
cipherText=encryptCeaser(plainText, shift);
msg="The cipher text is: "+cipherText;
JOptionPane.showMessageDialog(f,msg);
}
else if(enryptDecryptChoice==2)
{
shift=getUserInput("Enter the shift: ");
cipherText=JOptionPane.showInputDialog("Enter the encrypted Text.
");
plainText=decryptCeaser(cipherText, shift);
msg="The plaintext is: "+plainText;
JOptionPane.showMessageDialog(f,msg);
}
break;
case 2:
enryptDecryptChoice=getUserInput("1. Encryption 2. Decryption
");
if(enryptDecryptChoice==1)
{
plainText=JOptionPane.showInputDialog("Enter the plain Text.
");
vignerkey=JOptionPane.showInputDialog("Enter the key: ");
cipherText=encryptVigenere(plainText, vignerkey);
msg="The cipher text is: "+cipherText;
JOptionPane.showMessageDialog(f,msg);
}
else if(enryptDecryptChoice==2)
{
cipherText=JOptionPane.showInputDialog("Enter the encrypted Text.
");
vignerkey=JOptionPane.showInputDialog("Enter the key: ");
plainText=decryptVigenere(cipherText, vignerkey);
msg="The plaintext is: "+plainText;
JOptionPane.showMessageDialog(f,msg);
}
break;
case 3:
break;
default:
JOptionPane.showMessageDialog(f,"Invalid Choice. Please try
again.");
break;
}
}while(userChoice!=3);
}
private static int getUserInput(String msg) {
return Integer.parseInt(JOptionPane.showInputDialog(msg));
}
public static String encryptCeaser(String plaintext,int
shift)
{
char alphabet;
String ciphertext="";
for(int i=0; i < plaintext.length();i++)
{
// Shift one character at a time
alphabet = plaintext.charAt(i);
// if alphabet lies between a and z
if(alphabet >= 'a' && alphabet <= 'z')
{
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'z'
if(alphabet > 'z') {
// reshift to starting position
alphabet = (char) (alphabet+'a'-'z'-1);
}
ciphertext = ciphertext + alphabet;
}
// if alphabet lies between 'A'and 'Z'
else if(alphabet >= 'A' && alphabet <= 'Z') {
// shift alphabet
alphabet = (char) (alphabet + shift);
// if shift alphabet greater than 'Z'
if(alphabet > 'Z') {
//reshift to starting position
alphabet = (char) (alphabet+'A'-'Z'-1);
}
ciphertext = ciphertext + alphabet;
}
else {
ciphertext = ciphertext + alphabet;
}
}
return ciphertext;
}
public static String decryptCeaser(String ciphertext,int
shift)
{
String decryptMessage = "";
for(int i=0; i < ciphertext.length();i++)
{
// Shift one character at a time
char alphabet = ciphertext.charAt(i);
// if alphabet lies between a and z
if(alphabet >= 'a' && alphabet <= 'z')
{
// shift alphabet
alphabet = (char) (alphabet - shift);
// shift alphabet lesser than 'a'
if(alphabet < 'a') {
//reshift to starting position
alphabet = (char) (alphabet-'a'+'z'+1);
}
decryptMessage = decryptMessage + alphabet;
}
// if alphabet lies between A and Z
else if(alphabet >= 'A' && alphabet <= 'Z')
{
// shift alphabet
alphabet = (char) (alphabet - shift);
//shift alphabet lesser than 'A'
if (alphabet < 'A') {
// reshift to starting position
alphabet = (char) (alphabet-'A'+'Z'+1);
}
decryptMessage = decryptMessage + alphabet;
}
else
{
decryptMessage = decryptMessage + alphabet;
}
}
return decryptMessage;
}
public static String encryptVigenere(String text, final String
key)
{
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
public static String decryptVigenere(String text, final String
key)
{
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++)
{
char c = text.charAt(i);
if (c < 'A' || c > 'Z')
continue;
res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
j = ++j % key.length();
}
return res;
}
}
outputs
If you have any query regarding the code please ask me in the
comment i am here for help you. Please do not direct thumbs down
just ask if you have any query. And if you like my work then please
appreciates with up vote. Thank You.
Write a java program to encrypt and decrypt a message. The user should be able to...
Write a program that can encrypt and decrypt using the El-GAMALCipher Please show in Java with message = 100.
Write a javascript program which implements the following two classical cryptosystem which we covered in class: Affine Cipher Vigenere Cipher Your program should consist of at least five functions: Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string and a key as...
Question 1: Encrypt the message “attackhmsfleet” using Playfair Cipher with key =”security”. Use filler “x” and use “I” for the pair “I/J” in the table. Question 2: Using Vigenere cipher, encrypt the message “sendmoremoney” using the key “cipher”.
Using RSA cipher, public key e=3, private key d=7 Encrypt the following message “Hello there” Decrypt the previous message
Write a short Java program that uses private key (symmetric) encryption method to encrypt a short string. Approximate algorithm: Enter a text string Use Java to generate a key and encrypt the string Use Java (and the key) to decrypt the string Compare original and decrypted string to make sure it is the same Note: Please make it as simple as possible
Write a Python program which implements the following two classical cryptosystem which we covered n class: a) Affine Cipher b) Vigenere Cipher Your program should consist of at least five functions: a) Two functions named encrypt, one for each of the two algorithms which accepts a lowercase alphabetical plaintext string and key as input and outputs a corresponding cipher text string. b) Two functions named decrypt, one for each of the two algorithms which accepts a lowercase alphabetical ciphertext string...
C Programming - RSA Encryption I've tried to write a program that can encrypt and decrypt strings using RSA and want to be able to integrate it into a header file which contains codes for compression, linked list etc.. However, the use of global variables and fixed size of encryption is making it hard to do so Can someone please help me modify the following the code? I want to be able to just pass it in a string to...
use LFSR to encrypt and decrypt some message, and use two or more different structure of LFSR.
Need some help doing and writing this project in python. The purpose of this assignment is to assess your ability to Evaluate properties of modular arithmetic relations Utilize modular arithmetic to encrypt and decrypt plain text Research the Vigenere cipher. Write a program that contains an encode and a decode function. The encode function should take a plaintext message and a key. It should return the encoded message. The decode function should take the encoded message and the key and...
The next two problems use a tabular transposition cipher with keyword "VENUS". 23. Encrypt the message "MATHISSOMUCHFUN" 24. Decrypt the code "AOMRVOMEASRKEDEAHGUB".