Ejemplo n.º 1
0
def aes_decrypt(pwd):
    """
    解密当前文件夹下所有 txt 文件
    """
    aes = AESCipher(pwd)
    for txt_file in get_files_list('txt'):
        with open(txt_file, 'r', encoding='utf8') as f:
            contents = [
                aes.decrypt_line(b64decode(line)).decode() for line in f
            ]
        with open(txt_file, 'w', encoding='utf8') as f:
            f.writelines(contents)
        print('{} 解密完成'.format(txt_file))
Ejemplo n.º 2
0
def incremental_aes_decrypt(pwd, mark='*'):
    """
    针对增量加密的全文解密。
    通常情况下没必要频繁解密之前已加密的内容,否则该使用场景并不适宜用增量加密。
    """
    aes = AESCipher(pwd)
    for txt_file in get_files_list('txt'):
        with open(txt_file, 'r', encoding='utf8') as f:
            contents = f.readlines()
        for i, line in enumerate(contents):
            if line.startswith(mark):
                decrypt_contents = [
                    aes.decrypt_line(b64decode(line)).decode()
                    for line in contents[:i]
                ]
                decrypt_contents.extend(contents[i + 1:])
                break
        else:
            continue

        with open(txt_file, 'w', encoding='utf8') as f:
            f.writelines('*' + LINE_BREAK)
            f.writelines(decrypt_contents)
        print('{} 解密完成'.format(txt_file))