zoukankan      html  css  js  c++  java
  • 维吉尼亚密码的Python实现

    代换密码体制的一般定义为M=C=K=Z26,其中M为明文空间、C为密文空间、K为密钥空间、Z26为26个整数(对应26个英文字母)组成的空间;要求26个字母与模26的剩余类集合{0,1,2,…,25}建立一一对应的关系。

    1移位密码

    移位密码的加密实现上就是将26个英文字母向后循环移动k位,其加解密可分别表示为:

    c=Ek(m)=m+k(mod 26)

    m=Dk(c)=c-k(mod 26)

    其中,m、c、k是满足0≤m,c,k≤25的整数。

    2、维吉尼亚密码

    Vigenenre密码是最著名的多表代换密码,是16世纪法国著名密码学家Vigenenre发明的。Vigenenre密码使用一个词组作为密钥,密钥中每一个字母用来确定一个代换表,每一个密钥字母被用来加密一个明文字母,第一个密钥字母加密第一个明文字母,第二个密钥字母加密第二个明文字母,等所有密钥字母使用完后,密钥再次循环使用,于是加解密前需先将明密文按照密钥长度进行分组。密码算法可表示如下:

    设密钥K=(k1,k2,…,kd),明文M=(m1,m2,…,mn),密文C=(c1,c2,…,cn);

    加密变换为:ci=Eki(mi)=mi+ki(mod 26)

    解密变换为:mi=Dki(ci)=ci-ki(mod 26)

    通常通过查询维吉尼亚表进行加解密。

    以下为维吉尼亚密码的Python实现。


     1 #-*-coding:utf-8-*-
     2 #维吉尼亚
     3 
     4 '''
     5 fileName : main.py
     6 '''
     7 
     8 import VigenereEncrypto
     9 import VigenereDecrypto
    10 
    11 print u"维吉尼亚加密"
    12 
    13 plainText = raw_input ("Please input the plainText : ")
    14 key = raw_input ("Please input the key : ")
    15 
    16 plainTextToCipherText = VigenereEncrypto (plainText , key)
    17 print u"加密后得到的暗文是 : " + plainTextToCipherText
    18 
    19 print u"维吉尼亚解密"
    20 
    21 cipherText = raw_input ("Please input the cipherText : ")
    22 key = raw_input ("Please input the key : ")
    23 
    24 cipherTextToPlainText = VigenereDecrypto (cipherText , key)
    25 print u"解密后得到的明文是 : " + cipherTextToPlainText

     1 #-*-coding:utf-8-*-
     2 #维吉尼亚加密
     3 
     4 '''
     5 fileName : VigenereEncrypto.py
     6 '''
     7 
     8 def VigenereEncrypto (input , key) :
     9     ptLen = len(input)
    10     keyLen =  len(key)
    11 
    12     quotient = ptLen // keyLen    #
    13     remainder = ptLen % keyLen    #
    14 
    15     out = ""
    16 
    17     for i in range (0 , quotient) :
    18         for j in range (0 , keyLen) :
    19             c = int((ord(input[i*keyLen+j]) - ord('a') + ord(key[j]) - ord('a')) % 26 + ord('a'))
    20             #global output
    21             out += chr (c)
    22 
    23     for i in range (0 , remainder) :
    24         c =  int((ord(input[quotient*keyLen+i]) - ord('a') + ord(key[i]) - ord('a')) % 26 + ord('a'))
    25         #global output
    26         out += chr (c)
    27 
    28     return out

     1 #-*-coding:utf-8-*-
     2 #维吉尼亚解密
     3 
     4 '''
     5 fileName : VigenereDecrypto.py
     6 '''
     7 
     8 def VigenereDecrypto (output , key) :
     9     ptLen = len (output)
    10     keyLen = len (key)
    11 
    12     quotien = ptLen // keyLen
    13     remainder = ptLen % keyLen
    14 
    15     inp = ""
    16 
    17     for i in range (0 , quotient) :
    18         for j in range (0 , keyLen) :
    19             c = int((ord(output[i*keyLen+j]) - ord('a') + 26 - (ord(key[j]) - ord('a')) % 26 + ord('a')))
    20             #global input
    21             inp += chr (c)
    22 
    23     for i in range (0 , remainder) :
    24         c = int((ord(output[quotient*keyLen + i]) - ord('a') + 26 - (ord(key[i]) - ord('a')) % 26 + ord('a')))
    25         #global input
    26         inp += chr (c)
    27 
    28     return inp

    运行结果:

  • 相关阅读:
    学习笔记8:《大型网站技术架构 核心原理与案例分析》之 固若金汤:网站的安全架构
    学习笔记8:《大型网站技术架构 核心原理与案例分析》之 随需应变:网站的可扩展架构
    13 集合
    12 泛型程序设计
    11 异常, 日志, 断言和调试
    10 部署应用程序和applet
    08 事件处理
    06 接口与内部类
    05 继承
    04 对象与类
  • 原文地址:https://www.cnblogs.com/maoguy/p/6002510.html
Copyright © 2011-2022 走看看