Question

In: Computer Science

Convert this C++ code to Java code this code is to encrypt and decrypt strings of...

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;
}

Solutions

Expert Solution

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);
  
}
  
}


Related Solutions

Can you convert this code (Python) to C++ def decrypt(message, key): decryptedText = "" for i...
Can you convert this code (Python) to C++ def decrypt(message, key): decryptedText = "" for i in message: M = ord(i) k = int(key, 16) xor = M ^ k decryptedText += chr(xor) return decryptedText def encrypt(message, key): encryptedText = "" for i in message: M = ord(i) k = int(key, 16) xor = M ^ k encryptedText += chr(xor) return encryptedText # main function userText = input("Enter text: ") userKey = str(input("Enter a key: ")) encryptedMessage = encrypt(userText, userKey)...
how to write program in java for encrypt and decrypt input text using DH algorithm
how to write program in java for encrypt and decrypt input text using DH algorithm
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions: Objectives Command line input File input and output Rethrowing exceptions Program Description Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should...
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text....
A, B:    Design and Implement a C# windows form application to encrypt and decrypt text. The application use to receive a string and display another encrypted string. The application also decrypt the encrypted string. The approach for encryption/decryption is simple one i.e. to encrypt we will add 1 to each character, so that "hello" would become "ifmmp", and to decrypt we would subtract 1 from each character.    C:   Test and evaluate application by applying different strings.      ...
Write a Java program to encrypt and decrypt a phrase using two similar approaches, each insecure...
Write a Java program to encrypt and decrypt a phrase using two similar approaches, each insecure by modern standards. The first approach is called the Caesar Cipher, and is a simple “substitution cipher” where characters in a message are replaced by a substitute character. The second approach, due to Giovan Battista Bellaso (b 1505, d 1581), uses a key word, where each character in the word specifies the offset for the corresponding character in the message, with the key word...
I need the code in python where I can encrypt and decrypt any plaintext. For example,...
I need the code in python where I can encrypt and decrypt any plaintext. For example, the plaintext "hello" from each of these Block Cipher modes of Operation. Electronic Code Block Mode (ECB) Cipher block Mode (CBC) Cipher Feedback Mode (CFB) Output feedback Mode (OFB) Counter Mode (CTR) Here is an example, Affine cipher expressed in C. Encryption: char cipher(unsigned char block, char key) { return (key+11*block) } Decryption: char invcipher(unsigned char block, char key) { return (163*(block-key+256)) }
Convert the attached C++ code to working Java code. Be judicious in the change that you...
Convert the attached C++ code to working Java code. Be judicious in the change that you make. This assignment is not about re-writing or improving this code, but rather about recognizing the differences between C++ and Java, and making the necessary coding changes to accommodate how Java does things. PLEASE DO NOT use a built-in Java QUEUE (or any other) container. Additional resources for assignment: #include <iostream> #include <string> using namespace std; class pizza { public: string ingrediants, address; pizza...
Please convert This java Code to C# (.cs) Please make sure the code can run and...
Please convert This java Code to C# (.cs) Please make sure the code can run and show the output Thank you! Let me know if you need more information. Intructions For this assignment you will be creating two classes, an interface, and a driver program: Class Calculator will implement the interface CalcOps o As such it will implement hexToDec() - a method to convert from Hexadecimal to Decimal. Class HexCalc will inherit from Calculator. Interface CalcOps will have abstract methods...
I need convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT