Python implementation of Caesar's password (8 lines of code)

In cryptography, Caesar cipher (English: Caesar cipher), or Caesar encryption, Caesar transformation, transform encryption, is the simplest and most widely known encryption technology.  It is a replacement encryption technology. All letters in the plaintext are shifted backward (or forward) in the alphabet by a fixed number and then replaced with ciphertext.  For example, when the offset is 3, all letters A will be replaced with D, B will become E, and so on.  This encryption method is named after Caesar during the Roman Republic, when Caesar used this method to communicate with his generals.

Directly upload the code (light version)

#字母对应数字
dic = {
    
    "a":0,"b":1,"c":2,"d":3,"e":4,"f":5,"g":6,"h":7,"i":8,"j":9,"k":10,"l":11,"m":12,"n":13,"o":14,"p":15,"q":16,"r":17,"s":18,"t":19,"u":20,"v":21,"w":22,"x":23,"y":24,"z":25}
#打印时使用该字符串
cha = "abcdefghijklmnopqrstuvwxyz"
list1 = list(input("\n请输入一串字母:"))
i = eval(input("请输入偏移量(0-26):"))
if i >=0 and i <=25:#偏移并直接输出
    print("偏移量%d:" % (i),end=" ")
    for j in list1:
        #python三目运算符(条件为真时的结果 if 判段的条件 else 条件为假时的结果)
        print(cha[dic.get(j)+i - 26 if (dic.get(j)+i > 25) else dic.get(j)+i],end="")

operation result
Caesar's code running results

Guess you like

Origin blog.csdn.net/weixin_44864260/article/details/109269098