第十七章 RSA / 17.6.1密钥生成

       #include <openssl/rsa.h>

       int    main()

       {

              RSA                      *r;

              int                         bits=512,ret;

              unsigned long  e=RSA_3;

              BIGNUM               *bne;

 

              r=RSA_generate_key(bits,e,NULL,NULL);

              RSA_print_fp(stdout,r,11);

              RSA_free(r);

              bne=BN_new();

              ret=BN_set_word(bne,e);

              r=RSA_new();

              ret=RSA_generate_key_ex(r,bits,bne,NULL);

              if(ret!=1)

              {

                     printf("RSA_generate_key_ex err!\n");

                     return -1;

              }

              RSA_free(r);

              return 0;

       }

       说明:

       调用RSA_generate_keyRSA_generate_key_ex函数生成RSA密钥,

       调用RSA_print_fp打印密钥信息。

       输出:

       Private-Key: (512 bit)

       modulus:

           00:d0:93:40:10:21:dd:c2:0b:6a:24:f1:b1:d5:b5:

           77:79:ed:a9:a4:10:66:6e:88:d6:9b:0b:4c:91:7f:

           23:6f:8f:0d:9e:9a:b6:7c:f9:47:fc:20:c2:12:e4:

           b4:d7:ab:66:3e:73:d7:78:00:e6:5c:98:35:29:69:

           c2:9b:c7:e2:c3

       publicExponent: 3 (0x3)

       privateExponent:

           00:8b:0c:d5:60:16:93:d6:b2:46:c3:4b:cb:e3:ce:

           4f:a6:9e:71:18:0a:ee:f4:5b:39:bc:b2:33:0b:aa:

           17:9f:b3:7e:f0:0f:2a:24:b6:e4:73:40:ba:a0:65:

           d3:19:0f:c5:b5:4f:59:51:e2:df:9c:83:47:da:8d:

           84:0f:26:df:1b

       prime1:

           00:f7:4c:fb:ed:32:a6:74:5c:2d:6c:c1:c5:fe:3a:

           59:27:6a:53:5d:3e:73:49:f9:17:df:43:79:d4:d0:

           46:2f:0d

       prime2:

           00:d7:e9:88:0a:13:40:7c:f3:12:3d:60:85:f9:f7:

           ba:96:44:29:74:3e:b9:4c:f8:bb:6a:1e:1b:a7:b4:

           c7:65:0f

       exponent1:

           00:a4:dd:fd:48:cc:6e:f8:3d:73:9d:d6:83:fe:d1:

           90:c4:f1:8c:e8:d4:4c:db:fb:65:3f:82:51:38:8a:

           d9:74:b3

       exponent2:

           00:8f:f1:05:5c:0c:d5:a8:a2:0c:28:eb:03:fb:fa:

           7c:64:2d:70:f8:29:d0:dd:fb:27:9c:14:12:6f:cd:

           da:43:5f

       coefficient:

           00:d3:fa:ea:a0:21:7e:8a:e1:ab:c7:fd:e9:3d:cb:

           5d:10:96:17:69:75:cd:71:d5:e5:07:26:93:e8:35:

           ca:e3:49