簡單替代加密法

替代加密法

主要思想就是通過將明文替代成另外一個文字來進行加密。優勢就是能夠有效的抵擋暴力破解。

算法大意

例如:使用密鑰 “VJZBGNFEPLITMXDWKQUCRYAHSO”

要加密的信息是:Attack at dawn.
正常序列:ABCDEFGHIJKLMNOPQRSTUVWSYZ
密鑰序列:VJZBGNFEPLITMXDWKQUCRYAHSO
密文:Vccvzi vc bvax.

代碼實例

import sys, random

# 字典(符號集)
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

# 檢查密鑰是否可用 密鑰和字典轉成列表類型,在進行排序,如果排序結果是一樣則表示密鑰可用
def checkValidKey(key):
    keyList = list(key)
    lettersList = list(LETTERS)
    keyList.sort()
    lettersList.sort()
    if keyList != lettersList:
        sys.exit('There is an error in the key or symbol set.')

# 加密函數
def encryptMessage(key, message):
    return translateMessage(key, message, 'encrypt')

# 解密函數 
def decryptMessage(key, message):
    return translateMessage(key, message, 'decrypt')

# 加密解密算法 
def translateMessage(key, message, mode):
    translated = ''
    charsA = LETTERS
    charsB = key
    # 加密過程和解密過程是一個相反過程,所以解密的時候只要將密鑰和字典對調即可
    if mode == 'decrypt':
        charsA, charsB = charsB, charsA
    # 對信息每個字符進行替換處理 注意大小寫
    for symbol in message:
        if symbol.upper() in charsA:
            symIndex = charsA.find(symbol.upper())
            if symbol.isupper():
                translated += charsB[symIndex].upper()
            else:
                translated += charsB[symIndex].lower()
        # 如果沒有在字典內 就不替代 例如 空格符
        else:
            translated += symbol
    return translated

# 隨機密鑰
def getRandomKey():
    key = list(LETTERS)
    random.shuffle(key)
    return ''.join(key)

def main():
    myMessage = 'If a man is offered a fact which goes against his instincts, he will scrutinize it closely, and unless the evidence is overwhelming, he will refuse to believe it. If, on the other hand, he is offered something which affords a reason for acting in accordance to his instincts, he will accept it even on the slightest evidence. The origin of myths is explained in this way. -Bertrand Russell'
    myKey = getRandomKey()
    checkValidKey(myKey)
    print('-----encrypt-----')
    translated = encryptMessage(myKey, myMessage)
    print('Using key %s' % (myKey))
    print('The encrypted message is:' )
    print(translated)
    print()
    print('-----decrypt-----')
    translated = decryptMessage(myKey, translated)
    print('Using key %s' % (myKey))
    print('The decryptsed message is:' )
    print(translated)
    print()

if __name__ == '__main__':
    main()

運行結果

-----encrypt-----
Using key MKFCOJRYWTSZQINEDHVAGXLUPB sers\shang.vscode\extensions\ms-python.python-2020.2.63990\pythonFiles\ptvsd_launcher.py’ ‘–default’ ‘–clien
The encrypted message is: bCipher.py’
Wj m qmi wv njjohoc m jmfa lywfy rnov mrmwiva ywv wivawifav, yo lwzz vfhgawiwbo wa fznvozp, mic gizovv ayo oxwcoifo wv nxohlyozqwir, yo lwzz hojgvo an kozwoxo wa. Wj, ni ayo nayoh ymic, yo wv njjohoc vnqoaywir lywfy mjjnhcv m homvni jnh mfawir wi mffnhcmifo an ywvzz vfhgawiwbo wa fznvozp, mic gizovv ayo oxwcoifo wv nxohlyozqwir, yo lwzz hojgvo an kozwoxo wa. Wj, ni ayo nay wivawifav, yo lwzz mffoea wa oxoi ni ayo vzwryaova oxwcoifo. Ayo wi mffnhcmifo an ywv wivawifav, yo lwzz mffoea wa oxoi ni ayo vzwryaova oxwcoifo. Ayo nhwrwi nj qpayv wv ouezm
nhwrwi nj qpayv wv ouezmwioc wi aywv lmp. -Kohahmic Hgvvozz

-----decrypt-----
Using key MKFCOJRYWTSZQINEDHVAGXLUPB
The decryptsed message is:
If a man is offered a fact which goes against his instincts, he will scrutinize it closely, and unless the evidence is overwhelming, he will refuse to believe it. If, on the othll scrutinize it closely, and unless the evidence is overwhelming, in accordance to his instincts, he will accept it even on the slightest evidence. The origin of myths is expla he will refuse to believe it. If, on the other hand, he is offered something which affords a reason for acting in accordance to his instincts, he will accept it even on the slightest evidence. The
origin of myths is explained in this way. -Bertrand Russell

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章