Search code examples
crsa

A 1024 bits RSA algorithm with C


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;
}

Solution

  • int is limited to 32-bit (usually). You want a type that can hold 1024 bits or more. You should use an external library like GNU MP for that.

    And if you're not trying to learn about RSA and just trying to use, I'd just go with an existing implementation to save you the trouble. OpenSSL has everything.