Binary to text decryption

Question:

Write a script that decrypts a message coded by the method used in Project 6.

Method used in project 6:

Add 1 to each character’s numeric ASCII value.
Convert it to a bit string.
Shift the bits of this string one place to the left.
A single-space character in the encrypted string separates the resulting bit strings.

An example of the program input and output is shown below:

Enter the coded text: 0010011 1001101 1011011 1011011 1100001 000011 1110001 1100001 1100111 1011011 1001011 000101

Hello world!

The commented out stuff is seperate part of the code I’m not on. I am trying to get only with the binary to decimal conversion as I would like to figure as much out on my own as I can, but I feel like I’m getting too stuck and nothing I think of works. But in essence, it must be converted from binary to decimal, the decimal to ascii, then ascii to a writable string

This my code so far:

message = input("Enter the coded text: ")
decimal = 0
exponent = len(message) - 1
bString = ""
for digit in message:
    decimal = decimal + int(digit) * 2 ** exponent
    exponent = exponent - 1

print(bString)
   
   # for ch in value:
    #    value = chr(ch)
     #   bString = ""
        
        
 # print(bString)

When I run this top half I get this in return

Traceback (most recent call last):
  File "decrypt.py", line 6, in <module>
    decimal = decimal + int(digit) * 2 ** exponent
ValueError: invalid literal for int() with base 10: ' '
Asked By: Tarkle

||

Answers:

You’re processing every individual character of message one by one, i.e. the zeros, ones and spaces. You should instead do message = message.split() before the loop, to split message into the binary chunks.

You could see that if you put a print in the loop:

>>> for digit in message:
...     print(digit)
0
0
1
0
0
1
1
   # <--- your code fails here, trying to parse a space to int  
1
# etc.

What you probably need is:

>>> for digit in message.split():
...     print(digit)
0010011
1001101
1011011
1011011
# etc.
Answered By: Czaporka

version 4:

b = '0110001' # will store the binary string
i = int(b,2)  # this will convert the binary to a decimal value
c = chr(i)    # this will convert the decimal to ascii value
x = chr(i-1)  # this will reduce 1 from the decimal and conver to ascii value
print (b, i, c, x) #print binary, decimal, ascii, and shifted ascii values

The output of this will be:

0110001 49 1 0

Another option based on what your question says:

b = '0110001' # will store the binary string
b = b + '1'   # shifting the binary string one place to the left
i = int(b,2)  # this will convert the new binary to a decimal value
c = chr(i)    # this will convert the new decimal to ascii value
x = chr(i-1)  # this will reduce 1 from the decimal and conver to ascii value
print (b, i, c, x) #print binary, decimal, ascii, and shifted ascii values

The output of this will be:

01100011 99 c b

I hope these inputs will help you get the best answer.

If you are able to clearly articulate your problem statement, I can help you solve the problem.

version 3: Binary to Decimal to ASCII conversion:

Here’s a program that will convert the binary value to integer and then convert that to an ascii value for you to display.

message = input("Enter the coded text: ")
bString = ""
for digit in message.split():
    bString += chr(int(digit,2))
print(bString)

The output of this program is as follows:

Enter the coded text: 01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001
Hello, World!

version 2

If you are trying to convert the binary values into an integer, you can do it as simple as:

message = input("Enter the coded text: ")
for digit in message.split():
    print (digit, int(digit,2))

The result of this will be:

Enter the coded text: 0010011 1001101 1011011 1011011 1100001 000011 1110001 1100001 1100111 1011011 1001011 000101
0010011 19
1001101 77
1011011 91
1011011 91
1100001 97
000011 3
1110001 113
1100001 97
1100111 103
1011011 91
1001011 75
000101 5

However, this still does not give you Hello World. To give you Hello World, your input value has to be :

01001000 01100101 01101100 01101100 01101111 00101100 00100000 01010111 01101111 01110010 01101100 01100100 00100001

Old answer

While this solves your ValueError, it does not solve your full problem.

I added a if statement inside your for-loop to convert only digits. To do that, i am check if the value is a digit using digit.isdigit(). This will convert the value and compute into variable decimal (btw, its a bad variable name).

What is the use of bString. Do you intend to do something with it? You are printing out bString. What do you want to do with it?

message = input("Enter the coded text: ")
decimal = 0
exponent = len(message) - 1
bString = ""
for digit in message:
    if digit.isdigit():
        decimal = decimal + int(digit) * 2 ** exponent
    exponent = exponent - 1
print(bString)

The output for this is as follows:

Enter the coded text: 0010011 1001101 1011011 1011011 1100001 000011 1110001 1100001 1100111 1011011 1001011 000101

The output was a blank line as bString was empty string. However, i did check the value of decimal. It printed out as follows:

>>> decimal
1493433473927764848841434501
Answered By: Joe Ferndz

Recently ran into the same problem and I would like to help you solve it since I know what the question is fully asking.

Firstly, you have to get the coded text as well as create an empty plain text string (this will eventually be the value hidden inside of the coded text).

codedText = input("Enter the coded text: ")
plainText = ""

From there, you need to get the individual blocks of binary in the string (so in your case, you want to have 0010011, 1001101, 1011011, etc). We can do this by splitting the coded text string at every space to get the individual blocks of binary to perform the rest of our operations on.

codedText = input("Enter the coded text: ")
plainText = ""

for binary in codedText.split():

Lastly, we just need to reverse the method used in Project 6. So to reverse this, we must:

  1. Shift the bits of the individual blocks of binary to the right by 1.
  2. Get the ASCII value of the newly shifted binary.
  3. Subtract the ASCII value by 1.

Once we perform all of these operations, we will get the plain text.

codedText = input("Enter the coded text: ")
plainText = ""

for binary in codedText.split():
    # shift right by 1, imagine having 0010011
    # from the first operation, we get 1
    # from the second operation, we get 001001
    # now if we concatenate them together we get 1001001
    shiftedBinary = binary[-1:] + binary[:-1]
    
    # get the ASCII value of the shifted binary
    # we want to go from base 2 to base 10, hence why we have the second parameter as base=2
    asciiValue = int(shiftedBinary, base=2)
    
    # final step of our reversing process, just remove 1 off the ASCII value
    asciiValue -= 1

    # get each character of the ASCII value and concatenate them to the plain text string
    plainText += chr(asciiValue)

# we have our plain text, yay!
print(plainText)
Answered By: Ambrosia
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.