zoukankan      html  css  js  c++  java
  • DES加密

    一:DES加密

    1:分析说明

    实验环境:anaconda 4.4  à  python3.6.4

    DES加密分析

    64bit明文
    存于mingwen.xlsx sheet1
    初始置换
    Ip置换表存于mingwen.xlsx  sheet2
    对应实现函数:start_replace.py 文件中的
    def deal_of_IP(file_name,index_num):   #初始置换
    将明文分为左右两部分
    对应实现函数:start_replace.py 文件中的
    def left_and_right(data,flag):     #将明文分为
    右32bit明文扩展为48bit:def expand_right_minwen(RPT):   #扩展置换
    密钥移动:def move_key(list_of_key,round):#秘钥轮循环移动  参数 当前的秘钥表list_of_key 和 第几轮round
    密钥压缩为48bit:def move_and_compression_permutation(move_key_result):  #压缩置换
    Xor运算  :def 
    XOR(key_list,data_of_RPT_expand,list_len):   #异或运算
    
    S盒替换:def S_replace(list_xor):   #S盒替换
    
      P盒置换:def P_box_permutation(s2_replace):
    
    左明文块等于右明文块:
     
    右明文块为 P盒置换结果与左明文块异或的结果:def XOR_and_exchange(p_exchange,left_32_mingwen):
    
    将上述循环16轮
    拼接在一起: 
    
    再最后最后的置换,得到密文:def last_function(source_data):
    件形式提交)
    代码说明:mingwwen.xlsx中有两个表,一个是明文的64 bit表,一个是IP置换表。
    1:DES加密有三个代码文件,分别是main_function.py  mi_key .py  start_replace.py
    main_function.py  主要调用其他两个代码文件的函数,相当于自己写的两个模块
    mi_key .py   处理密钥的代码文件 主要写了 三个函数


    1》 密钥的移动

    2》 密钥的压缩置换

    3》 用于进制见得转换,一自己需要的形式返回



    start_replace.py  里面写了10 个函数



    1》 打开excel 文档

    2》 获取明文表中的固定坐标的明文数据

    3》 根据ip置换表中的值,调用函数2 拿到固定坐标的明文

    4》 明文的左右分块

    5》 右明文的扩展

    6》 Xor运算

    7》 S盒置换函数

    8》 P盒置换函数

    9》 P置换结果与左明文的异或运算,通过调用上面的函数6实现

    10》            最后的置换得到密文

    
    
    
     

     

    2:代码:三个文件

    main_function.py

    #codeing:UTF-8
    #__author__:Administrator
    #date:2018/3/31/031 11:01
    # start_replace.py
    # mi_key.py
    from DES加密算法 import mi_key
    from DES加密算法 import start_replace
    if __name__ == '__main__':
        file_name = input("请输入密文和中间结果的输出文件名>>>")
        file = open(file_name,"w",encoding="utf-8")
        dealed_of_data = start_replace.deal_of_IP("mingwen.xlsx", 1)   #完成初始置换
        # print(dealed_of_data)  #初始置换后你的密文
        RPT = start_replace.left_and_right(dealed_of_data, "right")  # 初始置换后的右半部分
        LPT = start_replace.left_and_right(dealed_of_data,"left")   #初始置换后的左半部分
        # print("初RPT",RPT)
        # print("初LPT",LPT)
        list_of_key = [  # 初始秘钥
            # [0, 0, 0, 0, 0, 1, 1, 1],  # 07
            # [0, 0, 0, 0, 0, 1, 1, 0],  # 06
            # [0, 0, 0, 0, 0, 1, 0, 1],  # 05
            # [0, 0, 0, 0, 0, 1, 0, 0],  # 04
            # [0, 0, 0, 0, 0, 0, 1, 1],  # 03
            # [0, 0, 0, 0, 0, 0, 1, 0],  # 02
            # [0, 0, 0, 0, 0, 0, 0, 1],  # 01
            # [0, 0, 0, 0, 0, 0, 0, 1],  # 00
        ]
        for i in range(0,8):
            msg = "请输入第%d个密钥>>>"%(i+1)
            mi_key_data = input(msg)
            mi_key_data = mi_key.dec2bin(mi_key_data)
            # print(mi_key_data)
            list_of_key.append(mi_key_data)
        print("密钥为:",list_of_key)
        for i in list_of_key:  # 放弃第8 位
          i.pop()
        for i in range(1, 17):
            list_of_key = mi_key.move_key(list_of_key, i)
            key_list = mi_key.move_and_compression_permutation(list_of_key)  # 密钥压缩变换后的48位密钥
            data_of_RPT_expand = start_replace.expand_right_minwen(RPT)  # 明文右边的32扩展为48位的结果
            list_of_xor = start_replace.XOR(key_list, data_of_RPT_expand, len(data_of_RPT_expand))  # XOR异或运算后的密文
            # print(list_of_xor)
            S_replace = start_replace.S_replace(list_of_xor,)  # S盒替换后的密文
            # print(S2_replace)
            P_result = start_replace.P_box_permutation(S_replace)  # P盒置换后的结果
            # print(P_result)
            new_RPT = start_replace.XOR_and_exchange(LPT, P_result)
            new_LPT = RPT
            RPT = new_RPT
            LPT = new_LPT
            file.write("第%d轮的LPT和RPT:
    "%i)
            data_write = str(LPT)
            file.write(data_write)
            file.write("
    ")
            data_write = str(RPT)
            file.write(data_write)
            file.write("
    ")
            # print(data_write)
    
            # print(data_write)
        # print("16轮后的RPT",RPT)
        # print("16轮后的LPT",LPT)
        last_of_source_data = []
        for i in range(0,4):
            last_of_source_data.append(LPT[i*8:i*8+8])
            last_of_source_data.append(RPT[i*8:i*8+8])
        # print(last_of_source_data)
        miwen = start_replace.last_function(last_of_source_data)
        # print("加密后的密文为",miwen)
        data=""
        file.write("加密后的密文为:
    ")
        # data = data.join("加密后的密文为:")
        data = str(miwen)
        file.write(data)
        file.close()

    mi_key.py

    #codeing:UTF-8
    #__author__:Administrator
    #date:2018/3/30/030 23:05
    
    def move_key(list_of_key,round):#秘钥轮循环移动  参数 当前的秘钥表list_of_key 和 第几轮round
        dict_round={   #移动为数参照字典
            1:1,#第1轮
            2:1,#第2轮
            3:2,#第3轮
            4:2,#第4轮
            5:2,#第5轮
            6:2,#第6轮
            7:2,#第7轮
            8:2,#第8轮
            9:1,#第9轮
            10:2,#第10轮
            11:2,#第11轮
            12:2,#第12轮
            13:2,#第13轮
            14:2,#第14轮
            15:2,#第15轮
            16:1,#第16轮
        }
        move_num = dict_round[round]  #取出移动的次数
        # if round == 1:
        #     for i in list_of_key:  # 放弃第8 位
        #         i.pop()
        # print("第%d变换前"%round,list_of_key)
    
    #放弃后的密钥为
        ''' list_of_key
           [
           [0, 0, 0, 0, 0, 1, 1], 
           [0, 0, 0, 0, 0, 1, 1], 
           [0, 0, 0, 0, 0, 1, 0], 
           [0, 0, 0, 0, 0, 1, 0], 
           [0, 0, 0, 0, 0, 0, 1],
           [0, 0, 0, 0, 0, 0, 1],
           [0, 0, 0, 0, 0, 0, 0]
         '''
        # print(move_num)
        temp = 0
        for x in range(0,move_num):  #密钥的移动
            for out_layer in list_of_key:
                for i in range(0, 7):
                    if i == 0:
                        temp = out_layer[i]
                        out_layer[i] = out_layer[i + 1]
                    elif i == 6:
                        out_layer[6] = temp
                    else:
                        out_layer[i] = out_layer[i + 1]
                    # print()
        # print("第%d变换后" % round, list_of_key)
        return list_of_key    #返回移动后的密钥表
        # [  list_of_key
        #     [0, 0, 0, 0, 1, 1, 0],
        #     [0, 0, 0, 0, 1, 1, 0],
        #     [0, 0, 0, 0, 1, 0, 0],
        #     [0, 0, 0, 0, 1, 0, 0],
        #     [0, 0, 0, 0, 0, 1, 0],
        #     [0, 0, 0, 0, 0, 1, 0],
        #     [0, 0, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 0, 0, 0, 0]
        # ]
    def move_and_compression_permutation(move_key_result):  #压缩置换
        # move_key_result = move_key(list_of_key)  #移动后的密钥表
        list_of_compression = [14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41,
                               52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32, ]
        # 压缩置换表
        compression_result = []
        for i in list_of_compression:
            local = i - 1
            row = int(local / 7)
            col = int(local % 7)
            # print(move_key_result[row][col])
            compression_result.append(move_key_result[row][col])
            # print(row,col)print(move_key_result)
        # print(compression_result)  #用于异或的验证
        return compression_result   #返回密钥变换后的密钥
    
    def dec2bin(string_num):
        dec2bin_str = [ "0","0","0","0","0","0","0","0"]
        for i in range(0,len(string_num)):
            deal_data = int(string_num[i])
            for x in range(i*4,i*4+4):
                if deal_data>((2**(3+4*i-x)-1)):
                    dec2bin_str[x] = "1"
                    deal_data = deal_data - 2**(3+4*i-x)
                else:
                    dec2bin_str[x] = "0"
        return dec2bin_str
    
        # print(string_num)
        # for i2 in range(0,6):
        #     if string_num >= 2**(5-i2):
        #         dec2bin_str[i2] = "1"
        #         string_num = string_num - (2**(5-i2))
        # return dec2bin_str

    start_replace.py

    #codeing:UTF-8
    #__author__:Administrator
    #date:2018/3/30/030 8:05
    import xlrd
    def open_excel(file):#打开要解析的Excel文件
        try:
            data = xlrd.open_workbook(file)
            # print(data)
            return data
        except Exception as e:
            print(e)
    def deal_ip(file_name,index_num,row,col):
        data = open_excel(file_name)
        deal_sheet = data.sheet_by_index(index_num)   #获取要读取处理的sheet
        return int(deal_sheet.cell_value(row,col))   #返回读取的数据
    
    def deal_of_IP(file_name,index_num):   #初始置换
        miwen_of_ip = []
        data = open_excel(file_name)
        deal_sheet = data.sheet_by_index(index_num)   #获取初始置换sheet
        all_row_num =int(deal_sheet.nrows)  #计算行数
        all_col_num =int(deal_sheet.ncols)   #计算列数
        for i in range(0,all_row_num):
            for k in range(0,all_col_num):
                deal_bit = int(deal_sheet.cell_value(i,k))-1  #读取sheet中的每一个数据,该sheet存放的为初始置换表
                row = int(deal_bit/16)
                col = int(deal_bit%16)
                # print(row)
                # print(col)
                # print(deal_sheet.cell_value(i, k),end=",")
                ip_of_data = deal_ip("mingwen.xlsx",0,row,col)   #获取该位置的明文数据(bit)
                miwen_of_ip.append(ip_of_data)  #加入到置换后的数据存储表中
            # print("
    ")
        return miwen_of_ip
    # dealed_of_data = deal_of_IP("mingwen.xlsx",1)
    # file = open("ip_miwen.txt", "w",encoding="utf-8")
    # def ip_of_write(data):   #将IP置换结果写到txt
    #     for i in range(0,64):
    #         if (i) % 16 == 0 and i !=0:
    #             file.write("
    ")
    #         file.write(str(data[i]))
    #         # print(dealed_of_data[i],end="")
    # ip_of_write(dealed_of_data)
    # file.close()
    def left_and_right(data,flag):     #将明文分为左右各32bit
        LPT = []
        RPT = []
        for i in range(0,64):
            k = i %16
            if k <8 :
                LPT.append(data[i])
            else:
                RPT.append(data[i])
        if flag == "right":
            return RPT
        if flag == "left":
            return LPT
            # print(LPT)
    # RPT = left_and_right(dealed_of_data,"right")  #右半部分
    # LPT = left_and_right(dealed_of_data,"left")   #左半部分
    # print(RPT)
    #[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0]
    def expand_right_minwen(RPT):   #扩展置换
        expand_result = []
        expand_list = [32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19,
                       20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1]
        for i in expand_list:
            expand_result.append(RPT[i-1])
        # print(expand_result)  #用于异或的验证
        return expand_result
    # expand_result = expand_right_minwen(RPT)  #明文右边的32扩展为48位的结果
    def XOR(key_list,data_of_RPT_expand,list_len):   #异或运算
        # print(list_len)
        list_of_xor = []
        for i in range(0,list_len):
            if key_list[i] == data_of_RPT_expand[i]:
                list_of_xor.append(0)
            else:
                list_of_xor.append(1)
        return list_of_xor
    
    def S_replace(list_xor):   #S盒替换
        list_of_s=[
         [[14, 4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7],
         [0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8],
         [4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0],
         [15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13]],
    
         [[15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10],
         [3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5],
         [0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15],
         [13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9]],
    
         [[10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8],
         [13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1],
         [13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7],
         [1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12]],
    
        [[7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11,  12,  4, 15],
         [13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,9],
         [10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4],
         [3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14]],
    
    
        [[2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9],
         [14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6],
         [4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14],
         [11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3]],
    
        [[12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11],
         [10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8],
         [9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6],
         [4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13]],
    
        [[4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1],
         [13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6],
         [1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2],
         [6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12]],
    
       [[13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7],
         [1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2],
         [7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8],
         [2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11]]
        ]
        group_num = int((len(list_xor)+1)/6)
        # print(group_num)
        list_ofs2_result = []
        for i in range(0,group_num):
            list_of_s2 = list_of_s[i]  #取出的s盒
            # print(list_of_s2)
            deal6_bit = list_xor[i*6:i*6+6]
            row = 0
            col = 0
            for x in range(0,6):
                if x ==0 or x==5:
                    row = row * 2
                    if deal6_bit[x] == 1:
                        row += 1
                else:
                    col = col * 2
                    if deal6_bit[x] == 1:
                        col = col + 1
            # print(row,col)
            # print(list_of_s2[row][col])
            # print(type(format(list_of_s2[row][col],'b')))
            S2_result = format(list_of_s2[row][col],'b')
            for x in range(0,4-len(S2_result)):
                list_ofs2_result.append(0)
            for x in S2_result:
                list_ofs2_result.append(int(x))
            # print(list_ofs2_result)
        # print(list_ofs2_result)  #s盒的结果
        return list_ofs2_result
    
    def P_box_permutation(s2_replace):
        P_list = [16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25]
        P_result = []
        for i in P_list:
            P_result.append(s2_replace[i-1])
        # print(P_result)
        return P_result
    
    def XOR_and_exchange(p_exchange,left_32_mingwen):
        XOR_exchange_result = XOR(p_exchange,left_32_mingwen,len(p_exchange))
        return XOR_exchange_result
    
    def last_function(source_data):
        last_miwen = []
        last_permutation_list = [40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25]
        for i in last_permutation_list:
            deal_bit =i - 1
            row = int(deal_bit / 8)
            col = int(deal_bit % 8)
            # print(row,col)
            # print(source_data[row][col])
            last_miwen.append(source_data[row][col])
        return last_miwen
  • 相关阅读:
    HttpResponse对象、JsonResponse、StreamingHttpResponse、FileResponse
    for循环
    字符串功能
    (二)第一个Spring Boot工程
    (一)安装Docker和Kubernetes
    如何解决CHM打开乱码的问题
    [译]PyUnit—Python单元测试框架(1)
    正则表达式
    Java开发心得
    Shell获取文件后缀名
  • 原文地址:https://www.cnblogs.com/duke77--null/p/8727142.html
Copyright © 2011-2022 走看看