0

I have a binary string encrypted with this: ( S is the binary string)

res = ''.join(format(ord(i), 'b') for i in s)

How to I decrypt it?

I tried the following but then the string is empty?

    for i in s:
      res += chr(int(str(i),2))
   print(res)
Sk1ppy
  • 3
  • 2

3 Answers3

1

Use '07b' or '08b' in format while encoding to binary, as it will keep leading zeros and represent number in 7 or 8 bits which will make it easy to decode back. Now we will consider 8 bits at a time as each character is represented in 8 bits now. Use 7 everywhere if you happen to use '07b' in format. Ascii characters need 7 bits to be represented.

>>> s = "abcde"
>>> res = ''.join(format(ord(i), '08b') for i in s)
>>> ans = []
>>> for i in range(0, len(res), 8):
...      ans.append(chr(int(res[i:i+8],2)))
... 
>>> ''.join(ans)
'abcde'

One liner:

>>> ''.join([ chr(int(res[i:i+8],2)) for i in range(0,len(res),8)])
'abcde'

Reference:

Convert to binary and keep leading zeros in Python

Is ASCII code 7-bit or 8-bit?

รยקคгรђשค
  • 1,919
  • 1
  • 10
  • 18
  • It does not work as expected s =(" Hello") #Encrypted res = ''.join(format(ord(i), 'b') for i in s) print("Encrypted:" + res) #Decrypt res = ''.join([chr(int(res[i:i+7],2)) for i in range(0,len(res),7)]) print("Decrypted: " + res) Results: Encrypted:10000010010001100101110110011011001101111 Decrypted: AKYY/ – Sk1ppy Sep 21 '20 at 14:50
  • Corrected. format(ord(i), 'b') was formatting to binary but without keeping leading zeros. – รยקคгรђשค Sep 21 '20 at 15:14
  • That is something I cant change. Like in the question I have a binary string encrypted with this code -> res = ''.join(format(ord(i), 'b') for i in s) <- and I am unable to decrypt it. I tried charging the 7 to 8 but no results. – Sk1ppy Sep 21 '20 at 15:37
  • you need to change the format to `08b` or `07b` while encoding to binary instead of `b` as mentioned in the answer as there can be multiple interpretations if the length of bits for a single character is not fixed. If you can provide the original question, it will be more helpful. – รยקคгรђשค Sep 21 '20 at 16:06
1

Your approach won't work. All you're doing is converting the ones and zeros of the encrypted string into \x01 and \x00 bytes, neither of which are printable.

Anyway, this isn't really encryption; it's just a flawed binary encoding where different inputs can give you the same output:

encode('0p')            == '1100001110000'
encode('a0')            == '1100001110000'
encode('\x01!\x18\x00') == '1100001110000'

encode('password')      == '11100001100001111001111100111110111110111111100101100100'
encode('p0<|>>?%$')     == '11100001100001111001111100111110111110111111100101100100'

As a strategy for decoding the output of this function, you need to make some assumptions about the original input.

To start with, it might be reasonable to assume that the message contains only printable ASCII values in the range from 32 to 126. In that case, each input character will be encoded as a chunk of either six or seven binary digits in the output, and each chunk will start with 1 (because format(ord(i), 'b') won't begin with 0 unless i==0).

For example, suppose you want to decode the following string:

11010001100101110110011011001101111

The first chunk must be seven bits, otherwise the next chunk would begin with 0, which is impossible.

1101000 1100101110110011011001101111

For the next chunk, it looks like we can consume 6 bits and leave a string that begins with 1:

1101000 110010 1110110011011001101111

But if we do that, then it would be impossible to extract 6 or 7 further bits and leave a string that starts with 1:

                      ### invalid ###
1101000 110010 111011 0011011001101111

                       ### invalid ###
1101000 110010 1110110 011011001101111

This would suggest that an earlier assumption was incorrect. Using a backtracking algorithm, you can identify all the valid partitionings of the encoded data.

Since a long encoded string could have a large number of valid partitionings, you might also need to rank each possible input string based on heuristics such as the number of alphabet characters minus the number of non-alphabet characters. In the above example, password would score 8 points, but p0<|>>?%$ would score −7. The correct input string is likely to be among the highest-scoring alternatives.

r3mainer
  • 23,981
  • 3
  • 51
  • 88
0

It does not work as expected

s =(" Hello")
#Encrypted
res = ''.join(format(ord(i), 'b') for i in s)
print("Encrypted:" + res)

#Decrypt
res = ''.join([chr(int(res[i:i+7],2)) for i in range(0,len(res),7)])
print("Decrypted: " + res)

Results:

Encrypted:10000010010001100101110110011011001101111

Decrypted: AKYY/

Sk1ppy
  • 3
  • 2