I have this RSA encryption code but it doesnt encrypt the whole message its suppose to encrypt. It only encrypts

#include <iostream>
#include <string>
#include <string.h>

using namespace std;

// prime numbers
int p, q;
//compute
int n ;//= p * q;
//totient
int phi;// = (p - 1) * (q - 1);

int e; // not devider of phi;
int d;//= e mod phi;

int main()
{
    p = 3;
    q = 7;
    phi = (q - 1) * (p - 1);
    e = 69;
    d = (e % phi);
    n = p * q;

    long long c, dc, m;

    string msg, msg2, msg3;
    msg = "hallo";
    int i;
    for(i = 0; i < msg.size(); i++)
    {
        m = msg[i];
        cout<<m;
    }

    c = m ^ e % d;

    cout<<c<<endl;


    dc = c ^ d & n;

    cout<<char(dc);

}

What am I doing wrong and what can i do to fix it?

Recommended Answers

All 5 Replies

What is your intended output?

My current output is just 'h'
it is suppose to output the entire message: 'hallo'

This tells me that it only encrypts the first letter of the word thus when decrypted it would show only the first letter.

I dont know how fix it so that the entire word can be encrypted.
And it apears that there are something wrong with my keys also as when i try to encrypt another word the letter isnt the same as the leter of the other word.

I see you're using the XOR operator here:

m ^ e

Did you mean to do that, or are you looking for the pow function?

I see that at the end you output a single char only. Is that where you're expecting to see all the char output?

Do you mean something like this? (No idea if encryption is right. Just made whatever you did apply to the whole string)

#include <iostream>
#include <string>
using namespace std;

// prime numbers
int p, q;
//compute
int n ;//= p * q;
//totient
int phi;// = (p - 1) * (q - 1);
int e; // not devider of phi;
int d;//= e mod phi;

int main()
{
    p = 3;
    q = 7;
    phi = (q - 1) * (p - 1);
    e = 69;
    d = (e % phi);
    n = p * q;
    long long c, dc, m;
    string msg, msg2, msg3;
    msg = "hallo";
    int i;

    cout << "Original line:\n";

    // Print the ASCII values of the message
    for(i = 0; i < msg.size(); i++)
    {
        // Show the character it is based on.
        cout<< static_cast<int>(msg[i]) << "(" << msg[i] << ") " ;
    }

    cout << "\nEncrypted line:\n";

    // Show the encrypted bytes
    for (i = 0; i < msg.size(); i++)
    {
        c = msg[i] ^ e % d;
        dc = c ^ d & n;
        cout << dc << "(" << static_cast<char>(dc) << ") ";
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.