随笔:简化des加密 python

python 3.6.5  丑陋的代码  简化的des算法  密钥10bit  明文8bit   好久没写python了 语法都快忘光了emmmmmmmm

说好的来波基础逆向呢->->   因为懒 周末还要上党课 烦


随笔:简化des加密 python_第1张图片

import copy
def f_replace(new,old,table):#####置换
    idx=0;
    for i in table:
        new[idx]=old[i-1]
        idx=idx+1
def f_s0(new,old,s):########s0,s1盒子
    row=old[0]+old[3]
    #print("row:",row)
    array=["00","01","10","11"]
    idx=0
    for i in range(len(array)):
        if(row==array[i]):
            idx=i
            break
   # print("idx",idx)
    temp=s[idx]
    a=int(old[1])
    b=int(old[2])
    a=a*2+b
    a=temp[a]
    a=array[a]
    for i in range(len(new)):
        new[i]=a[i]
def f(new,old,len):################异或操作 new=new^old
    for i in range(len):
        a=int(new[i])^int(old[i])
        a=str(a)
        new[i]=a
def f_secret_key(key,p10,p8,id):############生成子密钥 id表示第几次
    new=copy.deepcopy(key)
    f_replace(new,key,p10)
    le=new[:5]
    re=new[5:10]
    for i in range(id):
       # print("i:",i)#########左移i+1
        le=le[i+1:]+le[:i+1]
        re=re[i+1:]+re[:i+1]

    old=le+re
   # print("old",old)
    new=old[:8]
    f_replace(new,old,p8)
    return new
#init data
plain_text="00101101";##str
first_ip=[2,6,3,1,4,8,5,7];
last_ip=[4,1,3,5,7,2,8,6];
ex_replace_table=[4,1,2,3,2,3,4,1];
p10=[3,5,2,7,4,10,1,9,8,6];
p8=[6,3,7,4,8,5,10,9]
p4=[2,4,3,1]
s=[[1,0,3,2],[3,2,1,0],[0,2,1,3],[3,1,3,2]];
s2=[[0,1,2,3],[2,0,1,3],[3,0,1,0],[2,1,0,3]]
secret_key="0110011110"
change3=list(secret_key);
change1=list(plain_text);
#######################  init data have finished
########初始置换ip correct
f_replace(change1,plain_text,first_ip)
########### for循环
le=change1[:4];
re=change1[4:8];
for i in range(2):
    change2=re+re
############扩展置换
    f_replace(change2,re,ex_replace_table)
    key=f_secret_key(change3,p10,p8,1+i)
    f(change2,key,len(change2))
############ s0 s1 box
    s0=change2[:4]
    s1=change2[4:8]
    new1=[0,0]
    f_s0(new1,s0,s)
    new2=[0,0]
    f_s0(new2,s1,s2)
    old=new1+new2
    new=copy.deepcopy(old)
    f_replace(new,old,p4)
    f(new,le,len(le))
    if(i==1):
        le = copy.deepcopy(new)
        break
    le=re[:]
    re=copy.deepcopy(new)
new=le+re
old=new[:]
#print(new)
f_replace(new,old,last_ip)
print(new)

你可能感兴趣的:(加密与解密)