0

I have a specific AES key that I have to work with which is 10h,10h,10h,"A",10h,10h,10h,"AAA",10h,10h,10h,10h,10h,10h,10h,10h,10h,"A",10h,"A",10h,10h,"AA",10h,"A",10h,"A",10h,"A"

I have to use this key to encrypt a message. In python I covert it to "\x10,\x10,\x10,A,\x10,\x10,\x10,AAA,\x10,\x10,\x10,\x10,\x10,\x10,\x10,\x10,\x10,A,\x10,A,\x10,\x10,AA,\x10,A,\x10,A,\x10,A" but I get ValueError: Incorrect AES key length (60 bytes).

I tried encoding the key in a different way like key = bytearray("\x10,\x10,\x10,A,\x10,\x10,\x10,AAA,\x10,\x10,\x10,\x10,\x10,\x10,\x10,\x10,\x10,A,\x10,A,\x10,\x10,AA,\x10,A,\x10,A,\x10,A".encode('utf-8')) but I still get a same key length error.

What can I do in this case?

This is my code:

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

msg = b"<input><type=\"update\"/></input>"
key = bytearray("\x10,\x10,\x10,A,\x10,\x10,\x10,AAA,\x10,\x10,\x10,\x10,\x10,\x10,\x10,\x10,\x10,A,\x10,A,\x10,\x10,AA,\x10,A,\x10,A,\x10,A".encode('utf-8'))

print(key)

cipher = AES.new(key, AES.MODE_GCM)
encrypted_msg = cipher.encrypt(msg)
print(encrypted_msg)
snow monster
  • 91
  • 1
  • 11
  • What do you want to achieve with this edit? Do you want to use a different encryption key? Add a comment then. – Nineteendo Dec 16 '22 at 19:17

1 Answers1

2

How about using bytes instead?

#pip install pycryptodome
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes

msg = b"<input><type=\"update\"/></input>"
key = bytearray(b"\x10\x10\x10A\x10\x10\x10AAA\x10\x10\x10\x10\x10\x10\x10\x10\x10A\x10A\x10\x10AA\x10A\x10A\x10A")

print(key)

cipher = AES.new(key, AES.MODE_GCM)
encrypted_msg = cipher.encrypt(msg)
print(encrypted_msg)

This seems to be working. Please note that you can also use bytes.fromhex, which is more readable

#pip install pycryptodome
from Crypto.Cipher import AES

msg = b"<input><type=\"update\"/></input>" 
key = bytearray(bytes.fromhex("1010104110101041414110101010101010101041104110104141104110411041"))

print(key)

cipher = AES.new(key, AES.MODE_GCM)
encrypted_msg = cipher.encrypt(msg)
print(encrypted_msg)

Now, the good question: why does this happen? Well, utf-8 encodes the non ascii characters differently, pseudocode:

 '\x80    \x81    \x82    \x83    \x84    \x85    \x86    \x87    \x88    \x89    \x8a    \x8b    \x8c    \x8d    \x8e    \x8f    \x90    \x91    \x92    \x93    \x94    \x95    \x96    \x97    \x98    \x99    \x9a    \x9b    \x9c    \x9d    \x9e    \x9f    \xa0    \xa1    \xa2    \xa3    \xa4    \xa5    \xa6    \xa7    \xa8    \xa9    \xaa    \xab    \xac    \xad    \xae    \xaf    \xb0    \xb1    \xb2    \xb3    \xb4    \xb5    \xb6    \xb7    \xb8    \xb9    \xba    \xbb    \xbc    \xbd    \xbe    \xbf    '.encode()
b'\xc2\x80\xc2\x81\xc2\x82\xc2\x83\xc2\x84\xc2\x85\xc2\x86\xc2\x87\xc2\x88\xc2\x89\xc2\x8a\xc2\x8b\xc2\x8c\xc2\x8d\xc2\x8e\xc2\x8f\xc2\x90\xc2\x91\xc2\x92\xc2\x93\xc2\x94\xc2\x95\xc2\x96\xc2\x97\xc2\x98\xc2\x99\xc2\x9a\xc2\x9b\xc2\x9c\xc2\x9d\xc2\x9e\xc2\x9f\xc2\xa0\xc2\xa1\xc2\xa2\xc2\xa3\xc2\xa4\xc2\xa5\xc2\xa6\xc2\xa7\xc2\xa8\xc2\xa9\xc2\xaa\xc2\xab\xc2\xac\xc2\xad\xc2\xae\xc2\xaf\xc2\xb0\xc2\xb1\xc2\xb2\xc2\xb3\xc2\xb4\xc2\xb5\xc2\xb6\xc2\xb7\xc2\xb8\xc2\xb9\xc2\xba\xc2\xbb\xc2\xbc\xc2\xbd\xc2\xbe\xc2\xbf'
Nineteendo
  • 882
  • 3
  • 18