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

标签 python integer rsa

基于这篇文章:How to return RSA key in jwks_uri endpoint for OpenID Connect Discovery

我需要base64url-encode这两个数字的octet value:

n = 124692971944797177402996703053303877641609106436730124136075828918287037758927191447826707233876916396730936365584704201525802806009892366608834910101419219957891196104538322266555160652329444921468362525907130134965311064068870381940624996449410632960760491317833379253431879193412822078872504618021680609253

e = 65537

The "n" (modulus) parameter contains the modulus value for the RSA public key. It is represented as a Base64urlUInt-encoded value. Note that implementers have found that some cryptographic libraries prefix an extra zero-valued octet to the modulus representations they return, for instance, returning 257 octets for a 2048-bit key, rather than 256. Implementations using such libraries will need to take care to omit the extra octet from the base64url-encoded representation.

The "e" (exponent) parameter contains the exponent value for the RSA public key. It is represented as a Base64urlUInt-encoded value. For instance, when representing the value 65537, the octet sequence to be base64url-encoded MUST consist of the three octets [1, 0, 1]; the resulting representation for this value is "AQAB".

例如,一个有效的编码应该是这样的:https://www.googleapis.com/oauth2/v3/certs

我如何在 Python 中执行此操作?

最佳答案

在搜索了解决此问题的最佳方法后,使用 pyjwkest似乎是一个不错的方法,而不是创建我自己的函数。

pip install pyjwkest

然后我们为此使用long_to_base64函数

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

关于python - 在Python中获取整数的大端字节序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30784217/

相关文章:

python - Truecasing - SpaCy

python - 当从 Upstart 运行时没有获得环境变量时,Gunicorn + Django 不起作用

r - 为什么 R 会使用 "L"后缀来表示整数?

c++ - 表示 2000 位或更多位的整数

c++ - 使用 WinCrypt/CryptoAPI 验证基于 OpenPGP 的 RSA 签名

java - BouncycaSTLe RSA key 大小和填充

python - Tensorflow估计器输入函数: defining each feature or not?

python - 通过快捷方式运行Python可执行文件会更改程序运行的位置

java - 可能会损失精度/[类型]无法取消引用

ssh - 监控ssh连接,从authorized_keys获取登录用户信息