維吉尼亞加密法

維吉尼亞加密法

使用超過一組替代加密,也稱爲多表替代加密法。
密鑰是一系列字母,例如英文單詞。
加密方法就像在相同的消息上使用多個凱撒加密法。

算法大意

例如用 "PIZZA"作爲密鑰
第一個子密鑰是P 二個子密鑰是I 三是Z 四是Z 五是A
用第1個來加密第1個明文字母,第2個加密第2個明文字母,以此類推
到了第6個明文字母時回過頭來用第1個子密鑰加密。
例如:
加密消息爲 Common sense is not so common
COMMONSENSEISNOTSOCOMMON
PIZZAPIZZAPIZZAPIZZAPIZZ
C是數字2 P是數字15 所以加密出來是17®
密文:
Rwlloc admst qr moi an bobunm
密鑰字母越多,加密後越能抵擋暴力破解攻擊.
所以密碼可以是任何字母組合.

代碼實例

LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'


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


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


def translateMessage(key, message, mode):
    translated = [] # 處理後的字符空間

    keyIndex = 0
    key = key.upper()

    for symbol in message: 
        num = LETTERS.find(symbol.upper())
        if num != -1: # 如果字符沒有大寫則是-1
            # 利用凱撒加密算法進行加密
            if mode == 'encrypt':
                num += LETTERS.find(key[keyIndex]) 
            elif mode == 'decrypt':
                num -= LETTERS.find(key[keyIndex])

            num %= len(LETTERS) 

            if symbol.isupper():
                translated.append(LETTERS[num])
            elif symbol.islower():
                translated.append(LETTERS[num].lower())
            # 對密鑰進行循環使用
            keyIndex += 1 
            if keyIndex == len(key):
                keyIndex = 0
        else:
            translated.append(symbol)
    return ''.join(translated)

def main():
    myMessage = """Alan Mathison Turing was a British mathematician, logician, cryptanalyst, and computer scientist. He was highly influential in the development of computer science, providing a formalisation of the concepts of "algorithm" and "computation" with the Turing machine. Turing is widely considered to be the father of computer science and artificial intelligence. During World War II, Turing worked for the Government Code and Cypher School (GCCS) at Bletchley Park, Britain's codebreaking centre. For a time he was head of Hut 8, the section responsible for German naval cryptanalysis. He devised a number of techniques for breaking German ciphers, including the method of the bombe, an electromechanical machine that could find settings for the Enigma machine. After the war he worked at the National Physical Laboratory, where he created one of the first designs for a stored-program computer, the ACE. In 1948 Turing joined Max Newman's Computing Laboratory at Manchester University, where he assisted in the development of the Manchester computers and became interested in mathematical biology. He wrote a paper on the chemical basis of morphogenesis, and predicted oscillating chemical reactions such as the Belousov-Zhabotinsky reaction, which were first observed in the 1960s. Turing's homosexuality resulted in a criminal prosecution in 1952, when homosexual acts were still illegal in the United Kingdom. He accepted treatment with female hormones (chemical castration) as an alternative to prison. Turing died in 1954, just over two weeks before his 42nd birthday, from cyanide poisoning. An inquest determined that his death was suicide; his mother and some others believed his death was accidental. On 10 September 2009, following an Internet campaign, British Prime Minister Gordon Brown made an official public apology on behalf of the British government for "the appalling way he was treated." As of May 2012 a private member's bill was before the House of Lords which would grant Turing a statutory pardon if enacted."""
    myKey = 'ASIMOV'
    translated = encryptMessage(myKey, myMessage)
    print('encrypted message:')
    print(translated)
    translated = decryptMessage(myKey, translated)
    print('decrypted message:')
    print(translated)



if __name__ == '__main__':
    main()

運行結果

運行結果

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