算法实现:
设置两个字符变量,一个用来存储26个字母长度,另一个用来存储你需要加密的信息。
外加一个安全秘钥key,为偏移量,在26个字母中查询偏移。
本例子中信息转换为大写
凯撒加密法(python)
代码实现:

#Caesar Cipher
message='Please enter the English phrase you want to encrypt'
key=10  #You can choose between 1 and 26   Numbers 
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 num<0:
            num=num+len(LETTERS)
        translated=translated+LETTERS[num]
    else:
        translated=translated+symbol
print(translated)

若需要解密则将mode变量替换为‘decrypt’

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
  • 2021-06-27
  • 2021-07-10
猜你喜欢
  • 2022-12-23
  • 2022-01-03
  • 2021-09-14
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案