用AES-256-cbc对字符串进行递归解密



我正在尝试解密AES CBC字符串的解密次数。我第一次解密成功了。

from Crypto.Cipher import AES
k = '57067125438768260656188878670043'
key = bytes(k, 'ascii')
i = '5706712543876826'
iv = bytes(i, 'ascii') 
dec = AES.new(key, AES.MODE_CBC, iv)
n = 2
cipher = 'dd3364461dbca39ddb5eb32e9f11b81f000acac9ce8b91369f8bf7e4a88787785a8cc498c85ea20370e68f0e7014e92a2b5aedd4c670ec172d7adb45dfa5a770b582e8ed255bb857d94afdfd6e579525f24890070f984b8862133eda9cbb118ba7880db125c32dea7e7c54bc77abfc02'

def unpad(s):
return s[:-ord(s[len(s)-1:])]

def decrypt(cipherId):
cipher = bytes.fromhex(cipherId)
id = dec.decrypt(cipher)
node_dec = unpad(id.decode('utf-8'))
print (node_dec)
return node_dec

这是加密的第一阶段,但我不知道如何设置一个循环来再次运行基于n的函数。例如,这个密码被加密了两次,可能是三次、四次或更多次。

我创建了另一个函数来解密解密函数的输出,因为输出给出了不同的编解码器。

def decrypt_again(cipherId):
cipher = bytes.fromhex(cipherId)
id =   dec.decrypt(cipher)
node_dec = unpad(id.decode('ISO-8859-1'))
print (node_dec)
return node_dec

所以我试着写这个循环

x = decrypt(cipher)
for i in range (n):
y = decrypt_again(x)

但它只适用于n=1,如果n大于1,它只是不断重复y,而不是再次解析它。

请问我该怎么办?

同样,如果我重新运行该函数两次,字符串发生了一些事情,我得到的是ó³*Ä'»g)X»#¾ú84-8089-be57330fcd45而不是这个a214868d-f40b-4184-8089-be57330fcd45,那么编解码器中似乎出现了中断

为什么会这样?

每次将y设置为decrypt_again(x)时,x仍然相同,因此您应该添加一行将x设置为y,以便它记住上一次迭代的结果

x = decrypt(cipher)
for i in range (n):
y = decrypt_again(x)
x = y

所以伙计们,我能够解决这个问题。这是最有效的正确代码。

from Crypto.Cipher import AES
k = '57067125438768260656188878670043'
key = bytes(k, 'ascii')
i = '5706712543876826'
iv = bytes(i, 'ascii') 
n = 4
cipher = 'afc6f435d3bb68234bba2d39840d4c61cd0d45810940cf796ebf4078e1c0b2368caf618dd6c915fa3f9e0245372cc0dd9992d71aa3ac9e811a579a1d39fa9e26b0d1728d290f4bfa54931917afb45c49a815937498923e241a3eded50dff67ff18d9c1259404f1bdbafef7df5aba0c0b2aacc18079b6bb64058a7976246c0231b2178312c9a008dfa3bd954eef12c73d0b3adac8aa2f4733fe09767dc08741997a6243642d7a5fc627eede9fbeea9f9d931f90daa1cc6a760e352a36d55f22ea1a8bb4bd993d96f4cd006ad9de128d7ea9cd74b875ac2c55e62905b67eb354b5bac48961940fbdc1c0dc58ebc3b7356cb95c11505c8d9bbecc8670d7327ed5fcffd3577ed7d87adf628c5b44cb92c1e33f66715d2d429d3a62dc88a80fc3a3b4a720e9680b4ff444d4e87057a7fbe66afba6bffd96b641bfb03c0802528e30768c5695db92d0dd4f4db3d5301c17ed37accbe3c89d2c70d4fbb3e40d30164b640154fa9ecd5565b11af24f40fe91fbca3966ce1632a3dbbd6c1af432b2100a9f7136834fc00609170b6cf66a3861bf9aa1902292152ba6efdefeacc4d4d37e20896a42243f3ec2f4ae463cfa455e92d4df33688cb127ade64dbad056b1614fe2f2a32b0b2536a23f4b019511df378778c374413629e7afe83a047fe6745de19397193e7082f8ab27b4b41695f43abd9c'

def unpad(s):
return s[:-ord(s[len(s)-1:])]

def decrypt(cipherId):
dec = AES.new(key, AES.MODE_CBC, iv)
cipher = bytes.fromhex(cipherId)
id = dec.decrypt(cipher)
node_dec = unpad(id.decode('ISO-8859-1'))
return node_dec

if __name__ == '__main__':
x = decrypt(cipher)
result = ""
for i in range(n - 1):
y = decrypt(x)
x = y
result = x
print(result)

相关内容

  • 没有找到相关文章

最新更新