From b2b6708e8f00fd1a70b62b936e88528453ab9e14 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Tue, 7 Jun 2022 17:32:22 +0200 Subject: [PATCH] Added python-only implementation of PKCS7 padding --- RNS/Cryptography/PKCS7.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 RNS/Cryptography/PKCS7.py diff --git a/RNS/Cryptography/PKCS7.py b/RNS/Cryptography/PKCS7.py new file mode 100644 index 0000000..acffa63 --- /dev/null +++ b/RNS/Cryptography/PKCS7.py @@ -0,0 +1,18 @@ +class PKCS7: + BLOCKSIZE = 16 + + @staticmethod + def pad(data, bs=BLOCKSIZE): + l = len(data) + n = bs-l%bs + v = bytes([n]) + return data+v*n + + @staticmethod + def unpad(data, bs=BLOCKSIZE): + l = len(data) + n = data[-1] + if n > bs: + raise ValueError("Cannot unpad, invalid padding length of "+str(n)+" bytes") + else: + return data[:l-n] \ No newline at end of file