凯撒密码和处理非字母字符

时间:2016-10-28 15:09:31

标签: python caesar-cipher

我在Python 2.7中尝试实现Caesar密码并且遇到逗号,冒号等字符的问题。问题是我希望在文本加密后保持它们不变,但由于某种原因函数&#34 ; encrypt_plaintext"输出没有空格或任何非字母符号的字符数。我试过串联但没有运气;与" .join"相同方法。 我很感激任何建议。

import sys


plain = open('plain.txt')
try:
    plaintext = plain.read()
finally:
    plain.close()


decrypt = open('decrypt.txt')
try:
    decrypttext = decrypt.read()
finally:
    decrypt.close()


crypto = open('crypto.txt')
try:
    cryptotext = crypto.read()
finally:
    crypto.close()


key = open('key.txt')
try:
    keytext = key.read()
finally:
    key.close()


for cezarkey in keytext[0]:
    print cezarkey


def encrypt_plaintext(x):
    crypt = ''
    for i in x:
        if i>='a' and i<='z' or i>='A' and i<='Z':
            crypt += chr(ord(i)+int(cezarkey)%26)
        else:
            i+(crypt)

    print "The decrypted message is: " + crypt
    #crypto = open('crypto.txt', 'w')
    #crypto.write(crypt)
    #crypto.close()

#szyfr = [ chr(ord(c)+k%26) if c>='a' and c<='z' or c>='A' and c<='Z' else c for c in tekst ]
#print ''.join(szyfr)


def decrypt_cyphertext(x):
    crypt = ""
    for i in x:
        crypt += chr((((ord(i) - 97) - int(cezarkey)) % 26) + 97)
    print "The decrypted message is: " + crypt
    decrypt = open('decrypt.txt', 'w')
    decrypt.write(crypt)
    decrypt.close()


if sys.argv[1:3] == ['-c', '-e']:
    encrypt_plaintext(plaintext)

elif sys.argv[1:3] == ['-c','-d']:
    decrypt_cyphertext(cryptotext)

0 个答案:

没有答案