python密码学Vignere密码教程

作者:菜鸟教程 时间:2022-12-06 10:21:22 

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密码的可能组合几乎是不可能的.因此,它被视为安全加密模式.

来源:https://www.it1352.com/OnLineTutorial/cryptography_with_python/cryptography_with_python_implementing_vignere_cipher.html

标签:python,密码学,Vignere
0
投稿

猜你喜欢

  • 在Django中进行用户注册和邮箱验证的方法

    2022-02-01 16:18:35
  • 使用Pytorch搭建模型的步骤

    2022-03-05 21:28:38
  • innerHTML 引发“未知的运行时错误”

    2008-04-09 13:06:00
  • asp内置对象Application详解

    2007-09-19 12:08:00
  • python 计算两个列表的相关系数的实现

    2021-10-07 10:43:57
  • 使用ajax开发的五大误区

    2008-09-03 12:46:00
  • 在Ubuntu系统下安装使用Python的GUI工具wxPython

    2022-08-21 10:17:49
  • python 负数取模运算实例

    2022-06-17 00:50:49
  • pyinstaller封装exe的操作

    2021-02-12 03:21:16
  • python 进阶学习之python装饰器小结

    2023-05-12 07:13:42
  • 详解如何利用Python实现报表自动化

    2021-08-25 19:28:45
  • Python 基于TCP 传输协议的网络通信实现方法

    2023-06-18 22:04:01
  • python去除删除数据中\\u0000\\u0001等unicode字符串的代码

    2023-09-04 09:15:51
  • 轻松掌握python设计模式之策略模式

    2022-01-19 00:17:15
  • 网页效果图设计之色彩索引

    2008-03-23 13:53:00
  • pip如何用pipdeptree查看包依赖

    2022-07-28 01:56:26
  • asp下用OracleInProcServer完成对Oracle的连接和操作

    2008-04-13 07:10:00
  • 用Python判断奇偶数示例

    2021-03-17 22:18:22
  • 基于telepath库实现Python和JavaScript之间交换数据

    2023-05-24 04:20:39
  • python-docx的简单使用示例教程

    2023-10-27 08:32:08
  • asp之家 网络编程 m.aspxhome.com