[英]Recursive Decryption of String with AES-256-cbc
I am trying to decrypt an AES CBC string for the number of times it was decrypted.我正在尝试根据解密次数来解密 AES CBC 字符串。 I was successful in decrypting the first time.我第一次解密成功。
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
that is the first stage of encryption, but I don't know how to set a loop to run the function again based on n
.这是加密的第一阶段,但我不知道如何设置循环以根据n
再次运行该函数。 this cipher for example was encrypted twice, it might be three or four or more.例如,这个密码被加密了两次,它可能是三个或四个或更多。
I created another function to decrypt the output of the decrypt function because the output gives a different codec.我创建了另一个函数来解密解密函数的输出,因为输出提供了不同的编解码器。
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
so I tried writing this loop所以我试着写这个循环
x = decrypt(cipher)
for i in range (n):
y = decrypt_again(x)
but it only works for n = 1, if n is more than 1, it just keeps repeating y instead of parsing it again.但它只适用于 n = 1,如果 n 大于 1,它只是不断重复 y 而不是再次解析它。
how can I go about it, please?请问我该怎么办?
also if I re-run the function twice something happens to the string, I get ó³*Ä'»g)X»#¾ú84-8089-be57330fcd45
instead of this a214868d-f40b-4184-8089-be57330fcd45
, there seem to be a break in the codec此外,如果我重新运行该函数两次,字符串发生了一些事情,我得到ó³*Ä'»g)X»#¾ú84-8089-be57330fcd45
而不是这个a214868d-f40b-4184-8089-be57330fcd45
,似乎有打破编解码器
any reason why this is so?为什么会这样?
every time it sets y
to decrypt_again(x)
, x
is still the same so you should add a line that sets x
to y
so it remembers the result of the previous iteration每次将y
设置为decrypt_again(x)
, x
仍然相同,因此您应该添加一行将x
设置为y
以便它记住上一次迭代的结果
x = decrypt(cipher)
for i in range (n):
y = decrypt_again(x)
x = y
so guys I was able to solve the issue.所以伙计们,我能够解决这个问题。 here is the correct code that works top-notch.这是一流的正确代码。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.