• 加密解密算法学习


    背景与原理

    凯撒密码

    在密码学中,凯撒密码(英语:Caesar cipher),或称凯撒加密、凯撒变换、变换加密,是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母A将被替换成D,B变成E,以此类推。这个加密方法是以罗马共和时期凯撒的名字命名的,据称当年凯撒曾用此方法与其将军们进行联系。

    根据偏移量的不同,还存在若干特定的恺撒密码名称:

    1.偏移量为 10:Avocat (A→K)
    2.偏移量为 13:ROT13
    3.偏移量为 -5:Cassis (K 6)
    4.偏移量为 -6:Cassette (K 7)

    例子

    明文:flag{i_LOVE_Reverse}

    key=11

    
    model = "abcdefghijklmnopqrstuvwxyz"
    model2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    str1 = "flag{i_LOVE_Reverse}"
    key = 11
    
    for s in str1:
        if s.islower():
            n = model.find(s)
            s = model[n-key]
        elif s.isupper():
            n = model2.find(s)
            s = model2[n - key]
        print(s, end='')
    print('\n')
    

    得到加密之后的字符串:

    qwlr{t_WZGP_Cpgpcdp}

    解密

    
    model = "abcdefghijklmnopqrstuvwxyz"
    model2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    str1 = "qwlr{t_WZGP_Cpgpcdp}"
    
    for key in range(1,27):
        print("key=%d"%key, end='\n')
        for s in str1:
            if s.islower():
                n = model.find(s)
                s = model[n-key]
            elif s.isupper():
                n = model2.find(s)
                s = model2[n-key]
            print(s, end='')
        print('\n')
    
    key=1
    pvkq{s_VYFO_Bofobco}
    
    key=2
    oujp{r_UXEN_Anenabn}
    
    key=3
    ntio{q_TWDM_Zmdmzam}
    
    key=4
    mshn{p_SVCL_Ylclyzl}
    
    key=5
    lrgm{o_RUBK_Xkbkxyk}
    
    key=6
    kqfl{n_QTAJ_Wjajwxj}
    
    key=7
    jpek{m_PSZI_Vizivwi}
    
    key=8
    iodj{l_ORYH_Uhyhuvh}
    
    key=9
    hnci{k_NQXG_Tgxgtug}
    
    key=10
    gmbh{j_MPWF_Sfwfstf}
    
    key=11
    flag{i_LOVE_Reverse}
    
    key=12
    ekzf{h_KNUD_Qdudqrd}
    
    key=13
    djye{g_JMTC_Pctcpqc}
    
    key=14
    cixd{f_ILSB_Obsbopb}
    
    key=15
    bhwc{e_HKRA_Naranoa}
    
    key=16
    agvb{d_GJQZ_Mzqzmnz}
    
    key=17
    zfua{c_FIPY_Lypylmy}
    
    key=18
    yetz{b_EHOX_Kxoxklx}
    
    key=19
    xdsy{a_DGNW_Jwnwjkw}
    
    key=20
    wcrx{z_CFMV_Ivmvijv}
    
    key=21
    vbqw{y_BELU_Huluhiu}
    
    key=22
    uapv{x_ADKT_Gtktght}
    
    key=23
    tzou{w_ZCJS_Fsjsfgs}
    
    key=24
    synt{v_YBIR_Erirefr}
    
    key=25
    rxms{u_XAHQ_Dqhqdeq}
    
    key=26
    qwlr{t_WZGP_Cpgpcdp}
    
  • 相关阅读:
    Leetcode: Plus One Linked List
    Leetcode: Sort Transformed Array
    Leetcode: Rearrange String k Distance Apart
    Leetcode: Line Reflection
    Leetcode: Logger Rate Limiter
    Leetcode: Design Snake Game
    Leetcode: Android Unlock Patterns
    Leetcode: Longest Substring with At Most K Distinct Characters && Summary: Window做法两种思路总结
    Design Tic-Tac Toe
    Leetcode: Moving Average from Data Stream
  • 原文地址:https://www.cnblogs.com/socialbiao/p/15664509.html
Copyright © 2020-2023  润新知