gmp RSA算法实现问题

发布于 2022-09-04 17:25:58 字数 4823 浏览 23 评论 0

前言
--我正在学习RSA加密算法。查了很多资料后写了以下代码,成功解密。
--后来我想试试看RSA为什么安全(RSA原理方面还是理解不透彻),如果没有问题之后再深入理解数学方面的东西。

--不过问题来了。在相同公匙的情况下,我计算有没有下一个私匙。计算出来一个,还解密成功了。不是应该一个私匙对应一个公匙的吗?如果不是怎么能保证安全?

--请路过的前辈指教一下,本人刚学满一年,这问题还是有点太难了

代码

// Module
 // System
  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <limits.h>
  #include <errno.h>
  #include <string.h>
  #include <time.h>
  #include <math.h>
  #include <gmp.h>
  // sys
   #include <sys/time.h>
   #include <sys/file.h>
 // Homemade
  #include "/root/01_Code/03_C/00_Module/output_option.h"
  //
// main()
 int main(void){
  // Value
   // Basic
    long   Loop  [1];
    time_t Seed;
    long   Level = 8;
    gmp_randstate_t Random_State; gmp_randinit_default(Random_State);
   // RSA
    mpz_t Buffer_mpz_1; mpz_init(Buffer_mpz_1);
    mpz_t Buffer_mpz_2; mpz_init(Buffer_mpz_2);
    mpz_t Prime_1;      mpz_init(Prime_1);
    mpz_t Prime_2;      mpz_init(Prime_2);
    mpz_t Phi;          mpz_init(Phi);
    mpz_t Common_Key;   mpz_init(Common_Key);
    mpz_t Public_Key;   mpz_init(Public_Key);
    mpz_t Private_Key;  mpz_init(Private_Key);
  // Implementation
   // RSA init
    // Prime_1
     sleep(1);
     time(&Seed);
     gmp_randseed_ui(Random_State, Seed);
     mpz_urandomb(Prime_1, Random_State, Level);
     while(mpz_probab_prime_p(Prime_1, 49) != 2)
      mpz_add_ui(Prime_1, Prime_1, 1);
    // Prime_2
     sleep(1);
     time(&Seed);
     gmp_randseed_ui(Random_State, Seed);
     mpz_urandomb(Prime_2, Random_State, Level);
     while(mpz_probab_prime_p(Prime_2, 49) != 2)
      mpz_add_ui(Prime_2, Prime_2, 1);
    // Phi
     mpz_sub_ui(Phi,          Prime_1, 1);
     mpz_sub_ui(Buffer_mpz_1, Prime_2, 1);
     mpz_mul(   Phi,          Phi,     Buffer_mpz_1);
    // Common_Key
     mpz_mul(Common_Key, Prime_1, Prime_2);
     //
    // Public_Key
     // Init random Public_Key
      do{
       sleep(1);
       time(&Seed);
       gmp_randseed_ui(Random_State, Seed);
       mpz_urandomb(Public_Key, Random_State, Level);
      }while(mpz_cmp(Public_Key, Phi) >= 0 || mpz_cmp_ui(Public_Key, 1) == 0);
      mpz_set(Buffer_mpz_2, Public_Key);
     // Pick Public_Key
      while(1){
       if(mpz_cmp(Public_Key, Phi) < 0){
        mpz_gcd(Buffer_mpz_1, Public_Key, Phi);
        if(mpz_cmp_ui(Buffer_mpz_1, 1) == 0) break;
        mpz_add_ui(Public_Key, Public_Key, 1);
       }else{
        mpz_set(Public_Key, Buffer_mpz_2);
        while(1){
         mpz_sub_ui(Public_Key, Public_Key, 1);
         if(mpz_cmp_ui(Public_Key, 1) == 0){
          printf("%d\n", __LINE__);
          mpz_clear(Buffer_mpz_1);
          mpz_clear(Buffer_mpz_2);
          mpz_clear(Prime_1);
          mpz_clear(Prime_2);
          mpz_clear(Phi);
          mpz_clear(Common_Key);
          mpz_clear(Public_Key);
          mpz_clear(Private_Key);
          return 1;
         }
         mpz_gcd(Buffer_mpz_1, Public_Key, Phi);
         if(mpz_cmp_ui(Buffer_mpz_1, 1) == 0) goto l_END_PK;
        }
       }
      }
      l_END_PK:;
    // Private_Key
     mpz_set_ui(Buffer_mpz_1, 1);
     mpz_set_ui(Private_Key,  2); char Switch = 'V';
     l_AGAIN:do{
      mpz_add_ui(Private_Key, Private_Key, 1);
      mpz_mul(Buffer_mpz_1, Public_Key,   Private_Key);
      mpz_mod(Buffer_mpz_1, Buffer_mpz_1, Phi);
     }while(mpz_cmp_ui(Buffer_mpz_1, 1) != 0);
     if(Switch == 'V'){
      Switch = 'X';
      mpz_set(Buffer_mpz_2, Private_Key);
      goto l_AGAIN;
     }
   // Encrypt & Decrypt
    mpz_set_ui(Buffer_mpz_1, 88);
    mpz_powm(Buffer_mpz_1, Buffer_mpz_1, Public_Key,  Common_Key);
    mpz_powm(Buffer_mpz_1, Buffer_mpz_1, Private_Key, Common_Key);
    gmp_printf("Prime_1     %Zd\n", Prime_1);
    gmp_printf("Prime_2     %Zd\n", Prime_2);
    gmp_printf("Public_Key  %Zd\n", Public_Key);
    gmp_printf("Private_Key %Zd\n", Private_Key);
    gmp_printf("Common_Key  %Zd\n", Common_Key);
    gmp_printf("Decrypted   %Zd\n", Buffer_mpz_1);
    printf("----------------------------------------------------\n");
    mpz_set_ui(Buffer_mpz_1, 88);
    mpz_powm(Buffer_mpz_1, Buffer_mpz_1, Public_Key,   Common_Key);
    mpz_powm(Buffer_mpz_1, Buffer_mpz_1, Buffer_mpz_2, Common_Key);
    gmp_printf("Prime_1     %Zd\n", Prime_1);
    gmp_printf("Prime_2     %Zd\n", Prime_2);
    gmp_printf("Public_Key  %Zd\n", Public_Key);
    gmp_printf("Private_Key %Zd\n", Buffer_mpz_2);
    gmp_printf("Common_Key  %Zd\n", Common_Key);
    gmp_printf("Decrypted   %Zd\n", Buffer_mpz_1);
  // Clean up
   mpz_clear(Buffer_mpz_1);
   mpz_clear(Buffer_mpz_2);
   mpz_clear(Prime_1);
   mpz_clear(Prime_2);
   mpz_clear(Phi);
   mpz_clear(Common_Key);
   mpz_clear(Public_Key);
   mpz_clear(Private_Key);
   return 0;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文