Question

Write a java program to encrypt and decrypt a message. The user should be able to...

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Write a java program to encrypt and decrypt a message. The user should be able to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT