1
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad

key = b'mysecretpassword'  # 16 byte password
cipher = AES.new(key, AES.MODE_CBC)
plaintext = b'my super secret message to encrypt'
ciphertext = cipher.encrypt(pad(plaintext, AES.block_size))

When I print the ciphertext var it displays as follows: b'\x8e\xde\x02\xa9\x8f8=\x8b\x87\x95\x19f\xcc........

Is there some way I can get this to print out in regular text format?

I tried decode() (latin) that converts to "Ioß×Þ³>Û–ª......."

utf-8 generates an error (0xa9 in position 2: invalid start byte)

End of the day ...I just want the ciphertext in a format similar to "I2XlmXarM6GrX1Qq4p8h3EgnA..." to compare with ciphertext examples online with the same key, Iv, block mode, text,etc.

Hope all that makes sense.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
PidePython
  • 61
  • 1
  • 5
  • 2
    The ciphertext is bytes, a lot of them most likely not printable. To compare output, you probably want something like base64 or hexadecimal representation. – Marc Sep 14 '20 at 13:30
  • I converted to hex (hex()) and I think this helps. – PidePython Sep 14 '20 at 20:52

1 Answers1

0

The answer to the question is to use hex().upper() to print out in the same format as online sites.

So, in the example above..I woud put print(ciphertext.hex().upper()) as the end of the file and I would get the same output for my cipher text as other online sites so I can compare my answer.

As Marc pointed out above, I could also use base64 to convert the ciphertext. Thanks all

PidePython
  • 61
  • 1
  • 5