Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an Integer value to base64, and then decoding it to get a plaintext

Tags:

python

base64

I am given this number 427021005928, which i am supposed to change into a base64 encoded string and then decode the base64 string to get a plain text.

This decimal value 427021005928 when converted to binary gives 110001101101100011011110111010001101000 which corresponds to 'Y2xvdGg=', which is what i want. Got the conversion from (https://cryptii.com/pipes/binary-to-base64)

And then finally i decode 'Y2xvdGg=' to get the text cloth.

My problem is i do not have any idea how to use Python to get from either the decimal or binary value to get 'Y2xvdGg='

Some help would be appreciated!

NOTE: I only have this value 427021005928 at the start. I need to get the base64 and plaintext answers.

like image 847
Moud Avatar asked Mar 26 '19 09:03

Moud


People also ask

Can Base64 be decoded?

In JavaScript there are two functions respectively for decoding and encoding Base64 strings: btoa() : creates a Base64-encoded ASCII string from a "string" of binary data ("btoa" should be read as "binary to ASCII"). atob() : decodes a Base64-encoded string("atob" should be read as "ASCII to binary").

Can Base64 encoding be reversed?

Encoded data has similar paddings at the end as base64, but definitely it is not base64. Probably crypted with some SHA-like algorithm. With the data you provided, I would say that it is not possible to reverse-engineer the encoding process.

What is encoding and decoding Base64?

What Does Base64 Mean? Base64 is an encoding and decoding technique used to convert binary data to an American Standard for Information Interchange (ASCII) text format, and vice versa.

How do I decode Base64?

Decode from Base64 format. Simply enter your data then push the decode button. For encoded binaries (like images, documents, etc.) use the file upload form a little further down on this page. Source character set.


2 Answers

One elegant way would be using [Python 3]: struct - Interpret bytes as packed binary data, but given the fact that Python numbers are not fixed size, some additional computation would be required (for example, the number is 5 bytes long).

Apparently, the online converter, applied the base64 encoding on the number's memory representation, which can be obtained via [Python 3]: int.to_bytes(length, byteorder, *, signed=False)(endianness is important, and in this case it's big):

For the backwards process, reversed steps are required. There are 2 alternatives:

  • Things being done manually (this could also be applied to the "forward" process)
  • Using int.from_bytes
>>> import base64
>>>
>>> number = 427021005928
>>>
>>> number_bytes = number.to_bytes((number.bit_length() + 7) // 8, byteorder="big")  # Here's where the magic happens
>>> number_bytes, number_bytes.decode()
(b'cloth', 'cloth')
>>>
>>> encoded = base64.b64encode(number_bytes)
>>> encoded, encoded.decode()  # Don't let yourself tricked by the variable and method names resemblance
(b'Y2xvdGg=', 'Y2xvdGg=')
>>>
>>> # Now, getting the number back
...
>>> decoded = base64.b64decode(encoded)
>>> decoded
b'cloth'
>>>
>>> final_number0 = sum((item * 256 ** idx for idx, item in enumerate(reversed(decoded))))
>>> final_number0
427021005928
>>> number == final_number0
True
>>>
>>> # OR using from_bytes
...
>>> final_number1 = int.from_bytes(decoded, byteorder="big")
>>> final_number1
427021005928
>>> final_number1 == number
True

For more details on bitwise operations, check [SO]: Output of crc32b in PHP is not equal to Python (@CristiFati's answer).

like image 105
CristiFati Avatar answered Sep 28 '22 18:09

CristiFati


Try this (https://docs.python.org/3/library/stdtypes.html#int.to_bytes)

>>> import base64
>>> x=427021005928
>>> y=x.to_bytes(5,byteorder='big').decode('utf-8')
>>> base64.b64encode(y.encode()).decode()
'Y2xvdGg='
>>> y
'cloth'
like image 33
Devesh Kumar Singh Avatar answered Sep 28 '22 18:09

Devesh Kumar Singh