我的Caeser密碼在shell中用字符串交互工作,但是當我嘗試單獨執行加密和解密程序時,我遇到了問題,我不知道輸入不會被拆分成一個列表,但是我的加密函數中的if語句被繞過,並且默認爲未加密列表的else語句。任何建議感激。我使用Goldwasser書中的FileUtilities.py。該文件在第11章中的http://prenhall.com/goldwasser/sourcecode.zip處,但我認爲問題並非如此,但是誰知道。提前謝謝。Caeser從Python中的文件加密
#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.'
什麼是'plainset'(在你的'while'條件下)? – 2013-04-07 20:49:08