Question

In: Computer Science

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)
decryptedMessage = decrypt(encryptedMessage, userKey)
print("Encrypted message: ", encryptedMessage)
print("Decrypted message: ", decryptedMessage)

Solutions

Expert Solution

Converted C++ code:

#include <bits/stdc++.h>
using namespace std; 

// function to decrypt the message
string decrypt(string message, string key){
    
    string decryptedText = "";
    int k = 0;
    reverse(key.begin(), key.end());
    for (int i = 0; i < key.size(); i++){
        k += (key[i] - '0') * pow(16, i);
    }
    for (char &i: message){
        int M = (int) i;
        int _xor = M ^ k;
        string s(1, static_cast<char>(_xor));
        decryptedText += s;
    }
    return decryptedText;
}

// function to encrypt the message
string encrypt(string message, string key){
    
    string encryptedText = "";
    int k = 0;
    reverse(key.begin(), key.end());
    for (int i = 0; i < key.size(); i++){
        k += (key[i] - '0') * pow(16, i);
    }
    for (char &i: message){
        int M = (int) i;
        int _xor = M ^ k;
        string s(1, static_cast<char>(_xor));
        encryptedText += s;
    }
    return encryptedText;
}

// Main function
int main(){

    // Take input
    string userText, userKey;
    cout << "Enter text: "; cin >> userText;
    cout << "Enter a key: "; cin >> userKey;

    // Call the functions
    string encryptedMessage = encrypt(userText, userKey);
    string decryptedMessage = decrypt(encryptedMessage, userKey);

    // Print the output
    cout << "Encrypted message: " << encryptedMessage << endl;
    cout << "Decrypted message: " << decryptedMessage << endl;

    return 0;
}

Please refer to the following pictures for the source code:

Sample execution of the above code:


Related Solutions

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];...
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)) }
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):...
Convert this code written in Python to Java: # Definition of a function isprime(). def isprime(num):     count=2;     flag=0;     # Loop to check the divisors of a number.     while(count<num and flag==0):         if(num%count!=0):             # Put flag=0 if the number has no divisor.             flag=0         else:             # Put flag=1 if the number has divisor.             flag=1         # Increment the count.         count=count+1     # Return flag value.     return flag # Intialize list. list=[]...
Implement a Message Authentication Code program in either C/C++ or Python. 1. Accept a message as...
Implement a Message Authentication Code program in either C/C++ or Python. 1. Accept a message as keyboard input to your program. 2. Accept a secret key for the sender/recipient as keyboard input to your program. 3. Your hash function H() is simply the checksum. To compute the checksum, you add all the characters of the string in ASCII codes. For example, the checksum of a string "TAMUC" would be 84 + 65 + 77 + 85 + 67 = 378....
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.
Please create a Python program that can convert a text string to an encrypted message and...
Please create a Python program that can convert a text string to an encrypted message and convert the encrypted message back to the original message. Read a string message from your keyboard. Print the input string. Convert this message into an encrypted message by using a list element substitution. Print your encrypted message. Convert this encrypted message back to the original message by using a list element substitution. Print your original message. Important: please use a dictionary data type and...
PYTHON CODE: def square_matrix_multiplication(matrix1,matrix2): C=[[0 for i in range(len(matrix1))] for i in range(len(matrix2))] for i in...
PYTHON CODE: def square_matrix_multiplication(matrix1,matrix2): C=[[0 for i in range(len(matrix1))] for i in range(len(matrix2))] for i in range(len(matrix1)): for j in range(len(matrix2[0])): C[i][j]=0 for k in range(len(matrix2)): C[i][j] += matrix1[i][k]*matrix2[k][j]    return C I use that function in my code. When I type like "return C", It does not work. If I type print(C), then my code works. Why is it happening? I created two user-entered matrices above the function, then I called the function.
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 + "]" );...
What are the common key and block sizes used by AES? Decrypt the following message. What...
What are the common key and block sizes used by AES? Decrypt the following message. What is the message? What is the cipher? What is the key? Who is the author? ZW ERKLIV YRJ DRUV REP FEV KYZEX CVJJ JLJTVGKZSCV KYRE RCC FKYVIJ FW VOTCLJZMV GIFGVIKP, ZK ZJ KYV RTKZFE FW KYV KYZEBZEX GFNVI TRCCVU RE ZUVR, NYZTY RE ZEUZMZULRC DRP VOTCLJZMVCP GFJJVJJ RJ CFEX RJ YV BVVGJ ZK KF YZDJVCW; SLK KYV DFDVEK ZK ZJ UZMLCXVU, ZK WFITVJ ZKJVCW...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT