我的文本文件中的输出是随机的

2024-03-29 09:13:30 发布

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

我试图编码一些东西,将一个.txt文件,并把它变成一个凯瑟密码加密或解密一个凯瑟密码从一个.txt文件

这是一个修改后的ceaser密码程序,我只翻译了用户输入的字符串

fileName = input('Enter the file name: ')
inputFile = open(fileName, 'r')
text = inputFile.read()

distance = int(input('Enter the distance value: '))

print('Would you like to encrypt or decrypt?')

userinput = input('---] ')
if (userinput == 'encrypt'):
    encvar = ''
    for word in text:
        ordValue = ord(word)
        cipherValue = ordValue + distance
        if cipherValue > ord('z'):
            cipherValue = ord('a') + distance - (ord('z') - ordValue + 
        encvar += chr(cipherValue)
    print(encvar)
if (userinput == 'decrypt'):
    decvar = ''
    for word in text:
        ordValue = ord(word)
        cipherValue = ordValue - distance
        if cipherValue < ord('a'):
            cipherValue = ord('z') - (distance - (ord('a') - ordValue - 
        decvar += chr(cipherValue)
    print(decvar)

我用这样的东西加密

JgnnqVjku"kuCp"gpetarvgf"oguucigVjku"kuCFgoq"hkngHqtRtqlgev"vjtgg

然后将同样的加密粘贴到文件中进行解密,我得到这个输出

cjjmÎÎfgq¸gqÎÎl¸clapwnrcb¸kcqqwecÎÎfgq¸gqÎÎÎÎckm¸dgjcÎÎmpÎÎpmhcar¸rfpccÎÎÎÎ

Tags: 文件texttxt密码inputifworddistance