![]() |
|
[Python] Rijndael Padding in Python - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [Python] Rijndael Padding in Python (/showthread.php?tid=5976582) |
Rijndael Padding in Python - 000 - Jun 4, 2019 I know where the problem is but I don't know how to fix it. The problem is with the padding. I have absolutely no idea about it and how it works. I tried searching online but nothing seemed to help. Here's the actual code of my python: Code from rijndael.cipher.crypt import newThis is the output: Quote:Dv0Y/AFXdFMlDrcldFCu8v5o9zAlLNgyM+vO+PFeSrqWdzP1S1cumviFiEjNAjz5njnMMC9lfxsBl71x5y+xCw== I need the output of the encrypted string to be something like this: Quote:Dv0Y/AFXdFMlDrcldFCu8v5o9zAlLNgyM+vO+PFeSrpO8Ve82mdUcc4rkzp9afDYc75NmkSd4mdflt38kceOdA== Can anyone please help? I am lost at this point. :/ RE: Rijndael Padding in Python - HFDone - Jun 4, 2019 https://github.com/moeenz/rijndael you have usage there you dont need the r_pad function the blocksize is the length KEY = 'lkirwf897+22#bbtrm8814z5qq=498j5' IV = '741952hheeyy66#cs!9hjv887mxx7@8y' plain_text = "A padded string to BLOCKSIZE length." rjn = new(KEY, MODE_CBC, IV, blocksize=32) encd = rjn.encrypt(plain_text) data = base64.b64encode(encd) print(data) rjn = new(KEY, MODE_CBC, IV, blocksize=32) data = base64.b64decode(data) decd = rjn.decrypt(data) print(decd) RE: Rijndael Padding in Python - 000 - Jun 4, 2019 (Jun 4, 2019 11:06 AM)HFDone Wrote: https://github.com/moeenz/rijndael I would need the string padded if the length of the string exceeds 32 characters, wouldn't I? Here's what I came up with: Possible Solution 1: Split the long string into multiple strings: Code plain_text = b'A padded string to BLOCKSIZE length.'But that doesn't seem to work. I am getting this output: Quote:Encrypted: * Another problem with this method would be if the string is something like "AStringWhichIsMoreThan32CharactersLongWithoutSpaces". It would be difficult to split it since I won't know the input. Possible Solution 2: Maybe I should be converting the strings into bytes? Since some characters take more than 1 byte to represent? But this doesn't seem to be working as well: Code plain_text = b'A padded string to BLOCKSIZE length.'I don't think that's an actual way to convert string into bytes. But then again, I don't know how to. I have searched for it on Google but nothing seemed to help. Even tried .encode(). RE: Rijndael Padding in Python - HFDone - Jun 4, 2019 i didnt get at first here how you do it its not the same lib but you can get the idea pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING http://www.codekoala.com/posts/aes-encryption-python-using-pycrypto RE: Rijndael Padding in Python - 000 - Jun 4, 2019 (Jun 4, 2019 11:52 AM)HFDone Wrote: Snipped Thank you so much but I figured I was making a simple mistake. Here's what fixed my issue: Code PADDING = b'\x00'It's not the proper way to do it, I know but at least it works. A better approach would be: Code def pad(payload, block_size=32):RE: Rijndael Padding in Python - HFDone - Jun 4, 2019 dont use replace for unpadding as you can have the padding in your encrypt data if you dont use base64 or if you are using base64 char for padding use rstrip as in the link so you dont have to deal with this problem RE: Rijndael Padding in Python - 000 - Jun 4, 2019 (Jun 4, 2019 12:21 PM)HFDone Wrote: dont use replace for unpadding as you can have the padding in your encrypt data if you dont use base64 or if you are using base64 char for padding You're right. Thank you again. Have a nice day! :) |