首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python文件加密

Python文件加密
EN

Stack Overflow用户
提问于 2015-05-07 23:10:00
回答 2查看 275关注 0票数 1

作为评估的一部分,我需要使用关键短语加密文件。我正在用python做我的工作,遇到了一个问题。它是使用python 2.7.4编写的

我的代码如下:

导入数组

代码语言:javascript
复制
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的文件运行此命令时,我都会收到以下错误:

代码语言:javascript
复制
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'

有人能建议对我的代码进行修改吗?我需要这个相对较快,因为我的评估是在一个小时左右!

EN

回答 2

Stack Overflow用户

发布于 2015-05-07 23:12:38

根本不清楚为什么需要array模块。像这样的东西不会起作用吗?

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2015-05-07 23:15:52

从模块导入数组类

代码语言:javascript
复制
from array import array
plain = array('b', plain2)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30105064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档