I'm trying out xor encryption for strings in Python but for some reason I'm getting this weird output in terminal. Although the decryption runs just fine, transferring this data seems to be causing an error.
Line 1 is the plaintext string, 2-3 are the encrypted string output, 4 is the encrypted string decrypted, and 5 is the byte string for the encrypted string.
Hello world!
<
U
Hello world!
\x3c\x0\x1f\x18\x1b\x45\x4\x1b\x6\x9\x17\x55
Here's how the encryption is working in Python.
def encrypt(self, plaintext: str) -> str:
ciphertext = ""
for i in range(len(plaintext)):
current = plaintext[i]
current_key = self.key[i%len(self.key)]
ciphertext += chr(ord(current) ^ ord(current_key))
return ciphertext
And to actually call the encryption method is as follows.
tmp2 = tmp2.strip('"')
encryption = Encryption(self.key)
encrypted_string = encryption.encrypt(tmp2)
byte_string = encryption.to_byte_string(
encrypted_string.encode('utf-8'))
What I'm trying to do with the last line of output (byte string), is decrypt this xor string in a C++ program. However the output for that program is just giving me an H.
#define POLY_ENGINE_KEY "test"
static string Decrypt(string ciphertext)
{
string plaintext = ciphertext;
for (int i = 0; i < ciphertext.size(); i++)
plaintext[i] = ciphertext[i] ^ POLY_ENGINE_KEY[i % (sizeof(POLY_ENGINE_KEY) / sizeof(char))];
return plaintext;
}
Greatly appreciate some help with this.