I've problems in decrypting an html created from the python wrapper, I'm using python 3.8 with pycryptodome on arch linux.
It turned out the issue was due to padding, because I thought pycryptodome fixed the padding issue in pycrypto. For me the problem get resolved with the following change:
diff --git a/python/encrypt.py b/python/encrypt.py
index 700f416..8e03a08 100755
--- a/python/encrypt.py
+++ b/python/encrypt.py
@@ -7,6 +7,7 @@ except:
exit(1)
try:
from Crypto import Random
+ from Crypto.Util import Padding
from Crypto.Cipher import AES
except:
print("install pycrypto: \"pip3 install pycrypto\"")
@@ -14,6 +15,7 @@ except:
import os, sys
from base64 import b64encode
from getpass import getpass
+import codecs
def main():
# sanitize input
@@ -42,14 +44,8 @@ def main():
key = PBKDF2(passphrase=passphrase,salt=salt,iterations=100).read(32)
cipher = AES.new(key, AES.MODE_CBC, IV=iv)
- padded = data.decode("utf-8", "replace")
- # workaround for padding issue https://github.com/dlitz/pycrypto/issues/277
- for i in range(16):
- try:
- encrypted = cipher.encrypt(padded.encode("utf8"))
- break
- except ValueError:
- padded += chr(0)
+ padded = Padding.pad(data,16)
+ encrypted = cipher.encrypt(padded)
projectFolder = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
with open(os.path.join(projectFolder, "decryptTemplate.html")) as f:
@@ -61,7 +57,7 @@ def main():
filename, extension = os.path.splitext(inputfile)
outputfile = filename + "-protected" + extension
- with open(outputfile, 'w') as f:
+ with codecs.open(outputfile, 'w','utf-8-sig') as f:
f.write(encryptedDocument)
print("File saved to %s"%outputfile)