Skip to content

Instantly share code, notes, and snippets.

@chrix2
Created November 29, 2012 19:40
Show Gist options
  • Save chrix2/4171336 to your computer and use it in GitHub Desktop.
Save chrix2/4171336 to your computer and use it in GitHub Desktop.
Padding PKCS7 on python
import binascii
import StringIO
class PKCS7Encoder(object):
def __init__(self, k=16):
self.k = k
## @param text The padded text for which the padding is to be removed.
# @exception ValueError Raised when the input padding is missing or corrupt.
def decode(self, text):
'''
Remove the PKCS#7 padding from a text string
'''
nl = len(text)
val = int(binascii.hexlify(text[-1]), 16)
if val > self.k:
raise ValueError('Input is not padded or padding is corrupt')
l = nl - val
return text[:l]
## @param text The text to encode.
def encode(self, text):
'''
Pad an input string according to PKCS#7
'''
l = len(text)
output = StringIO.StringIO()
val = self.k - (l % self.k)
for _ in xrange(val):
output.write('%02x' % val)
return text + binascii.unhexlify(output.getvalue())
@vishnuji12
Copy link

Hi ,
I am trying to use PBKDf2 with sha256 for decryption as i am new cryptography so please suggest what need to be changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment