如何在加密代码中解密文件

时间:2021-10-22 18:23:44

I have tried to decrypt an encrypted image with no success I really can't program it the encryption code is listed below (parts are missing)

我试图解密加密的图像没有成功我真的无法编程它下面列出的加密代码(部分缺失)

The encrypted picture should be decrypted by the xor the key using the first byte 0x89 if the of the PNG file and switching the place of the pictures

如果PNG文件的位置和切换图片的位置,则应使用第一个字节0x89通过xor键解密加密的图片

`import key_transformator
 import random
 import string

 key_length = 4


def generate_initial_key():
  return ''.join(random.choice(string.ascii_uppercase) for _ in range(4))


def xor(s1, s2):
  res = [chr(0)]*key_length
  for i in range(len(res)):
      q = ord(s1[i])
      d = ord(s2[i])
      k = q ^ d
      res[i] = chr(k)
      res = ''.join(res)
      return res


 def add_padding(img):
    l = key_length - len(img)%key_length
    img += chr(l)*l
     return img


  with open('decrypted.png', 'rb') as f:
   img = f.read()

img = add_padding(img)
key = generate_initial_key()

enc_data = ''
for i in range(0, len(img), key_length):
  enc = xor(img[i:i+key_length], key)
   key = key_transformator.transform(key)
    enc_data += enc

with open('encrypted.png', 'wb') as f:
   f.write(enc_data)`

3 个解决方案

#1


0  

What you trying to do with that script?

您尝试使用该脚本做什么?

#2


0  

It's hard to understand what exactly don't work: The code you showed? If yes I'm guessing that it's "key_transformator" that is missing. It probably have a function that encode the key based on some algorithm.

很难理解究竟什么不起作用:你展示的代码?如果是的话我猜它是缺少的“key_transformator”。它可能具有基于某种算法对密钥进行编码的功能。

Maybe you trying to decrypt a file made by this code (is this a CTF challenge?): Even if you have the missing function you'll still need the original random key. If you have it you need to xor it with the first one you're file and then find a way to understand what was the next key

也许你试图解密这个代码生成的文件(这是一个CTF挑战吗?):即使你有缺失的功能,你仍然需要原始的随机密钥。如果你有它你需要用你提交的第一个xor它,然后找到一种方法来理解下一个键是什么

#3


0  

The idea is to solve the CSA challenge by your own, not to * it ^^

我们的想法是通过您自己解决CSA挑战,而不是堆栈溢出它^^

Since challenge only ends up on Sep, I won't post a full solution. Here are few (heavy) tips on how to solve this challenge (png++): 1) KNOWN file format has KNOWN file header. 2) Pay close attention the key size is 4 (not by coincidence). 3) Ask yourself what happens with key_transformation when reaching 0xFF, can it go to 0x100 ??

由于挑战只在9月结束,我不会发布完整的解决方案。以下是有关如何解决此挑战的一些(重点)提示(png ++):1)KNOWN文件格式具有KNOWN文件头。 2)密切关注密钥大小为4(不是巧合)。 3)问问自己到达0xFF时key_transformation会发生什么,它可以转到0x100吗?

This above should be suffice for you to write your own decryptor, convert the encrypted.png to flag.pnh & get the flag along with it's 30 points

上面这个应该足以让你编写自己的解密器,将encrypted.png转换为flag.pnh并获得旗帜以及它的30分

#1


0  

What you trying to do with that script?

您尝试使用该脚本做什么?

#2


0  

It's hard to understand what exactly don't work: The code you showed? If yes I'm guessing that it's "key_transformator" that is missing. It probably have a function that encode the key based on some algorithm.

很难理解究竟什么不起作用:你展示的代码?如果是的话我猜它是缺少的“key_transformator”。它可能具有基于某种算法对密钥进行编码的功能。

Maybe you trying to decrypt a file made by this code (is this a CTF challenge?): Even if you have the missing function you'll still need the original random key. If you have it you need to xor it with the first one you're file and then find a way to understand what was the next key

也许你试图解密这个代码生成的文件(这是一个CTF挑战吗?):即使你有缺失的功能,你仍然需要原始的随机密钥。如果你有它你需要用你提交的第一个xor它,然后找到一种方法来理解下一个键是什么

#3


0  

The idea is to solve the CSA challenge by your own, not to * it ^^

我们的想法是通过您自己解决CSA挑战,而不是堆栈溢出它^^

Since challenge only ends up on Sep, I won't post a full solution. Here are few (heavy) tips on how to solve this challenge (png++): 1) KNOWN file format has KNOWN file header. 2) Pay close attention the key size is 4 (not by coincidence). 3) Ask yourself what happens with key_transformation when reaching 0xFF, can it go to 0x100 ??

由于挑战只在9月结束,我不会发布完整的解决方案。以下是有关如何解决此挑战的一些(重点)提示(png ++):1)KNOWN文件格式具有KNOWN文件头。 2)密切关注密钥大小为4(不是巧合)。 3)问问自己到达0xFF时key_transformation会发生什么,它可以转到0x100吗?

This above should be suffice for you to write your own decryptor, convert the encrypted.png to flag.pnh & get the flag along with it's 30 points

上面这个应该足以让你编写自己的解密器,将encrypted.png转换为flag.pnh并获得旗帜以及它的30分