Des算法的简单实现
# -*- coding:utf-8 -*-
#Author: lengqiuqiu
# 字节转换
def dex_to_bit(data, n=0):
output = []
while data != 0:
output.append(data % 2)
data >>= 1
if n > 0 and len(output) < n:
for i in range(len(output), n):
output.append(0)
output.reverse()
return output
# bytes转化为位列表
def bytes_to_list(bdata):
if len(bdata) != 8:
bdata = bdata + int(0).to_bytes(8 - len(bdata), byteorder='big', signed=False)
# bdata = bdata + b'0x0' * (8 - len(bdata))
return dex_to_bit(int.from_bytes(bdata, byteorder='big', signed=False), 64)
# 位列表转化为bytes
def list_to_bytes(ldata):
temp=''
while len(ldata) != 0:
temp=temp + str(ldata.pop(0))
output=int(temp, 2)
return output.to_bytes(8, byteorder='big', signed=False)
# 置换算法
def _substitution(plaintext, ReplacementTable):
ciphertext=[]
for i in range(0, len(ReplacementTable)):
ciphertext.append(plaintext[ReplacementTable[i]-1])
return ciphertext
# 初始置换IP
def IP(plaintext):
# 定义初始置换表
ReplacementTable=[58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 26, 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, 31, 23, 15, 7]
return _substitution(plaintext, ReplacementTable)
# 逆初始置换IP
def re_IP(plaintext):
# 定义逆初始置换表
Re_ReplacementTable=[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]
return _substitution(plaintext, Re_ReplacementTable)
# E-拓展运算
def E(plaintext32):
EBox=[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]
plaintext48=_substitution(plaintext32, EBox)
return plaintext48
# S-盒运算
def S(plaintext):
outtext=[]
s1=[[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]]
s2=[[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]]
s3=[[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]]
s4=[[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]]
s5=[[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]]
s6=[[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]]
s7=[[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]]
s8=[[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]]
SBox=[s1, s2, s3, s4, s5, s6, s7, s8]
for i in range(0, 48, 6):
col=int(str(plaintext[i])+str(plaintext[i+5]), 2)
row=int(str(plaintext[i+1])+str(plaintext[i+2]) +
str(plaintext[i+3])+str(plaintext[i+4]), 2)
temp=dex_to_bit(SBox[i // 6][col][row], 4)
outtext.extend(temp)
return outtext
# P-盒置换
def P(plaintext):
PBox=[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]
return _substitution(plaintext, PBox)
# 置换选择PC-1
def PC_1(keys):
PC_1Box=[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]
return _substitution(keys, PC_1Box)
# 置换选择PC-2
def PC_2(keys):
PC_2Box=[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]
return _substitution(keys, PC_2Box)
#密钥初始化
def KeyInit(keys):
keys56=PC_1(keys)
lkeys28=keys56[0:28]
rkeys28=keys56[28:56]
return [lkeys28, rkeys28]
#轮密钥生成
def GenerateRoundKey(key, RoundNumber):
ROL_TIMES=[1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
#for i in range(0, RoundNumber):
for j in range(0, ROL_TIMES[RoundNumber]):
key[0].append(key[0].pop(0))
key[1].append(key[1].pop(0))
keys56=key[0]+key[1]
keys48=PC_2(keys56)
return keys48
#轮密钥生成
def DeGenerateRoundKey(key, RoundNumber):
ROL_TIMES=[0, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
#for i in range(0, RoundNumber):
for j in range(0, ROL_TIMES[RoundNumber]):
key[0].insert(0,key[0].pop(len(key[0]) -1))
key[1].insert(0,key[1].pop(len(key[1] )-1))
keys56=key[0]+key[1]
keys48=PC_2(keys56)
return keys48
# 异或运算
def XOR(list1, list2):
output=[]
for i in range(0, len(list1)):
output.append(list1[i] ^ list2[i])
return output
#文件输入
def input_to_file(file_name):
with open(file_name, 'rb') as fp:
return fp.read()
#加密
def DES(filename,k):
outname=filename+'.des'
text=input_to_file(filename)
key=k
size = len(text) // 8 if len(text) % 8 == 0 else len(text) // 8 + 1
keys=bytes_to_list(key)
f=open(outname, 'bw')
for i in range(0, size):
temp_text=text[8*i:8*i+8]
plaintext=bytes_to_list(temp_text)
out = _des(plaintext,keys)
bout = list_to_bytes(out)
f.write(bout)
f.close()
#加密
def _des(plaintext, keys):
plaintext64=IP(plaintext)
lplaintext32=plaintext64[0:32]
rplaintext32=plaintext64[32:64]
key=KeyInit(keys)
for i in range(0, 16):
plaintext48=E(rplaintext32)
keys48=GenerateRoundKey(key[:], i)
temp=XOR(plaintext48, keys48)
plaintext32=S(temp)
plaintext32=P(plaintext32)
temp=XOR(plaintext32, lplaintext32)
lplaintext32=rplaintext32
rplaintext32=temp
plaintext64=rplaintext32 + lplaintext32
cryptytext=re_IP(plaintext64)
return cryptytext
#解密,可优化,但是我懒
def DES_Decode(filename,k):
outname=filename+'.out'
text=input_to_file(filename)
key=k
size = len(text) // 8
keys=bytes_to_list(key)
f=open(outname, 'bw')
for i in range(0, size - 1):
temp_text=text[8*i:8*i+8]
plaintext=bytes_to_list(temp_text)
out = _des_decode(plaintext,keys)
bout = list_to_bytes(out)
f.write(bout)
temp_text=text[8*(size - 1):8*(size - 1)+8]
plaintext=bytes_to_list(temp_text)
out = _des_decode(plaintext,keys)
bout = list_to_bytes(out)
f.write(bout)
f.close()
#解密,可优化,但是我懒
def _des_decode(plaintext, keys):
plaintext64=IP(plaintext)
lplaintext32=plaintext64[0:32]
rplaintext32=plaintext64[32:64]
key=KeyInit(keys)
for i in range(0, 16):
plaintext48=E(rplaintext32)
keys48=DeGenerateRoundKey(key[:], i)
temp=XOR(plaintext48, keys48)
plaintext32=S(temp)
plaintext32=P(plaintext32)
temp=XOR(plaintext32, lplaintext32)
lplaintext32=rplaintext32
rplaintext32=temp
plaintext64=rplaintext32 + lplaintext32
cryptytext=re_IP(plaintext64)
return cryptytext
import time
import os
import sys
#简单界面
def Main():
os.system('cls')
print('********************欢迎使用DES加密解密工具********************')
print('************************作者: lqq****************************')
print('请选择功能:')
print('1.加密 2.解密 3.退出')
s = 0
try:
s = int(input())
if s != 1 and s != 2 and s != 3:
raise Exception("")
except:
os.system('cls')
print('请输入正确的数字')
time.sleep(3)
sys.exit(0)
if s == 3:
sys.exit(0)
elif s == 1:
print('请输入要加密的文件路径:')
filename = input()
if filename == '':
print('请输入路径')
sys.exit(1)
print('请输入密钥:')
key = input()
keys = key.encode()
while len(keys) != 8:
print('密钥长度为64bit,请重新输入')
key = input()
keys = key.encode()
print('请选择加密模式:')
print('1.ECB 2.CBC 3.CFB 4.OFB')
try:
ms = int(input())
if ms < 1 and ms > 4:
raise Exception("")
except:
os.system('cls')
print('请输入正确的数字')
time.sleep(3)
sys.exit(0)
if ms == 1:
DES(filename,keys)
elif ms == 2:
pass
elif ms == 3:
pass
elif ms == 4:
pass
if s == 3:
sys.exit(0)
elif s == 2:
print('请输入要解密的文件路径:')
filename = input()
if filename == '':
print('请输入路径')
sys.exit(1)
print('请输入密钥:')
key = input()
keys = key.encode()
while len(keys) != 8:
print('密钥长度为64bit,请重新输入')
key = input()
keys = key.encode()
print('请选择加密模式:')
print('1.ECB 2.CBC 3.CFB 4.OFB')
try:
ms = int(input())
if ms < 1 and ms > 4:
raise Exception("")
except:
os.system('cls')
print('请输入正确的数字')
time.sleep(3)
sys.exit(0)
if ms == 1:
DES_Decode(filename,keys)
elif ms == 2:
pass
elif ms == 3:
pass
elif ms == 4:
pass
Main()
时间有限,能力有限,为了应付作业随便写的代码,可能会存在许多的问题,如果小伙伴们在参考的时候发现存在问题也希望可以反馈一下。