CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2017
    Posts
    1

    Question RSA Implementation

    Hi, this is an RSA algorithm implementation in c++. The code runs fine but whenever I included a space in the message to be encrypted, when it decrypts, the space is replaced with a C. Why is this?

    Code:
    #include<iostream>
    #include<cmath>
    #include<cstdio>
    
    using namespace std;
    
    class RSA
    {
           public:
                  long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
                  string msg;
                  RSA(long x, long y, string s)
                  {
                         long i;
                         p = x;
                         q = y;
                         msg = s;
                         n=p*q;
                         t=(p-1)*(q-1);
                         for(i=0;i<msg.size();i++)
                         {
                               m[i] = msg[i];
                         }
                  }
                 
                  int prime(long pr)
                  {
                         int i;
                         j=sqrt(pr);
                         for (i=2;i<=j;i++) {
                               if(pr%i==0)
                                   return 0;
                         }
                         return 1;
                  }
                  void ce()
                  {
                         int k;
                         k=0;
                         for (i=2;i<t;i++)
                         {
                               if(t%i==0)
                                   continue;
                               flag=prime(i);
                               if( flag==1 && i!=p && i!=q)
                               {
                                      e[k]=i;
                                      flag=cd(e[k]);
                                      if(flag>0) {
                                             d[k]=flag;
                                             k++;
                                      }
                                      if(k==99)
                                              break;
                               }
                         }
                         for (i=0;i<j-1;i++)
                         {
                               cout<<(i+1)<<"\t("<<e[i]<<","<<d[i]<<")"<<endl;
                         }
                         cout<<endl;
                  }
                  long int cd(long int x)
                  {
                         long int k=1;
                         while(1) {
                               k=k+t;
                               if(k%x==0)
                                   return(k/x);
                         }
                  }
                  void encrypt(int x)
                  {
                         long int pt,ct,key=e[x-1],k,len;
                         cout<<"Value of e used is "<<key<<endl;
                         i=0;
                         len = msg.size();
                         while(i!=len)
                         {
                               pt=m[i];
                               pt=pt-96;
                               k=1;
                               for (j=0;j<key;j++)
                               {
                                      k=k*pt;
                                      k=k%n;
                               }
                               temp[i]=k;
                               ct=k+96;
                               en[i]=ct;
                               i++;
                         }
                         en[i]=-1;
                         cout<<"The Encrypted Message : "<<endl;
                         for (i=0;en[i]!=-1;i++)
                         cout<<(char)en[i];
                         cout<<endl;
                  }
                  void decrypt(int x)
                  {
                         long int pt,ct,key=d[x-1],k;
                         cout<<"Value of d used is "<<key<<endl;
                         i=0;
                         while(en[i]!=-1)
                         {
                               ct=temp[i];
                               k=1;
                               for (j=0;j<key;j++) {
                                      k=k*ct;
                                      k=k%n;
                               }
                               pt=k+96;
                               m[i]=pt;
                               i++;
                         }
                         m[i]=-1;
                         cout<<"The Decrypted Message : "<<endl;
                         for (i=0;m[i]!=-1;i++)
                         cout<<(char)m[i];
                         cout<<endl;
                  }
    };
    
    
    int main()
    {
           long i,n,t,p,q,x;
           cout<<"Enter the Prime Numbers P & Q"<<endl;
           cin>>p>>q;
           cout<<"Enter the Message"<<endl;
           string msg;
           getchar();
           getline(cin,msg);
           RSA ob(p,q,msg);
           cout<<"Possible values of (e,d) are"<<endl;
           ob.ce();
           cout<<"Chose which values of (e,d) you want to use?"<<endl;
           cin>>x;
           cout<<endl;
           ob.encrypt(x);
           cout<<endl;
           ob.decrypt(x);
           return 0;
    }
    Thanks. Also if you have any tips to help me modify this to make it read from a text file and then decrypt to a new file, let me know!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: RSA Implementation

    What debugging of the code has been done by stepping through the code with the debugger to see where the execution deviates from that expected?

    PS It looks like an issue is with the prime numbers. If they both are > 10 then it seems to work, but if one is < 10 then it seems not to.

    There is also a buffer overflow problem if the entered message has more than 99 characters.
    Last edited by 2kaud; December 8th, 2017 at 05:01 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured