3

I am working on a small project with C language , it's an RSA text encrypting .

The code works but the problem is that it doesn't work if i choose a large key . I think that the problem is due to the modular arithmetic but i can't find the solution.

There is the code of the 2 functions :

ENCRYPT

     unsigned int crypt( unsigned int mchiff, unsigned int n,unsigned int e)
{
      unsigned int i;
      double cc=1;   
      printf("\n\n\n");
        for(i=0;i<e;i++)
    {
       cc=cc*mchiff;
       printf(" : %g :    ",cc);
       cc=fmod(cc,n);
       printf(" < %g > \n",cc);
    }
      printf("\n\n\n");
      return cc;
}

DECRYPT

unsigned long int decrypt(long cc,int n,int d)
{
      int i;
      unsigned long int cd=1;
        for(i=0;i<d;i++)   /* the main problem is here if the d is apprx equal to 2^1024 */
    {
       cd=cd*cc;
       cd=cd%n;
    }
      return cd;
}
4

1 回答 1

1

int仅限于 32 位(通常)。您需要一种可以容纳 1024 位或更多位的类型。为此,您应该使用像GNU MP这样的外部库。

如果您不想学习 RSA 而只是尝试使用,我会选择现有的实现来为您省去麻烦。OpenSSL拥有一切。

于 2013-04-27T17:21:52.387 回答