使用https://jwt.io/使用RSA密钥对JWT签名时签名无效

时间:2018-11-29 14:14:05

标签: jwt rsa jwk

嗨,我正在通过POSTMAN测试一些api。

  1. REGISTRATION API-它需要公共密钥并存储在服务器中。要获取公共JWK密钥,我正在使用Upgraded 。它生成带有公钥和私钥的密钥。下面是生成的密钥:

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    int global[3];
    
    void *sum_thread(void *arg)
    {
        int *args_array;
        args_array = arg;
        int n1,n2,sum;
        n1=args_array[0];
        n2=args_array[1];
        sum = n1+n2;
    
        printf("N1 + N2 = %d\n",sum);
    
        return sum;
    }
    void *sum_thread1(void *arg)
    {
        int *args_array;
        args_array = arg;
        int n3,n4,sum2;
        n3=args_array[0];
        n4=args_array[1];
        sum2=n3+n4;
    
        printf("N3 + N4 = %d\n",sum2);
    
        return sum2;
    }
    
    
    
    int main()
    {
        printf("First number: ");
        scanf("%d",&global[0]);
    
        printf("Second number: ");
        scanf("%d",&global[1]);
    
        printf("Third number: ");
        scanf("%d",&global[2]);
    
        printf("Fourth number: ");
        scanf("%d",&global[3]);
    
        pthread_t tid_sum;
        pthread_create(&tid_sum,NULL,sum_thread,global);
        pthread_join(tid_sum,NULL);
    
        pthread_t tid_sum1;
        pthread_create(&tid_sum1,NULL,sum_thread1,global);
        pthread_join(tid_sum1,NULL);
    
        return 0;
    }
    

    }

我仅向该API发送公钥部分,因此该API可以正常工作。

  1. 使用JWT API获取访问令牌-这需要一个JWT,它将通过一些声明创建。而且我们必须使用RSA公钥和私钥对其进行签名。我在签署JWT时遇到问题。 要从JWK上方获取公钥和私钥的文本/ pem版本,我正在使用https://mkjwk.org/。这给了我们下面的公钥和私钥。

    {
      "kty": "RSA",
      "d": "HgP6c8xA3D_-8DKgSk3fQ_FZuPj2RNSFE5NLfGz3GJjkyt9fzOPztNObQmLZ2EoJzPrYL8ljk-1mKGIr5Ma1n4TPX_kQ9JErq9wNyhMUTykQ8PqjMuxmUpddn43RZ27VPjvUvHMulk5hPBFv0uH3LnDsM1xn34icj40y4zcRTYXwixqDgj74Ua_9aaZwPXX06Ykc3vzC5M4F5JP4ZjuLTFjmM8jxzJpu4JNDncXwhHE5Xtv3t1oezXTHNZdRzyTTtFLOeyYeLt5WWM-y549du3hsUtvgNnJ4JTS4uEHEonDMW8CV5ZzoQE8F81LT5Q84coYzOjVlzC50w7st3vLFYQ",
      "e": "AQAB",
      "use": "sig",
      "kid": "sdk291",
      "alg": "RS256",
      "n": "0h5Pgw32t3NtODVj1UrNkk90RytZBz2T09JBEo1NwYtw0T7NM3SrFsleLK8e_DknE26rB8EXD2T1YmUSuseuWvW-LPdwbk--SrKo8Df1H5ff5hDZYJ_x-NPDJ_ZmoY7r2U83aGY-DubilufhDf6icB2auTGxBvVylAl5Jdf7UBsjHAWrgWAtFFvnkbcaUk1O7ZA8nS4Iyk4l8vVsoUOJCSRrysceObAOG-EYj1UfdzOBZsuSj5Usst6ebRjF9M1VNJOLcvm6EiAqazayyqbsengnA-hqUGRgfd6HXXZ5Hg4BU_srPVBXrVmx81azHY5lRUdZr_Khtw4O0Zy3UIwMWw"
    

我正在使用这些密钥,然后转到https://8gwifi.org/jwkconvertfunctions.jsp,然后使用所需的值更新Header和有效负载。 标题:

    -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0h5Pgw32t3NtODVj1UrN
kk90RytZBz2T09JBEo1NwYtw0T7NM3SrFsleLK8e/DknE26rB8EXD2T1YmUSuseu
WvW+LPdwbk++SrKo8Df1H5ff5hDZYJ/x+NPDJ/ZmoY7r2U83aGY+DubilufhDf6i
cB2auTGxBvVylAl5Jdf7UBsjHAWrgWAtFFvnkbcaUk1O7ZA8nS4Iyk4l8vVsoUOJ
CSRrysceObAOG+EYj1UfdzOBZsuSj5Usst6ebRjF9M1VNJOLcvm6EiAqazayyqbs
engnA+hqUGRgfd6HXXZ5Hg4BU/srPVBXrVmx81azHY5lRUdZr/Khtw4O0Zy3UIwM
WwIDAQAB
-----END PUBLIC KEY-----


-----BEGIN RSA PRIVATE KEY-----
MIICHgIBAAKCAQEA0h5Pgw32t3NtODVj1UrNkk90RytZBz2T09JBEo1NwYtw0T7N
M3SrFsleLK8e/DknE26rB8EXD2T1YmUSuseuWvW+LPdwbk++SrKo8Df1H5ff5hDZ
YJ/x+NPDJ/ZmoY7r2U83aGY+DubilufhDf6icB2auTGxBvVylAl5Jdf7UBsjHAWr
gWAtFFvnkbcaUk1O7ZA8nS4Iyk4l8vVsoUOJCSRrysceObAOG+EYj1UfdzOBZsuS
j5Usst6ebRjF9M1VNJOLcvm6EiAqazayyqbsengnA+hqUGRgfd6HXXZ5Hg4BU/sr
PVBXrVmx81azHY5lRUdZr/Khtw4O0Zy3UIwMWwIBAAKCAQAeA/pzzEDcP/7wMqBK
Td9D8Vm4+PZE1IUTk0t8bPcYmOTK31/M4/O005tCYtnYSgnM+tgvyWOT7WYoYivk
xrWfhM9f+RD0kSur3A3KExRPKRDw+qMy7GZSl12fjdFnbtU+O9S8cy6WTmE8EW/S
4fcucOwzXGffiJyPjTLjNxFNhfCLGoOCPvhRr/1ppnA9dfTpiRze/MLkzgXkk/hm
O4tMWOYzyPHMmm7gk0OdxfCEcTle2/e3Wh7NdMc1l1HPJNO0Us57Jh4u3lZYz7Ln
j127eGxS2+A2cnglNLi4QcSicMxbwJXlnOhATwXzUtPlDzhyhjM6NWXMLnTDuy3e
8sVhAgEAAgEAAgEAAgEAAgEA
-----END RSA PRIVATE KEY-----

有效载荷:

https://jwt.io/

此后,当我从上面签名包括公共和私有密钥的JWT时,它不生成JWT令牌并给出“ INVALID SIGNATURE”错误。因此,我无法生成该第二个API的输入所需的签名JWT。

PS-我现在处于测试阶段。因此,我没有编写任何代码来测试api,仅使用在线生成器工具。

请让我知道如何解决该问题。

0 个答案:

没有答案