アルゴリズム:シーザー暗号問題:アルファベットを一定のビット数移動することで暗号化と復号化を実現する.


シーザーパスワードの問題:アルファベットを一定のビット数移動することで暗号化と復号化を実現します.
例えば(移動3コマ):a-D、b-E、c-F、d-G、e-H…s-V…、z-C明文:access control可変:DFHVV FRQWURO
import random
def Letter_num(Letter):  # a-z   0-25
    return ord(Letter) - 97


def num_Letter(num):  # 0-25   a-z
    return chr(num + 97)


def encryption_Letter(P, K):  #       
    C = num_Letter((Letter_num(P) + K) % 26)
    return C


def Decrypt_Letter(C, K):  #       
    P = num_Letter((Letter_num(C) - K) % 26)
    return P


def encryption_fun(P_char, K):  #      
    C_char = ''
    for P in P_char:
        if P.isalpha():
            P = encryption_Letter(P, K)
        C_char = C_char + P
    return C_char


def Decrypt_fun(C_char, K):  #      
    P_char = ''
    for C in C_char:
        if C.isalpha():
            C = Decrypt_Letter(C, K)
        P_char = P_char + C
    return P_char


if __name__ == '__main__':
    K = int(random.random()*26%26)
    P_text = "let's meet after the party.if you understand this sentence, the program runs successfully.abcd efghjgkl mnopqr stu vwxy z"
    print('   :',P_text)
    print('K:',K)
    C_text = encryption_fun(P_text, K)
    print('   :', C_text)
    PP_text = Decrypt_fun(C_text, K)
    print('    :', PP_text)


結果
   : let's meet after the party.if you understand this sentence, the program runs successfully.abcd efghjgkl mnopqr stu vwxy z
K: 14
   : zsh'g assh othsf hvs dofhm.wt mci ibrsfghobr hvwg gsbhsbqs, hvs dfcufoa fibg giqqsggtizzm.opqr stuvxuyz abcdef ghi jklm n
    : let's meet after the party.if you understand this sentence, the program runs successfully.abcd efghjgkl mnopqr stu vwxy z