I have this code and when ever i try to put in the value 'e' it doesnt want to take it, the program freses.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int encrypt();
int decrypt();

int phi, M, n, e, d, C, N, D, answer, p, q, s;
string line, line2, ofile, ifile;

int main()
{
    cout<<"1 enc"<<endl;
    cout<<"2 dec"<<endl;
    cin>>answer;

    if(answer == 1)
    {
        encrypt();
    }
    else
        decrypt();

    return 0;
}

int encrypt()
{
    cout<<"Enter two prime numbers (seperated by a space): ";
    cin>>p>>q;

    n = p * q;
    phi = (p - 1) * (q - 1);
    cout<<"F(n): "<<phi<<endl;;

    cout<<"Enter e: ";
    cin>>e;

    do
    {
        d = 1;
        s = (d * e) % phi;
    }while(s != 1);

    d = d - 1;
    cout<<"Public key: ["<<e<<","<<n<<"]"<<endl;
    cout<<"Private key: ["<<d<<","<<n<<"]"<<endl;

    cout<<"Enter input file: ";
    getline(cin, ifile);
    cout<<"Enter output file: ";
    cin.ignore();

    ifstream infile(ifile.c_str());
    ofstream outfile;

    outfile.open(ofile.c_str(), ios::app);

    while(getline(infile, line))
    {
        for(int i = 0; i < line.size(); i ++)
        {
            line2 += (line[i] * e) % n;
            outfile<<line2;
        }
        outfile<<""<<endl;
    }

    cout<<"Done!"<<endl;
    return 1;
}

int decrypt()
{
    cout<<"Enter d: ";
    cin>>N;
    cout<<"Enter n: ";
    cin>>D;
    cout<<"Enter input file: ";
    getline(cin, ifile);
    cout<<"Enter output file: ";
    cin.ignore();

    ifstream infile(ifile.c_str());
    ofstream outfile;

    outfile.open(ofile.c_str(), ios::app);

    while(getline(infile, line))
    {
        for(int i = 0; i < line.size(); i ++)
        {
            line2 += (line[i] * d) % n;
            outfile<<line2;
        }
        outfile<<""<<endl;
    }

    cout<<"Done!"<<endl;
    return 1;
}

Also if you would be so kind to maby make the code a bit better RSA encryption, thanks.

Line 41 to 45, since you don't change d, e or phi in the loop and since d is always 1 the value of s is constant with-in the loop and if e % phi is not equal to 1 the loop will never exit.

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.