Cryptography: Classical cryptographic algorithm

    Recently on a cryptography class to see Mu video, the teacher felt quite good, but it, talking about the most is all theoretical knowledge, a bit boring O (∩_∩) O ~ ha ha, it ctf game the cryptography knowledge, if a hand count really solving the case, it seems that efficiency is too low, So, I combined the theory to problem-solving methods to achieve certain kinds of questions using python for a moment. This blog which involves only part of the contents of the back will be updated later on.
    Cryptography video link mentioned above: https://www.icourse163.org/course/UESTC-1003046001
    today we look at the major classical cryptographic algorithm "substitution cipher" and "instead of the password algorithm." First of all have to look at the principles, and then solving the python script.
00
1. Introduction password replacement
01
alternative means to establish a password substitution table, the encrypted plaintext to be encrypted successively look-up table, replacing the corresponding characters, the character is replaced by one plaintext to generate a string of no meaning, i.e., ciphertext the key is to replace it with an alternative password table.
02
03
04
2. replace passwords presentation
05
06
(1) single table instead of the password
07
08
09
income: for this is, Here is the python for a single table substitution cipher:
    either I'm too lazy to do this every time the topic of time in each character into ASCII code, but also a variety of difference then, a variety of computing ...
    ah ah ah O (≧ mouth ≦) O headache, I would not write this script for this yet. Script writing is not perfect place also hope forgive me, I'm white is still a long way to go ah. At that time if there is this type of exercise, then directly out to use on the line, in part because the code length is to better interact with us, if that jumbled, you can streamline your own look, if there Gangster simplified, It can be placed in the comments area, a study ah ^ _ ^.

A single table instead of the password - additive cipher achieved:
(when k = 3, is the standard Caesar Cipher)

#!/usr/bin/python3
#coding:utf-8
#@Author:醉清风

def alphabet_1():
    alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ'  #做关于大写字母的代换表(0,1,2...分别对应A,B,..)
    alphabet_dict={}
    for i in range(0,26):
        alphabet_dict[alphabet[i]]=i
    return alphabet_dict

def alphabet_2():  
    alphabet='abcdefghijklmnopqrstuvwxyz'  #做关于小写字母的代换表(0,1,2...分别对应a,b,..)
    alphabet_dict={}
    for i in range(0,26):
        alphabet_dict[alphabet[i]]=i
    return alphabet_dict 
def run1(y,k):    #根据x=y-k(mod26)得到对应的明文
    m = ''
    alphabet_dict = alphabet_1()
    for i in y:
        x = alphabet_dict[i] - k%26
        if(x<0):
            x = x + 26
        m = m + chr(x+65)
    print("k="+str(k)+":"+m)

def run2(y,k):   #根据x=y-k(mod26)得到对应的明文
    m = ''
    alphabet_dict = alphabet_2()
    for i in y:
        x = alphabet_dict[i] - k%26
        if(x<0):
            x = x + 26
        m = m + chr(x+97)
    print("k="+str(k)+":"+m)
def main():
    y = input("请输入要解密的内容:")
    if(ord(y[0])<97):    #判断输入的是大写还是小写
        for k in range(0,26):
            run1(y,k)
    else:
        for k in range(0,26):
            run2(y,k)

if __name__=="__main__":
    main()

Single table instead of the password - Password multiplication achieved:

#!/usr/bin/python3
#coding:utf-8
#@Author:醉清风

alphabet_dict={}
plain=[]

def alphabet1(k,y):  #做k所有可能下的关于大写字母的字符代换表(明文:A,B,C..分别对应密文:A,J,S..)
    pwd={}
    for i in range(26):  
        alphabet_dict[chr(((k*i))%26+63)] = chr(i+63)
    return alphabet_dict
def alphabet2(k,y):  #做k所有可能下的关于小写字母的字符代换表(明文:a,b,c..分别对应密文:a,j,s..)
    pwd={}
    for i in range(26):  
        alphabet_dict[chr(((k*i))%26+97)] = chr(i+97)
    return alphabet_dict
def run1(y,key):   #从代换表中取回密文对应的明文
    for k in key:
        alphabet_dic = alphabet1(k,y)
        for i in y:
            plain.append(alphabet_dict[i])
    tmp="".join(plain)
    k=0
    m=len(tmp)
    n=int(len(tmp)/12)
    for i in range(0,m,n):
        print ("k="+str(key[k])+":")
        print (tmp[i:i+n])
        k=k+1
def run2(y,key):   #从代换表中取回密文对应的明文
    for k in key:
        alphabet_dic = alphabet2(k,y)
        for i in y:
            plain.append(alphabet_dict[i])
    tmp="".join(plain)
    k=0
    m=len(tmp)
    n=int(len(tmp)/12)
    for i in range(0,m,n):
        print ("k="+str(key[k])+":")
        print (tmp[i:i+n])
        k=k+1

def main():
    y = input("请输入密文:")
    key = [1,3,5,7,9,11,15,17,19,21,23,25] #因为(k,26)=1,故取这些值
    if(ord(y[0])<97):    #判断输入的是大写还是小写
        run1(y,key)
    else:
        run2(y,key)

if __name__=="__main__":
    main()

    Key script mentioned only 12 values, i.e., satisfying (k, 26) = 1, i.e. to meet the "less than 26, and k is coprime, the value of k can be obtained by the following calculation python2.

#!/usr/bin/python2
#coding:utf-8
#@Author:醉清风

def gcd(a,b):    #判断来两个数是否互素,辗转相除法
    if(b==0):
        return a
    else:
        return gcd(b,a%b)

def main():
    y = 26
    for  x in range(0,26):
        if gcd(x,y)==1:    #如果两个数的最大公约数是1,那么两数互素。
            print x,y
if __name__=="__main__":
    main()

Single table substitution cipher - Affine Cipher achieved:

#!/usr/bin/python3
#coding:utf-8
#@Author:醉清风

alphabet_dict={}
plain=[]

def run1(a,b):
    pwd={}
    for i in range(26):  #做关于大写字母的代换表
        alphabet_dict[chr(((a*i+b))%26+63)] = chr(i+63)
    return alphabet_dict
def run2(a,b):
    pwd={}
    for i in range(26):  #做关于小写字母的代换表
        alphabet_dict[chr(((a*i+b))%26+97)] = chr(i+97)
    return alphabet_dict

def main():
    y = input("请输入密文:")
    print("仿射密码格式:y=ax+b")
    a = int(input("请输入a的值:"))
    b = int(input("请输入b的值:"))
    if(ord(y[0])<97):
        alphabet_dict = run1(a,b)
    else:
        alphabet_dict = run2(a,b)
    for i in y:
        plain.append(alphabet_dict[i])
    print("Flag  is : "+"".join(plain))

if __name__=="__main__":
    main()

(2) multi-table substitution cipher
10
11
12
13
14
another day to fix the python achieve this ^ _ ^.

Guess you like

Origin blog.51cto.com/13400543/2413156