Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取Python中整数的大端字节序列_Python_Integer_Rsa - Fatal编程技术网

获取Python中整数的大端字节序列

获取Python中整数的大端字节序列,python,integer,rsa,Python,Integer,Rsa,根据这篇文章: 我需要base64url编码这两个数字的octet值: n = 124692971944797177402996703053303877641609106436730124136075828918287037758927191447826707233876916396730936365584704201525802806009892366608834910101419219957891196104538322266555160652329444921468362525907130

根据这篇文章:

我需要base64url编码这两个数字的octet值

n = 124692971944797177402996703053303877641609106436730124136075828918287037758927191447826707233876916396730936365584704201525802806009892366608834910101419219957891196104538322266555160652329444921468362525907130134965311064068870381940624996449410632960760491317833379253431879193412822078872504618021680609253

e = 65537
“n”(模数)参数包含RSA公钥的模数值。它表示为Base64urlUInt编码值。 注意,实现者发现一些加密库 将一个额外的零值八位字节作为其表示的模表示的前缀 例如,返回2048位密钥的257个八位字节,而不是 超过256。使用这些库的实现需要 是否要从base64url编码中省略额外的八位字节 代表性

“e”(指数)参数包含RSA的指数值 公钥。它表示为Base64urlUInt编码值。 例如,当表示值65537时,八进制序列 要进行base64url编码,必须由三个八位组组成[1,0,1]; 该值的结果表示为“AQAB”

例如,有效的编码应如下所示:

?在Python中如何实现这一点?

不幸的是,pack()不支持那么大的数字,而int.to_bytes()仅在Python 3中受支持,因此我们必须在编码之前自己打包它们。受此启发,我首先通过转换为十六进制字符串找到了解决方案:

import math
import base64

def Base64urlUInt(n):
    # fromhex() needs an even number of hex characters,
    # so when converting our number to hex we need to give it an even
    # length. (2 characters per byte, 8 bits per byte)
    length = int(math.ceil(n.bit_length() / 8.0)) * 2
    fmt = '%%0%dx' % length
    packed = bytearray.fromhex(fmt % n)
    return base64.urlsafe_b64encode(packed).rstrip('=')
导致:

n = 124692971944797177402996703053303877641609106436730124136075828918287037758927191447826707233876916396730936365584704201525802806009892366608834910101419219957891196104538322266555160652329444921468362525907130134965311064068870381940624996449410632960760491317833379253431879193412822078872504618021680609253
e = 65537

Base64urlUInt(n) == 'sZGVa39dSmJ5c7mbOsJZaq62MVjPD3xNPb-Aw3VJznk6piF5GGgdMoQmAjNmANVBBpPUyQU2SEHgXQvp6j52E662umdV2xU-1ETzn2dW23jtdTFPHRG4BFZz7m14MXX9i0QqgWVnTRy-DD5VITkFZvBqCEzWjT_y47DYD2Dod-U'
Base64urlUInt(e) == 'AQAB'

在寻找解决这个问题的最佳方法之后,使用似乎是一个好方法,而不是创建我自己的函数

pip install pyjwkest
然后我们使用
long\u to\u base64
函数进行此操作

>>> from jwkest import long_to_base64
>>> long_to_base64(65537)
'AQAB'

下面是任务的一段不同的Python代码,取自
rsalette

def bytes_to_int(data):
    """Convert bytes to an integer"""
    hexy = binascii.hexlify(data)
    hexy = b'0'*(len(hexy)%2) + hexy
    return int(hexy, 16)

def b64_to_int(data):
    """Convert urlsafe_b64encode(data) to an integer"""
    return bytes_to_int(urlsafe_b64decode(data))

def int_to_bytes(integer):
    hexy = as_binary('%x' % integer)
    hexy = b'0'*(len(hexy)%2) + hexy
    data = binascii.unhexlify(hexy)
    return data

def int_to_b64(integer):
    """Convert an integer to urlsafe_b64encode() data"""
    return urlsafe_b64encode(int_to_bytes(integer))

def as_binary(text):
    return text.encode('latin1')

您使用的是什么Python版本?