Python的文件加密 [英] Python File Encryption

查看:148
本文介绍了Python的文件加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加密以关键短语的文件进行考核的一部分。我做在python矿井已经运行到一个问题。它是使用Python 2.7.4编写的

I need to encrypt a file with a key phrase for part of an assessment. I am doing mine in python and have run into a problem. It is written using python 2.7.4

我的code是如下:

导入阵列

def encrypter(intext, shift, modder):
    plain2 = list(intext)
    plain = array.fromlist(plain2)
    out = ''
    j = 0
    key = list(shift)
    for c in plain:
        if mod > 0:
            x = chr((ord(c) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        if mod < 0:
            x = chr((ord(c) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        out += x
        j += 1
    return out
sel = raw_input("Encrypt (e)/ Decrypt (d)")
if sel == 'e':
    mod = 1
    intext = open(raw_input("what is your file"),'r')
    shift = raw_input("what is your first password")
    encrypter(intext, shift, mod)

else:
    pass

我的问题是,每当我跑这跟一个名为text1.txt我得到这个错误:

My problem is that whenever I run this with a file called text1.txt I get this error:

Traceback (most recent call last):
  File "D:/Programming/Computing GCSE/Tasks/task3.py", line 22, in <module>
    encrypter(intext, shift, mod)
  File "D:/Programming/Computing GCSE/Tasks/task3.py", line 5, in encrypter
    plain = array.fromlist(plain2)
AttributeError: 'module' object has no attribute 'fromlist'

任何人都可以建议我在code的变化?我需要这个比较快因为我的评估是一个小时左右!

Can anyone suggest a change in my code? I need this relatively quickly as my assessment is in an hour or so!

推荐答案

目前还不清楚为什么你需要在阵列模块的。会不会像这样工作?

It's not clear why you need the array module at all. Won't something like this work?

def encrypter(intext, shift, modder):
    plain = intext
    out = ''
    j = 0
    key = shift
    for c in plain:
        if mod > 0:
            x = chr((ord(c) + ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        if mod < 0:
            x = chr((ord(c) - ord(key[(j % (len(plain) - 1)) % len(key)]) - 48) % 58 + 48)
        out += x
        j += 1
    return out

这篇关于Python的文件加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆