凱撒密碼腳本

凱撒密碼的加密解密

密碼.jpeg

前言

凱撒密碼作為一種最為古老的對稱加密體制,在古羅馬的時候都已經(jīng)很流行,他的基本思想是:通過把字母移動一定的位數(shù)來實現(xiàn)加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一個固定數(shù)目進行偏移后被替換成密文。例如,當偏移量是3的時候,所有的字母A將被替換成D,B變成E,以此類推X將變成A,Y變成B,Z變成C。由此可見,位數(shù)就是凱撒密碼加密和解密的密鑰。

凱撒密碼加密腳

  • 交單的26次加密腳本
#coding:utf-8

upperDict=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
lowerDict=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
def cesarWithLetter(ciphertext,offset):
    '''
    凱撒密碼 :
        只轉(zhuǎn)換字母(包括大寫小寫)
    參數(shù) : 
        ciphertext : 明文
        offset : 偏移量
    '''
    result = ""
    for ch in ciphertext:
        if ch.isupper():
            result=result+upperDict[((upperDict.index(ch)+offset)%26)]
        elif ch.islower():
            result=result+lowerDict[((lowerDict.index(ch)+offset)%26)]
        elif ch.isdigit():
            result=result+ch
        else:
            result=result+ch
    return result

def printAllResult(ciphertext):
    '''
    打印所有偏移結(jié)果
    '''
    for i in range(len(upperDict)):
        print cesarWithLetter(ciphertext,i)

ciphertext=raw_input("Please input the words :")
printAllResult(ciphertext)

自動控制偏移位自動解密

#-*-coding:utf-8-*-
__author__ = 007
__date__ = 2016 / 02 / 04


#==================================================================#
#         凱撒密碼(caesar)是最早的代換密碼,對稱密碼的一種                #
#   算法:將每個字母用字母表中它之后的第k個字母(稱作位移值)替代            #
#==================================================================#

def encryption():
    str_raw = raw_input("請輸入明文:")
    k = input("請輸入位移值:")
    str_change = str_raw.lower()
    str_list = list(str_change)
    str_list_encry = str_list
    i = 0
    while i < len(str_list):
        if ord(str_list[i]) < 123-k:
            str_list_encry[i] = chr(ord(str_list[i]) + k)
        else:
            str_list_encry[i] = chr(ord(str_list[i]) + k - 26)
        i = i+1
    print "加密結(jié)果為:"+"".join(str_list_encry)

def decryption():
    str_raw = raw_input("請輸入密文:")
    k = input("請輸入位移值:")
    str_change = str_raw.lower()
    str_list = list(str_change)
    str_list_decry = str_list
    i = 0
    while i < len(str_list):
        if ord(str_list[i]) >= 97+k:
            str_list_decry[i] = chr(ord(str_list[i]) - k)
        else:
            str_list_decry[i] = chr(ord(str_list[i]) + 26 - k)
        i = i+1
    print "解密結(jié)果為:"+"".join(str_list_decry)

while True:
    print u"1. 加密"
    print u"2. 解密"
    choice = raw_input("請選擇:")
    if choice == "1":
        encryption()
    elif choice == "2":
        decryption()
    else:
        print u"您的輸入有誤!"

#if __name__ == "__main__":
#    main
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 0x01 目錄 常見編碼: ASCII編碼 Base64/32/16編碼 shellcode編碼 Quoted-p...
    H0f_9閱讀 13,504評論 2 17
  • CTF中那些腦洞大開的編碼和加密 0x00 前言 正文開始之前先閑扯幾句吧,玩CTF的小伙伴也許會遇到類似這樣的問...
    查無此人asdasd閱讀 6,457評論 0 19
  • 珍惜現(xiàn)在的資源和時間,堅持讓編程改變世界 密碼學 信息理論之父:克勞德 香農(nóng)論文《通信的數(shù)學理論》 如果沒有信息加...
    極客圈閱讀 3,450評論 0 2
  • 一、凱撒加密 1. 概述 凱撒密碼作為一種最為古老的對稱加密體制,在古羅馬的時候都已經(jīng)很流行,他的基本思想是:通過...
    撒法第閱讀 1,425評論 0 1
  • 拍拖總有一次爭吵,結(jié)果無數(shù)次爭吵 爭吵總有一次落淚,結(jié)果無數(shù)次落淚 落淚總有一次失望,結(jié)果無數(shù)次失望 失望總有一次...
    你不太熟的關(guān)雎爾閱讀 1,289評論 46 33

友情鏈接更多精彩內(nèi)容