### RSA Algorithm C++ Implementation

Hi guys ... i am having a serious problem here..... i will be thankfull if you realy help me out.
Actually i am writing RSA algorithm in c++. Below is the code that i have written This generates the public and private keys correctly and encrypts the string but it does not decrypt correctly. Please help me out ... Please it is really important for me........

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170`` `````` #include #include #include #include #include #include #include using namespace std; int p, q, n, e = 7, d, tot; string text = "Hello"; //Checks if the number is prime or not bool isPrime(int a) { for (int i = 2; i < a / 2; i++) { if (a % i == 0) return false; } return true; } void setkeys() { int num = 11; srand(time(0)); p = rand() % num + 5; while (!isPrime(p)) { p = rand() % num + 5; } q = rand() % num + 5; while (!isPrime(q) || q == p) { q = rand() % num + 5; } /*q = 11; p = 17;*/ n = p * q; tot = (p - 1) * (q - 1); e = 7; } int LCD(int a, int b) { int total = 1; if (b <= 0) return a; else { int q = a / b; int r = a - q * b; a = b; b = r; LCD(a, b); } } int calcNegativeMod(int n1, int mod) { n1 = abs(n1); int i = 1; while (mod * i < n1) i++; int ans = mod * i - n1; return ans; } int findE() { int er = 2; while (LCD(er, tot) != 1) er++; return er; } int findIn(int n, int phi) { int r = 1; for (int i = 1; i < phi; i++) { if ((i * n) % phi == 1) { r = i; break; } } cout << "d is " << r << endl; return r; } string encrypt(string s) { e = findE(); cout << "Public key is " << e << endl; string dec = ""; char c = '\0'; for (int i = 0; i < s.length(); i++) { int h = (int)s.at(i); long long a = ((long long)pow(h, e)); a = a % n; if (a < 0) a = calcNegativeMod(a, n); c = a; dec += c; c = '\0'; } return dec; } string decrypt(string s) { string dec = ""; char *c1 = new char[s.length()]; d = findIn(e, tot); char c = '\0'; for (int i = 0; i < s[i] != NULL; i++) { int h = s[i]; long long a = ((long long)pow(h, d)); a = a % n; if (a < 0) a = calcNegativeMod(a, n); c = a; dec += c; c1[i] = (char)a; c = '\0'; } return dec; } int main() { setkeys(); cout << " P is " << p << " \n Q is " << q << endl; //d = findIn(e, tot); string en = encrypt("Salman"); string dec = decrypt(en); cout << " tot is " << tot << endl; cout << "Encrypted String is " << en << endl; cout << endl; cout << "Decrypted String is " << dec << endl; system("pause"); }``````
I have not tried to implement the RSA before, but here's an issue I noticed, there may be more.

 ``3031323334353637383940414243444546`` ``````void setkeys() { int num = 11; //... while (!isPrime(p)) { p = rand() % num + 5; } //... while (!isPrime(q) || q == p) { q = rand() % num + 5; } //... n = p * q; //... }``````

Both p and q are in the range [5, 13] (14 and 15 are not prime so don't count). That means that n is in the range [35, 143] (5 * 7 and 11 * 13).

 ``103104105106107108109110`` ``````string encrypt(string s) { //... int h = (int)s.at(i); long long a = ((long long)pow(h, e)); a = a % n; //... }``````

^ Encryption is only supposed to work when 0 <= h < n. The ASCII values of alphanumeric characters are in the range [48,122].

http://www.asciitable.com/

Most of the values you have for n would be too small to work correctly for encryption. You should attempt to find n so that it is larger than the highest possible ASCII value for your input.

I also think there's something wrong with your LCD function (shouldn't it be named GCD?). Line 66 should be `return LCD(a, b);`.
Last edited on
So what's going on here? (line 135, in your decrypt() function)

`for (int i = 0; i < s[i] != NULL; i++)`

Andy
Topic archived. No new replies allowed.