您现在的位置是:网站首页> 编程资料编程资料

python密码学Vignere密码教程_python_

2023-05-26 435人已围观

简介 python密码学Vignere密码教程_python_

Vignere密码

Vignere Cipher包含用于加密和解密的Caesar Cipher算法. Vignere Cipher与Caesar Cipher算法类似,只有一个主要区别:Caesar Cipher包含一个字符移位的算法,而Vignere Cipher包含多个字母移位的键.

数学方程

Vignere密码使用多组替换,因此它也被称为 polyalphabetic cipher . Vignere Cipher将使用字母键而不是数字键表示:字母A将用于键0,字母B将用于键1,依此类推.加密过程之前和之后的字母数字显示在下面 :

基于Vignere密钥长度的可能密钥数量的可能组合如下,给出了Vignere Cipher算法的安全性的结果 :

Vignere Tableau

用于Vignere密码的画面如下所示 :

实现

让我们了解如何实现Vignere密码.考虑文本这是Vignere密码的基本实现将被编码,使用的密钥是 PIZZA.

代码

您可以使用以下代码在Python中实现Vignere密码 :

import pyperclip LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def main():    myMessage = "This is basic implementation of Vignere Cipher"    myKey = 'PIZZA'    myMode = 'encrypt'      if myMode == 'encrypt':       translated = encryptMessage(myKey, myMessage)    elif myMode == 'decrypt':       translated = decryptMessage(myKey, myMessage)      print('%sed message:' % (myMode.title()))    print(translated)    print() def encryptMessage(key, message):    return translateMessage(key, message, 'encrypt') def decryptMessage(key, message):    return translateMessage(key, message, 'decrypt') def translateMessage(key, message, mode):    translated = [] # stores the encrypted/decrypted message string    keyIndex = 0    key = key.upper()    for symbol in message:       num = LETTERS.find(symbol.upper())       if num != -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) if __name__ == '__main__':    main()

输出

当您实现上面给出的代码时,您可以观察到以下输出;

攻击Vignere密码的可能组合几乎是不可能的.因此,它被视为安全加密模式.

以上就是python密码学Vignere密码教程的详细内容,更多关于python密码学Vignere的资料请关注其它相关文章!

-六神源码网