Python implements Caesar encryption and decryption

Caesar's cipher refers to the encryption method used by Julius Caesar two thousand years ago and is the simplest and most widely known encryption technique. It is a technique of substitution encryption. All letters in the plaintext are offset backwards (or forwards) by a fixed number on the alphabet and then replaced with ciphertext. Historically, it was common to use an encryption wheel to encrypt plaintext into ciphertext.

Crypto Roulette

introduce

Caesar encryption is a common heap encryption method, and it is also an encryption step of other more complex encryption algorithms. Its encryption and decryption are as follows:

accomplish

As follows, the source code for implementing Caesar encryption in Python is provided:

#caesarCipher.py
import pyperclip

message = "this is secret message"

key =  13  # 加解密key

mode = "encrypt"  # 模式,支持encrypt(加密)、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 num < 0:
            num = num + len(LETTERS);
    else:
        translated =  translated + symbol

print(translated)   #输出加密或解密结果

An implementation is provided here. If you need to use it as a tool, you need to package it into a separate module, and use the value passed in the main function to judge and run to realize the function of encryption or decryption.

Guess you like

Origin blog.csdn.net/chf1142152101/article/details/128249055