i'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:
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