将Integer值转换为base64,然后对其进行解码以获取纯文本 [英] Converting an Integer value to base64, and then decoding it to get a plaintext

查看:118
本文介绍了将Integer值转换为base64,然后对其进行解码以获取纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个编号427021005928,我应该将其更改为base64编码的字符串,然后对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.

当转换为二进制时,此十进制值427021005928给出110001101101100011011111110111010001101000,它对应于'Y2xvdGg =',这就是我想要的.从( https://cryptii.com/pipes/binary-to-base64)

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)

然后最后我解码'Y2xvdGg ='以获取文本布料.

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

我的问题是我不知道如何使用Python从十进制或二进制值中获取'Y2xvdGg ='

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='

一些帮助将不胜感激!

注意:我一开始只有这个值427021005928.我需要得到base64和纯文本答案.

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

推荐答案

一种优雅的方法是使用 [Python 3]:结构-将字节解释为打包的二进制数据,但是考虑到 Python 的数字大小不是固定的,因此需要进行一些额外的计算(例如,数字长度为 5 个字节).

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).

很明显,在线转换器将 base64 编码应用于数字的内存表示形式,可以通过

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):

对于向后过程,需要相反的步骤.有2种选择:

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

  • 事情是手动完成的(也可以应用于转发"过程)
  • 使用 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).

这篇关于将Integer值转换为base64,然后对其进行解码以获取纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆