Python Forum
Aes padding - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Aes padding (/thread-10448.html)



Aes padding - Hairy_Ape - May-21-2018

I have a question about padding when encrypting using aes.

Killerrex had previously helped me develop a code for decrypting an AES encrypted string where only only part of the key was know (we were tasked with creating a loop to iterate through possible key values as it was all numeric).

The code decrypts in ECB mode, so there is no IV.

To strip the padding I used:
unpad = lambda s : s [0:-ord (s [-1])]
This worked for an encrypted string with a known key. But for this puzzle, I'm unable to get any readable output.

I was wondering what other padding methods may have been used. Any thoughts?


RE: Aes padding - scidam - May-23-2018

Some time ago I used the following functions for padding/unpadding operations:

def padding(s, bs=16):
    if len(s) % bs == 0:
       return s + ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(bs - 1)) + chr(96 - bs)
    if len(s) % bs > 0 and len(s) > bs:
        res = s + ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(bs - len(s) % bs -1)) + chr(96 + len(s) % bs - bs)
    else:
        res = s + ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(bs - len(s) - 1)) + chr(96 + len(s) - bs)
    return res


def unpadding(s, bs=16):
    return s[:ord(s[-1])-96] if len(s) % bs == 0 else ''