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...
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=[]...
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...
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 <...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT