Why does the RSA encryption algorithm not work?

Good day to all

I implemented the RSA encryption algorithm, but it doesn't work under the given p, q, n, e, d.

In order not to write a lot of code, I took two mutually simple p and q, counted n, took e such that the NODE (e, (p-1)*(q-1)) = 1, using an online calculator, I found the key d, which is equal to the inverse number 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 doesn't work.

Here is what is output on 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 ?

 0
Author: Yuriy Lisovskiy, 2017-07-15

1 answers

I don't even try to understand all the code... That's enough:

uint64_t k = pow(buffStrToEncrypt[i], e);

Let's say the character is just Latin A - i.e. 65. e you have 17. The total is 65^17, which is approximately 6.6e30. You're trying to stick it in a 64-bit integer... With d=89, things are still much worse...

Not to mention the fact that encrypting text by character is essentially using a wildcard cipher, which was famously handled back when the abacus was the coolest computer :)

 2
Author: Harry, 2017-07-15 15:52:28