我是靠谱客的博主 坚强翅膀,最近开发中收集的这篇文章主要介绍Python实现DES加密Python实现DES加密,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Python实现DES加密

实现功能:输入明文和密钥即可输出密文
所有加密相关的表是直接体现在代码的,也可以改用读取csv等方法导入

实现代码

def plaintext():
    M = input("请输入十六进制数明文:")
    M = list(bin(int(M, 16))[2:].zfill(64))

    first_IP = [58, 50, 12, 34, 26, 18, 10, 2,
                60, 52, 44, 36, 28, 20, 42, 4,
                62, 54, 46, 38, 30, 22, 14, 6,
                64, 56, 48, 40, 32, 24, 16, 8,
                57, 49, 41, 33, 25, 17, 9, 1,
                59, 51, 43, 35, 27, 19, 11, 3,
                61, 53, 45, 37, 29, 21, 13, 5,
                63, 55, 47, 39, 37, 23, 15, 7]
    L0R0 = [0] * 64
    for i in range(64):
        L0R0[i] = M[int(first_IP[i]) - 1]

    return L0R0

def secret():
    K = input("请输入十六进制数密钥:")
    K = list(bin(int(K, 16))[2:].zfill(64))
    #K = list("0001001100110100010101110111100110011011101111001101111111110001")
    PC_1 = [57,49,41,33,25,17,9,
            1,58,50,42,34,26,18,
            10,2,59,51,43,35,27,
            19,11,3,60,52,44,36,
            63,55,47,39,31,23,15,
            7,62,54,46,38,30,22,
            14,6,61,53,45,37,29,
            21,13,5,28,20,12,4]
    new_K = [0] * 56
    for i in range(56):
        new_K [i] = K[int(PC_1[i]) - 1]
    return new_K
def change(L,R,CnDn,offset):
    Cn = CnDn[0:28]
    Dn = CnDn[28:]
    Ln_1 = R
    Cn = Cn[offset:]+Cn[0:offset]
    Dn = Dn[offset:]+Dn[0:offset]
    CnDn = Cn + Dn
    PC_2 = [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]
    Kn = [str(0)] * 48
    R = R[31:32] + R[0:4] + R[4:5] 
         + R[3:4] + R[4:8] + R[8:9] 
         + R[7:8] + R[8:12] + R[12:13] 
         + R[11:12] + R[12:16] + R[16:17] 
         + R[15:16] + R[16:20] + R[20:21] 
         + R[19:20] + R[20:24] + R[24:25] 
         + R[23:24] + R[24:28] + R[28:29] 
         + R[27:28] + R[28:32] + R[0:1]
    En = [str(0)] * 48
    for i in range(48):
        if Kn[i] != R[i]:
            En[i] = str(1)
        else:
            En[i] = str(0)
    print(En)
    Sn = []
    temp = [0]*6
    for i in range(0,48,6):
        temp = En[i] + En[i+1] + En[i+2] + En[i+3] + En[i+4] + En[i+5]
        Sn = Sn + compress(temp,int(i))
    Rn_1 = [str(0)] * 32
    for i in range(32):
        if L[i] != P[i]:
            Rn_1[i] = str(1)
        else:
            Rn_1[i] = str(0)
    return Ln_1,Rn_1,CnDn
L0R0 = plaintext()
Ln = L0R0[0:32]
Rn = L0R0[32:]
new_K = secret()
i = 0
R16L16 = Rn + Ln
last_IP=[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]
result = [str(0)] * 64
for i in range(64):
    result[i] = R16L16[int(last_IP[i]) - 1]


结果

在这里插入图片描述

最后

以上就是坚强翅膀为你收集整理的Python实现DES加密Python实现DES加密的全部内容,希望文章能够帮你解决Python实现DES加密Python实现DES加密所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部