简单替代加密法

替代加密法

主要思想就是通过将明文替代成另外一个文字来进行加密。优势就是能够有效的抵挡暴力破解。

算法大意

例如:使用密钥 “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

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