我的Caeser密码在shell中以字符串交互式工作,但是当我尝试进行单独的程序加密和解密时,我遇到了问题,我不知道输入是否被分成了列表与否,但我的加密函数中的if语句被绕过并默认为填充未加密列表的else语句。任何建议赞赏。我正在使用Goldwasser书中的FileUtilities.py。该文件在第11章中为http://prenhall.com/goldwasser/sourcecode.zip,但我认为问题不在于此,但谁知道。提前谢谢。
#CaeserCipher.py
class CaeserCipher:
def __init__ (self, unencrypted="", encrypted=""):
self._plain = unencrypted
self._cipher = encrypted
self._encoded = ""
def encrypt (self, plaintext):
self._plain = plaintext
plain_list = list(self._plain)
i = 0
final = []
while (i <= len(plain_list)-1):
if plain_list[i] in plainset:
final.append(plainset[plain_list[i]])
else:
final.append(plain_list[i])
i+=1
self._encoded = ''.join(final)
return self._encoded
def decrypt (self, ciphertext):
self._cipher = ciphertext
cipher_list = list(self._cipher)
i = 0
final = []
while (i <= len(cipher_list)-1):
if cipher_list[i] in cipherset:
final.append(cipherset[cipher_list[i]])
else:
final.append(cipher_list[i])
i+=1
self._encoded = ''.join(final)
return self._encoded
def writeEncrypted(self, outfile):
encoded_file = self._encoded
outfile.write('%s' %(encoded_file))
#encrypt.py
from FileUtilities import openFileReadRobust, openFileWriteRobust
from CaeserCipher import CaeserCipher
caeser = CaeserCipher()
source = openFileReadRobust()
destination = openFileWriteRobust('encrypted.txt')
caeser.encrypt(source)
caeser.writeEncrypted(destination)
source.close()
destination.close()
print 'Encryption completed.'
答案 0 :(得分:3)
caeser.encrypt(source)
到
caeser.encrypt(source.read())
source
是一个文件对象 - 这个代码“工作”(通过不加密任何东西)的事实很有意思 - 事实证明你在迭代之前在源上调用list()
并将其转换为文件中的行列表。而不是通常的list(string)
结果,这是一个字符列表。因此,当它尝试加密每个字符时,它会找到与您设置的任何替换都不匹配的整行。
也像其他人指出的那样,你忘了在代码中包含plainset,但这并不重要。
关于你的代码的一些随机记录(可能是你没有要求的挑剔,嘿)
for item in string:
- 字符串已经像你试图转换的字节列表一样工作。outfile.write(self._encoded)
编辑:如果有人想知道这些文件函数的功能及其工作原理,他们会在while循环中调用raw_input()
,直到有合适的文件返回。 openFileWriteRobust()
有一个参数,如果用户没有输入任何内容,则该参数是默认值。代码链接在OP帖子上。
答案 1 :(得分:1)
有些观点:
with
)确保文件在读取或写入后关闭。encrypt
和decrypt
成员函数:它们是相同的但是具有“密钥”否定。writeEncrypted
方法只是文件写入方法的包装器。因此,该类实际上只有两种方法,其中一种方法是__init__
。考虑到这一点,您的代码可以替换为此;
import string
def caesartable(txt, shift):
shift = int(shift)
if shift > 25 or shift < -25:
raise ValueError('illegal shift value')
az = string.ascii_lowercase
AZ = string.ascii_uppercase
eaz = az[-shift:]+az[:-shift]
eAZ = AZ[-shift:]+AZ[:-shift]
tt = string.maketrans(az + AZ, eaz + eAZ)
return tt
enc = caesartable(3) # for example. decrypt would be caesartable(-3)
with open('plain.txt') as inf:
txt = inf.read()
with open('encrypted.txt', 'w+') as outf:
outf.write(txt.translate(enc))
如果您使用的是13的班次,则可以使用内置的rot13
编码器。
答案 2 :(得分:0)
对我来说,在source
致电后openFileReadRobust()
中会有任何内容,这一点并不明显。我不知道openFileReadRobust()
的规范,但似乎它不知道要打开哪个文件,除非有作为参数给出的文件名,并且没有一个。
因此,我怀疑source
为空,因此plain
也是空的。
我建议打印出source
,plaintext
和plain
,以确保他们的价值符合您的预期。
顺便说一句,如果openFileReadRobust()
函数可以返回非感知参数值的非感知值,那么{{1}}函数似乎对我没有帮助。我非常喜欢我的函数在这种情况下立即抛出异常。