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

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...
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.
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.
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...
message = 'youcannotdecodemyciphertexttoday' def transposition_cipher_encode(plain_text, key): # the input, key should be a permutation of integers...
message = 'youcannotdecodemyciphertexttoday' def transposition_cipher_encode(plain_text, key): # the input, key should be a permutation of integers 0 to some number # your code here return need code in PYTHON
I have the following python code. I get the following message when running it using a...
I have the following python code. I get the following message when running it using a test script which I cannot send here: while textstring[iterator].isspace(): # loop until we get other than space character IndexError: string index out of range. Please find out why and correct the code. def createWords(textstrings): createdWords = [] # empty list for textstring in textstrings: # iterate through each string in trxtstrings iterator = 0 begin = iterator # new begin variable while (iterator <...
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
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...
Q: decrypt the message MPFOAIMSTTAITLEYRO there is no any other hint. That is why I dont...
Q: decrypt the message MPFOAIMSTTAITLEYRO there is no any other hint. That is why I dont know how to decrypt it.. please figure it out with showing your method.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT