从Python文件中获取Caeser密码加密

2024-10-02 00:43:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Caeser密码在shell中与字符串交互工作,但是当我尝试执行单独的程序来加密和解密时,我遇到了一些问题,我不知道输入是否被拆分成一个列表,但加密函数中的if语句被绕过,并默认为填充的else语句名单未加密。有什么建议,谢谢。我在用文件实用程序.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.'

Tags: pyselfifdefelselistencryptedfinal
3条回答
caeser.encrypt(source)

进入

^{pr2}$

source是一个file对象-这个代码“工作”(不加密任何东西)这一事实很有趣-结果是在迭代之前,您在源代码上调用list(),这将它转换为文件中的一个行列表。而不是通常的list(string)的结果,这是一个字符列表。因此,当它试图加密每个chracter时,它会发现一整行与您设置的任何替换项都不匹配。在

同样,正如其他人指出的那样,您忘记在代码中包含plainset,但这并不重要。在

一些关于你的代码的随机注释(可能是吹毛求疵,呵呵)

  • 你把“凯撒”打错了
  • 您使用的习惯用法在python中效率低下(通常称为“非pythonic”),其中一些可能来自于使用其他语言(如C)的经验。
    • 那些while循环可以是for item in string:-字符串已经作为字节列表工作,就像您试图转换的那样。在
    • 写入outfile的行可以是outfile.write(self._encoded)
  • 这两个函数非常相似,几乎是复制粘贴的代码。尝试编写第三个函数,它共享两者的功能,但有两个“模式”,即加密和解密。例如,您可以根据模式,在密码列表或普通列表上运行
  • 我知道您这样做是为了练习,但标准库中包含these functions用于此类替换。包括电池!在

编辑:如果有人想知道这些文件函数做什么以及它们为什么工作,他们会在while循环中调用raw_input(),直到有合适的文件可以返回。openFileWriteRobust()有一个参数,该参数是用户不输入任何内容时的默认值。代码链接在操作站上。在

一些要点:

  • 使用上下文管理器(with)可确保文件在读或写后关闭。在
  • 由于caesar密码是一种替换密码,其中shift参数是密钥,因此不需要单独的encrypt和{}成员函数:它们是相同的,但是“key”是否定的。在
  • writeEncrypted方法只是文件write方法的包装器。因此该类实际上只有两个方法,其中一个是__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编码器。在

对我来说,在调用openFileReadRobust()之后,source中不会有任何内容。我不知道openFileReadRobust()的规范,但它似乎不知道要打开什么文件,除非有一个文件名作为参数给定,而且没有文件名。在

因此,我怀疑source是空的,因此{}也是空的。在

我建议打印出sourceplaintext和{},以确保它们的值是您期望的值。在

顺便说一句,openFileReadRobust()函数对于我来说似乎没有什么帮助,如果它可以为非感官参数值返回非感官值。我非常希望我的函数在这种情况下立即抛出异常。在

相关问题 更多 >

    热门问题