python实现DES加密解密_des python 代码 16进制

_IP_table=[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
]

循环移位位数表

moveNum=[1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1]

IP置换函数

def IP(message):
result = ‘’
# 用IP表进行置换即可
for i in IP_table:
result += message[i-1]
return result

扩展置换函数

def extend(R0):
result=‘’
# 用扩展置换表进行置换即可
for i in extend_table:
result += R0[i-1]
return result

S盒子压缩函数

def sbox(afterRoundKey):
result = ‘’
for m in range(0,48,6):
# 通过第1位和第6位算出列号
i = int(afterRoundKey[m])*2+int(afterRoundKey[m+5])
# 通过第2、3、4、5位算出行号
j = int(afterRoundKey[m+1])*8+int(afterRoundKey[m+2])*4+int(afterRoundKey[m+3])*2+int(afterRoundKey[m+4])
result += str(‘{:04b}’.format(S[m//6][i][j]))
return result

轮密钥加函数

def addRoundKey(afterExtend,roundKey):
result = ‘’
for i in range(48):
# 进行按位异或运算
result += str(int(afterExtend[i])^int(roundKey[i]))
return result

线性置换P盒

def pbox(afterSbox):
result = ‘’
# 用P盒表进行置换即可
for i in range(32):
result += str(afterSbox[P_table[i]-1])
return result

F函数

def F(R0,key):
# 扩展将32位扩展为48位
R0 = extend(R0)
# 与轮

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值