我正在尝试使用python编码JWT,我需要在base64中编码它,我做了。然后我必须在发送到服务器之前用私钥对其进行签名。实际上我被封锁,什么时候签名我不知道怎么样,我从昨天开始在网上搜索,我有点失落。这是我的代码。
import jwt
print ("\nStart..")
encoded = jwt.encode({'some': 'payload'}, 'secret', algorithm='HS256')
print("\nJWT : ",encoded)
try:
decoded = jwt.decode(encoded, 'secret', algorithms=['HS256'])
except jwt.InvalidTokenError:
print("Invalid token!!")
print("\ndecoded : ", decoded)
print("\nencodage : ")
#LOAD THE PRIVATE KEY
#SIGN THE ENCODED token
并且有我的密钥格式,它是一个RSA私钥。
-----BEGIN RSA PRIVATE KEY-----
dsjkfhsdfkshkdfhks...
-----END RSA PRIVATE KEY-----
我给服务器crt.crt颁发了证书,我想我需要用我的私钥加密,然后他们就能用证书中的密钥解密消息,这就是我所理解的。
在此先感谢,G.B
你可以尝试参考:
from Crypto.PublicKey import RSA
from Crypto.Cipher import HS256
def encrypt_text(input_text):
utf8_text = input_text.encode('utf-8')
pub_key = RSA.importKey(open(settings.RSA).read())
cipher = HS256.new(public_key)
cipher_text = base64.encodebytes(cipher.encrypt(utf8_text))
return cipher_text.decode('utf-8')
创建公钥和私钥:
ssh-keygen -t rsa -C "[email protected]"
希望有帮助
根据JWT RFC,RSA + SHA256的算法类型是“RS256”,但你使用的是“HS256”