-2

Alright, wording this is going to be a bit weird, but I'll do my best. I'm doing some mini experimental projects with PyCrypto and am having some issues with some code that I am writing. This code takes an encryption key and a message to be encrypted as input, then outputs both the key and the encrypted message. However, PyCrypto requires bytecode as the input to its encryption functions. Therefore, when I output the encrypted message, it is in bytecode. Due to the nature of how AES encryption works, the message content contains Hexadecimal in the '\x' or '0x' format. This means that the built-in str.decode() method won't work to convert the message back to Unicode.

I did some investigating and could not find a solution to the problem. I instead tried to go around the problem by inputting the bytecode string into the decryption function through the input() method. However, this causes the original bytecode message, b'\x85\xee9', for example, to be converted into the string "b'\x85\xee9". Obviously, I can't just use the str.encode() method on that, as it's the bytecode string I need, just in the wrong format. Is there a way to convert that Unicode string that contains the bytecode into the original bytecode?

If anyone has a solution to either part of the problem, I'd be grateful to hear it. If any error logs are needed, please let me know.

Thanks for the help!

martineau
  • 119,623
  • 25
  • 170
  • 301
  • https://stackoverflow.com/questions/29897480/possible-to-execute-python-bytecode-from-a-script – cs95 Jul 12 '17 at 21:43
  • Are you using Python 2 or 3? – martineau Jul 12 '17 at 21:46
  • I am using Python 3.6.1 – Trilliante Jul 12 '17 at 21:47
  • What kind of bytecode are we talking here? Do you have a documentation link? It seems highly unusual for any API to take Python bytecode; I suspect you're using the wrong word. – user2357112 Jul 12 '17 at 22:56
  • Why the heck were you trying to convert the AES output to Unicode, anyway? – user2357112 Jul 12 '17 at 23:04
  • As I said, it was an experiment. I was using the library to make a function that would encrypt a message, then give you the encrypted message, the key, and the initial value. Then, at a later time, you could enter the decryption portion program and enter the message, key, and iv to decrypt the message. As I said, it was just an experiment. I know that I didn't do things the most efficient way, and I consider myself a noob at python. However, I work best when I have an objective, and this taught me a lot about both encryption and python, so even though I failed, it was a good experience. – Trilliante Jul 13 '17 at 00:14

1 Answers1

1

Although I generally don't suggest using eval() on any user input, in this case it's probably OK since you're just experimenting.

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> inp = input()
'\x85\xee9'
>>> data = eval(inp.encode('latin1'))
>>> bytes(data.encode('latin1'))
b'\x85\xee9'
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thank you very much. Your answer got me closer to the solution, but did not solve it. I now have an error that's coming from the library itself, not python. As this was just an experiment, I'm not going to pursue it further at this time, I am still very much a novice at python, and feel that I probably should learn a bit more before returning to this project, especially since the library I'm using doesn't have the most "noob friendly" documentation. Thank you for your time, you've shown me some new things to learn about! – Trilliante Jul 13 '17 at 00:11
  • @Trilliante: Happy to hear you felt it helped. Dealing with bytes and strings in Python 3 can be difficult since they're no longer almost the same thing as they were in the previous version. One important take-away shown in my answer is that you can encode arbitrary byte strings into `'latin1'` when necessary, since those can contain any sequence 8-bit values, it preserves the original data. Also note I didn't use the term "byte code" because that means something else entirely in Python. – martineau Jul 13 '17 at 02:44