维吉尼亚加密法

一笑奈何 提交于 2020-03-05 01:19:52

维吉尼亚加密法

使用超过一组替代加密,也称为多表替代加密法。
密钥是一系列字母,例如英文单词。
加密方法就像在相同的消息上使用多个凯撒加密法。

算法大意

例如用 "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()

运行结果

运行结果

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!