Good day to all
I implemented the RSA encryption algorithm, but it does not work for given p, q, n, e, d.
In order not to write a lot of code, I took two mutually simple p and q, calculated n, took e such that GCD (e, (p-1) * (q-1)) = 1, using the online calculator found the discovery key d which is equal to the inverse of modulo e.
Code:
#include <string> #include <iostream> static int p = 7; static int q = 29; static int n = p * q; static int e = 17; static int d = 89; bool encrypt(std::string& strToEncrypt) { if (strToEncrypt.size() < 1) { std::cerr << "Incorrect string to be encrypted.\n"; return false; } std::string buffStrToEncrypt = strToEncrypt; strToEncrypt.clear(); for (std::size_t i = 0; i < buffStrToEncrypt.size(); i++) { uint64_t k = pow(buffStrToEncrypt[i], e); int var = k % n; std::cout << var << " -- " << (char)var << std::endl; strToEncrypt += var; } return true; } bool decrypt(std::string& strToDecrypt) { if (strToDecrypt.size() < 1) { std::cerr << "Incorrect string to be encrypted.\n"; return false; } std::string buffStrToDecrypt = strToDecrypt; strToDecrypt.clear(); for (std::size_t i = 0; i < strToDecrypt.size(); i++) { uint64_t k = pow(buffStrToDecrypt[i], d); int var = k % n; std::cout << var << " -- " << (char)var << std::endl; strToDecrypt += var; } return true; } void main() { std::string str = "Hello"; std::cout << "Inputed: " << str << std::endl; encrypt(str); std::cout << "Encrypted: " << str << std::endl; decrypt(str); std::cout << "Decrypted: " << str << std::endl << std::endl; system("pause"); } But the algorithm does not work.
Here is what is displayed on the screen:
Inputed: Hello 99 -- c 99 -- c 99 -- c 99 -- c 99 -- c Encrypted: ccccc Decrypted:
Can someone tell me why it doesn't work?