0

enter image description herei'm having trouble with this c++ code, i'm trying to make it encrypt and decrypt a string after it converts it to ASCII code, problem is when it gets converted to ascii no encryption happens, i'm aware i need to encrypt each of like,3 numbers from the ascii code alone then decrypt them ,but i have no idea how to do that,ive been trying to for two days, here is my code :

#include<iostream>
#include<math.h>
using namespace std;
// find gcd
int gcd(int a, int b) {
   int t;
   while(1) {
      t= a%b;
      if(t==0)
      return b;
      a = b;
      b= t;
   }
}
int main() {
   //2 random prime numbers
   double p ,q,track,e;
   cout<<"entrer p :";
   cin>>p;
   if(p)
   cout<<"entrer q :";
   cin>>q;
   
   double n=p*q;//calculate n
   
   double phi= (p-1)*(q-1);//calculate phi
   
   //public key
   //e stands for encrypt
   cout<<"entrer e :";
   cin>>e;
   
   //for checking that 1 < e < phi(n) and gcd(e, phi(n)) = 1; i.e., e and phi(n) are coprime.
   while(e<phi) {
      track = gcd(e,phi);
      if(track==1)
         break;
      else
         e++;
   }
   //private key
   //d stands for decrypt
   //choosing d such that it satisfies d*e = 1 mod phi
   double d1=1/e;
   string message;
   double d=fmod(d1,phi);
  
   
    char word[32]; 
    int x = 0; 
    cout << "Please enter the word (maximum 32 characters):\n"; 
    cin >> word; 
    cout << "The ASCII for this word is:\n"; 
    while (word[x] != '\0')    // While the string isn't at the end... 
    { 
        cout << int(word[x]);    // Transform the char to int 
        x++; 
    } 
  
    
   double c = pow(int(word[x]),e); //encrypt the message
   double m = pow(c,d);
   c=fmod(c,n);
   m=fmod(m,n);
   cout<<"\n";
   cout<<"Original Message = "<<word;
   cout<<"\n"<<"p = "<<p;
   cout<<"\n"<<"q = "<<q;
   cout<<"\n"<<"n = pq = "<<n;
   cout<<"\n"<<"phi = "<<phi;
   cout<<"\n"<<"e = "<<e;
   cout<<"\n"<<"d = "<<d;
   cout<<"\n"<<"Encrypted message = "<<c;
   cout<<"\n"<<"Decrypted message = "<<m;
   return 0;
}

i'm guessing what it does here is try to encrypt the whole ascii code at once,but it needs to divide it to at least 4parts depending on how long it is,for example here is the otput i get while only compling this code: enter image description here for example in this example, ascii of hello is 104101108108111 but it needs to be divided like 104 101 108 108 111 and then each of these numbers should be encrypted to form an ancrypted message,im not sure how to do it

Tarick Welling
  • 3,119
  • 3
  • 19
  • 44
joxavy
  • 9
  • 8
  • Assuming that's an accurate copy of your code, it looks like you're encrypting the NUL byte at the end of your message (i.e after the loop `word[x] == '\0'`) – Hasturkun Dec 22 '20 at 16:36
  • 1
    RSA is a purely integer type of thing. I haven't tried it but I doubt that computing the inverse in floating point is going to work. Some time ago, I posted a [working demo](https://stackoverflow.com/questions/20111827/various-questions-about-rsa-encryption/20114154#20114154) that may be helpful though. – Jerry Coffin Dec 22 '20 at 18:58
  • @JerryCoffinahh okay thank you^^ – joxavy Dec 22 '20 at 20:47

0 Answers0