Created
November 29, 2012 19:40
-
-
Save chrix2/4171336 to your computer and use it in GitHub Desktop.
Padding PKCS7 on python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi ,
I am trying to use PBKDf2 with sha256 for decryption as i am new cryptography so please suggest what need to be changed.