Convert this C++ code to Java code this code is to encrypt and decrypt strings o
ID: 3863709 • Letter: C
Question
Convert this C++ code to Java code
this code is to encrypt and decrypt strings of characters using Caesar cipher
please attach samples run of the code
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string>
#define MAX_SIZE 200
void encrypt_str(char xyz[], int key); // Function prototype for the function to encrypt the input string.
void decrypt_str(char xyz[], int key); // Function prototype for the function to decrypt the encrypted string.
using namespace std;
int main()
{
char input_str[MAX_SIZE];
int shift = 6; // This is the Caesar encryption key... You can change it to whatever value you wish!
system("cls"); // System call to clear the console screen!
cout<<" Enter the string to be encrypted : ";
gets(input_str); // Getting the user to input the string to be encrypted!
cout<<" Original string: " << input_str;
// Function call to encrypt the input string
encrypt_str(input_str, shift);
return 0;
}
// Function Definition for the function to encrypt the input string
void encrypt_str(char xyz[], int key){
char crypted_str[MAX_SIZE]; // To store the resulting string
int k=0; // For indexing purpose
char str;
while(xyz[k] !='') // Processing each character of the string until the "end of line" character is met
{
str = toupper(xyz[k]); // Remove "toupper" from this line if you don't wish to see the
// result string in Uppercase...
if(str != ' ') str += key;
{
if(str > 'z') str -=26;
{
crypted_str[k] = str;
k++;
}
}
}
crypted_str[k]='';
cout << " Encrypted string is: " << crypted_str; // Displaying the Crypted String
// Function call to decrypt the encrypted string
decrypt_str(crypted_str, key);
}
// Function Definition for the function to decrypt the encrypted string.
void decrypt_str(char xyz[], int key){
char decrypted_str[MAX_SIZE];
char str;
int k=0;
while(xyz[k] !='')
{
str = xyz[k];
if(str != ' ') str -= key;
{
if(str < 'A' && str !=' ') str += 26;
{
decrypted_str[k] = str;
k++;
}
}
}
decrypted_str[k]='';
cout << " Decrypted string is: " << decrypted_str;
}
Explanation / Answer
import javax.swing.JOptionPane; public class CaesarCipher { public static void main(String[] args) { //gets a string to encrypt String str = (JOptionPane.showInputDialog("Input Data to encypt:")); //gets a key String key = (JOptionPane.showInputDialog("Input the key:")); int keyLength=key.length(); //prints encryption String encrypted = encrypt(str, keyLength); System.out.println("Encrypted:" + encrypted); //prints decryption String decrypted = decrypt(encrypted, keyLength); System.out.println("Decrypted:" + decrypted); //prints key System.out.println("Key:" + key); } public static String encrypt(String str, int keyLength) { String encrypted = ""; for(int i = 0; i 'Z') c = c - 26; } else if (Character.isLowerCase(c)) { c = c + (keyLength % 26); if (c > 'z') c = c - 26; } encrypted += (char) c; } return encrypted; } public static String decrypt(String str, int keyLength) 58 { 59 String decrypted = ""; 60 for(int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.