In: Computer Science
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<<"\nEnter the string to be encrypted :\t";
gets(input_str); // Getting the user to input the string to be
encrypted!
cout<<"\nOriginal string:\t" << 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] !='\0') // 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]='\0';
cout << "\nEncrypted string is:\t" << 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] !='\0')
{
str = xyz[k];
if(str != ' ') str -= key;
{
if(str < 'A' && str !=' ') str += 26;
{
decrypted_str[k] = str;
k++;
}
}
}
decrypted_str[k]='\0';
cout << "\n Decrypted string is:\t" <<
decrypted_str;
}
package encryptdecrypt;
import java.util.Scanner;
public class EncryptDecrypt {
int MAX_SIZE = 200;
public void encrypt_str(char xyz[], int key)
{
char crypted_str[] = new char[MAX_SIZE];
int k=0;
char str = ' ';
while(xyz[k] != '\0'){
str = Character.toUpperCase(xyz[k]);
System.out.println("str = "+ str);
if(str != ' ')
str += key;
if(str > 'Z')
str -= 26;
crypted_str[k] = str;
k++;
}
crypted_str[k] = '\0';
System.out.println("\n Encrypted string is:\t"+crypted_str);
decrypt_str(crypted_str,key);
}
public void decrypt_str(char xyz[], int key){
char decrypted_str[] = new char[MAX_SIZE];
char str;
int k = 0;
while(xyz[k] != '\0'){
str = xyz[k];
if(str != ' ') str -= key;
if(str < 'A' && str != ' ') str += 26;
{
decrypted_str[k] =str;
k++;
}
decrypted_str[k] = '\0';
System.out.println("\nDecrypted string is:\t"+decrypted_str);
}
}
public static void main(String[] args) {
EncryptDecrypt ed = new EncryptDecrypt();
Scanner sc = new Scanner(System.in);
char input_str[] = new char[200];
int shift = 6;
System.out.println("Enter the string to be enrypted:\t");
String str = sc.nextLine();
System.out.println("Original string:\t"+str);
input_str = str.toCharArray();
ed.encrypt_str(input_str,shift);
}
}