我创建了一个程序,使用凯撒密码对文件进行加密,唯一的问题是它不包含大写字母,因为它们不包含在我的列表中。是否可以通过这种方式包含大写字母,或者我需要将所有字符都变为小写?
我当前的代码:
encrypt = str.maketrans('abcdefghijklmnopqrstuvwxyz0123456789', 'ghijklmnopqrstuvwxyz0123456789abcdef')
decrypt = str.maketrans('ghijklmnopqrstuvwxyz0123456789abcdef', 'abcdefghijklmnopqrstuvwxyz0123456789')
filename = "abc_abd.txt"
with open(filename, "r") as readfile:
with open(filename+'-encrypted.txt', 'w+') as writefile:
for line in readfile:
print(line.translate(encrypt), file=writefile)
我希望程序在班次中包含大写字母,但不包括在内。
答案 0 :(得分:0)
>>> import string
>>> charset = ''.join(string.ascii_lowercase + string.ascii_uppercase + ''.join([str(i) for i in range(0, 10)]))
>>> charset
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
>>> delta = 6
>>> encrypt = str.maketrans(charset, charset[delta:] + charset[:delta])
>>> decrypt = str.maketrans(charset[delta:] + charset[:delta], charset)
>>> with open(r'C:/Users/Kevin/Desktop/abc_abd.txt') as f1:
... for line in f1:
... print(f'Original: {line}')
... encrypted_line = line.translate(encrypt)
... print(f'Encrypted: {encrypted_line}')
... decrypted_line = encrypted_line.translate(decrypt)
... print(f'Unencrypted: {decrypted_line}')
...
Original: this is a test
Encrypted: znoy oy g zkyz
Unencrypted: this is a test
Original: number 9 in here
Encrypted: tAshkx f ot nkxk
Unencrypted: number 9 in here
请注意,我假设您从这里开始,要实现包括大写字母的功能。另外,我希望您不要在其中存储任何重要内容,无论如何这都不是真正的加密。我认为这不会安全地存储任何内容。