脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - python密码学Vignere密码教程

python密码学Vignere密码教程

2023-02-16 11:53菜鸟教程 Python

这篇文章主要为大家介绍了python密码学Vignere密码教程,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

Vignere密码

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

数学方程

python密码学Vignere密码教程

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

python密码学Vignere密码教程

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

python密码学Vignere密码教程

Vignere Tableau

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

python密码学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()

输出

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

python密码学Vignere密码教程

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

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

原文地址:https://www.it1352.com/OnLineTutorial/cryptography_with_python/cryptography_with_python_implementing_vignere_cipher.html

延伸 · 阅读

精彩推荐