看题目名字就能猜测和DES有关系,直接看所给的python代码也能看出和现有的DES遵循一个同样的框架,更确切来说,和现行DES都遵循feistel结构,其中要求轮函数F是自反的,即
这里的就是代码中的round_add
函数,很容易能写出对应的解密函数,跟加密函数完全相反就可以。
def encrypt(self, plaintext):
if (len(plaintext) % 16 != 0 or isinstance(plaintext, bytes) == False):
raise Exception("plaintext must be a multiple of 16 in length")
res = ''
for i in range(len(plaintext) / 16):
block = plaintext[i * 16:(i + 1) * 16]
L = block[:8]
R = block[8:]
for round_cnt in range(32):
L, R = R, (round_add(L, self.Kn[round_cnt]))
L, R = R, L
res += L + R
return res
def decrypt(self, ciphertext):
if (len(ciphertext) % 16 != 0 or isinstance(ciphertext, bytes) == False):
raise Exception("ciphertext must be a multiple of 16 in