问题
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.
回答1:
try
number = 427021005928
encode = base64.b64encode(bytes(number))
decode = base64.b64decode(encodeNumber)
回答2:
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).
回答3:
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'
回答4:
You can do following conversions by using python
First of all import base64 by using following syntax
>>> import base64
For converting text to base64 do following
encoding
>>> base64.b64encode("cloth".encode()).decode()
'Y2xvdGg='
decoding
>>> base64.b64decode("Y2xvdGg=".encode()).decode()
'cloth'
来源:https://stackoverflow.com/questions/55354229/converting-an-integer-value-to-base64-and-then-decoding-it-to-get-a-plaintext