密码学编程_凯撒加密法

import pyperclip
message='This is my secret message.'
key=13
mode='encrypt'
LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
translated=''
message=message.upper()
for symbol in message:
    if symbol in LETTERS:
        num=LETTERS.find(symbol)
        if mode=='encrypt':
            num=num+key
        elif mode=='decrypt':
            num=num-key
        if num>=len(LETTERS):
            num=num-len(LETTERS)
        elif mode<0:
            num=num+len(LETTERS)
        translated=translated+LETTERS[num]
    else:
        transted=translated+symbol
print(translated)
pyperclip.copy(translated)
    

以上是加密,现在解密:

注释:mode为encrypt时为加密,为decrypt时则为解密。

import pyperclip
message='GUVFVFZLFRPERGZRFFNTR'
key=13
mode='decrypt'
LETTERS='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
translated=''
message=message.upper()
for symbol in message:
    if symbol in LETTERS:
        num=LETTERS.find(symbol)
        if mode=='encrypt':
            num=num+key
        elif mode=='decrypt':
            num=num-key
        if num>=len(LETTERS):
            num=num-len(LETTERS)
        elif mode<0:
            num=num+len(LETTERS)
        translated=translated+LETTERS[num]
    else:
        transted=translated+symbol
print(translated)
pyperclip.copy(translated)
    

猜你喜欢

转载自blog.csdn.net/qq_41938259/article/details/81704107