Python shift password, affine transformation decryption example code

Time:2022-5-6
catalogue
  • preface
  • 1、 Shift Cipher
  • 2、 Affine transformation
  • 3、 All codes
  • summary

preface

This code is a long time ago. I have forgotten the specific idea. You can combine this article to understand the contents of the two encryption methods.
The first two steps are the functions corresponding to the encryption method, and the third step is integration.

1、 Shift Cipher

def yiwei(string,number):
    '''
    Move each letter of the string to the right by the specified number of digits, and return the moved string
    For example, if a moves 3 bits to the right, the return value is d
    String is a string and number is an integer
    The ASCII code of a is 65
    '''
    new_string = ""
    string = string. Upper() # convert to uppercase
    For I in string: # shift operation
        #After being converted into ASCII code, it is shifted into new ASCII and then converted back to letters
        new_string += chr((ord(i)-65+number)%26+65) 
    return new_string
##Print (Yiwei ('aaa ', 3)) # test the correctness of the function

def yiwei_decode(c):
    '' shift password decryption and print decryption results' ''
    For I in range (1,27): # exhaustive key search, traversal range [1,26]
        print(yiwei(c,i))
    Print ("decryption end")
##yiwei_ Decode ("a") # test the correctness of the function

2、 Affine transformation

def fangshe_key(m,c):
    '''
    Through the known two pairs of plaintext ciphertext, two keys are obtained by solving
    M is plaintext and C is ciphertext
    [A-Z] converted to number [0-25]
    '''
    #Capitalize
    m = m.upper()
    c = c.upper()
    #Slice
    M1 = m [: 1:] # first letter of plaintext
    M2 = the last letter of M [- 1::] # plaintext
    C1 = C [: 1:] # first letter of ciphertext
    C2 = last letter of C [- 1::] # ciphertext
##    print(m1,m2)
##    print(c1,c2)
    #Convert to integer
    m1 = ord(m1)-65
    m2 = ord(m2)-65
    c1 = ord(c1)-65
    c2 = ord(c2)-65
##    print(m1,m2)
##    print(c1,c2)
    #Exhaustive method
    for i in range(0,26):
        for j in range(0,26):
            If (M1 * I + J)% 26 = = C1 and (M2 * I + J)% 26 = = C2: # when the encryption algorithm is satisfied at the same time
                return i,j
    return False
##Print (fangshe_key ("if", "Ed") # test the correctness of the function

def fangshe_reverse(x,y):
    '''
    Calculate the multiplication inverse, solve the multiplication inverse of x ^ - 1 (% y), and return the corresponding value
    '''
    i = 0
    while True:
        If x * I% 26 = = 1: # meet the condition of multiplicative inverse
            return i
        i += 1
##Print (fangshe_reverse (9,26)) # test the correctness of the function

def fangshe_decode(c,a,b):
    '''
    Affine transformation decryption, and print the decryption results
    C is the ciphertext and a and B are the key
    0 < = a, B < = 25, and GCD (a, 26) = 1, a ^ - 1 represents the inverse element of A
    Encryption formula: C = a * m + B% 26
    Decryption formula: M = (a ^ - 1) * (C-B)% 26
    '''
    new_string = ''
    c = c.upper() #Capitalize转换
    For I in C: # decrypt letter by letter
        new_ I = ord (I) - 65 # converted to number
        new_ I = (fangshe_reverse (a, 26) * (new_i - b))% 26 # decryption
        new_ String + = Chr (new_i + 65) # convert back to uppercase
    print(new_string)
    Print ("decryption end")
##fangshe_ Decode ("Ed", 9,10) # test the correctness of the function

3、 All codes

Shift Cipher

在这里插入图片描述

affine transformation

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# coding=utf-8
#Author: Little Fox FM
#Title: classical password
def menu():
    '''
    Menu interface
    '''
    print("-----------------------")
    Print ("| 0. Exit |")
    Print ("| 1. Shift password decryption |")
    Print ("| 2. Affine transformation decryption |")
    print("-----------------------")
##Menu() # test function

def yiwei(string,number):
    '''
    Move each letter of the string to the right by the specified number of digits, and return the moved string
    For example, if a moves 3 bits to the right, the return value is d
    String is a string and number is an integer
    The ASCII code of a is 65
    '''
    new_string = ""
    string = string. Upper() # convert to uppercase
    For I in string: # shift operation
        #After being converted into ASCII code, it is shifted into new ASCII and then converted back to letters
        new_string += chr((ord(i)-65+number)%26+65) 
    return new_string
##Print (Yiwei ('aaa ', 3)) # test the correctness of the function

def yiwei_decode(c):
    '' shift password decryption and print decryption results' ''
    For I in range (1,27): # exhaustive key search, traversal range [1,26]
        print(yiwei(c,i))
    Print ("decryption end")
##yiwei_ Decode ("a") # test the correctness of the function

def fangshe_key(m,c):
    '''
    Through the known two pairs of plaintext ciphertext, two keys are obtained by solving
    M is plaintext and C is ciphertext
    [A-Z] converted to number [0-25]
    '''
    #Capitalize
    m = m.upper()
    c = c.upper()
    #Slice
    M1 = m [: 1:] # first letter of plaintext
    M2 = the last letter of M [- 1::] # plaintext
    C1 = C [: 1:] # first letter of ciphertext
    C2 = last letter of C [- 1::] # ciphertext
##    print(m1,m2)
##    print(c1,c2)
    #Convert to integer
    m1 = ord(m1)-65
    m2 = ord(m2)-65
    c1 = ord(c1)-65
    c2 = ord(c2)-65
##    print(m1,m2)
##    print(c1,c2)
    #Exhaustive method
    for i in range(0,26):
        for j in range(0,26):
            If (M1 * I + J)% 26 = = C1 and (M2 * I + J)% 26 = = C2: # when the encryption algorithm is satisfied at the same time
                return i,j
    return False
##Print (fangshe_key ("if", "Ed") # test the correctness of the function

def fangshe_reverse(x,y):
    '''
    Calculate the multiplication inverse, solve the multiplication inverse of x ^ - 1 (% y), and return the corresponding value
    '''
    i = 0
    while True:
        If x * I% 26 = = 1: # meet the condition of multiplicative inverse
            return i
        i += 1
##Print (fangshe_reverse (9,26)) # test the correctness of the function

def fangshe_decode(c,a,b):
    '''
    Affine transformation decryption, and print the decryption results
    C is the ciphertext and a and B are the key
    0 < = a, B < = 25, and GCD (a, 26) = 1, a ^ - 1 represents the inverse element of A
    Encryption formula: C = a * m + B% 26
    Decryption formula: M = (a ^ - 1) * (C-B)% 26
    '''
    new_string = ''
    c = c.upper() #Capitalize转换
    For I in C: # decrypt letter by letter
        new_ I = ord (I) - 65 # converted to number
        new_ I = (fangshe_reverse (a, 26) * (new_i - b))% 26 # decryption
        new_ String + = Chr (new_i + 65) # convert back to uppercase
    print(new_string)
    Print ("decryption end")
##fangshe_ Decode ("Ed", 9,10) # test the correctness of the function
    

if __name__=='__main__':
    while True:
        menu()
        Choose = int (input ("please select:")
        if choose==1:   
            String = input ("please enter ciphertext:")
            yiwei_decode(string)
        elif choose==2:            
            String = input ("please enter ciphertext:")
            M = input ("known plaintext:")
            C = input ("corresponding ciphertext:")
            a,b = fangshe_key(m,c)
            fangshe_decode(string,a,b)
        else:
            break

summary

This is the end of this article about Python shift password and affine transformation decryption. For more information about affine transformation of Python shift password, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

SQL server cannot generate fruncm thread

SQL Server was unable to generate the fruncm thread. The database error log is as follows: Copy codeThe code is as follows: 2013-09-26 21:21:50.31 Server      Microsoft SQL Server 2005 – 9.00.1399.06 (Intel X86)Oct 14 2005 00:33:37 Copyright (c) 1988-2005 Microsoft CorporationEnterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2) 2013-09-26 21:21:50.31 Server      (c) […]