def encrypt(msg, password): salt = os.urandom(8) pad_num = 8 - (len(msg) % 8) for i in range(pad_num): msg += chr(pad_num) (dk, iv) = PBEWithMD5AndDES.get_derived_key(password, salt, 1000) crypter = DES.new(dk, DES.MODE_CBC, iv) enc_text = crypter.encrypt(msg) return base64.b64encode(salt + enc_text)
def decrypt(msg, password): msg_bytes = base64.b64decode(msg) salt = msg_bytes[:8] enc_text = msg_bytes[8:] (dk, iv) = PBEWithMD5AndDES.get_derived_key(password, salt, 1000) crypter = DES.new(dk, DES.MODE_CBC, iv) text = crypter.decrypt(enc_text) # remove the padding at the end, if any return re.sub(r'[\x01-\x08]', '', text)
def decrypt(msg, password): msg_bytes = base64.b64decode(msg) salt = msg_bytes[:8] enc_text = msg_bytes[8:] (dk, iv) = PBEWithMD5AndDES.get_derived_key(password, salt, 1000) crypter = DES.new(dk, DES.MODE_CBC, iv) text = crypter.decrypt(enc_text) # remove the padding at the end, if any return re.sub(r'[\x01-\x08]','',text)