I just have a program to implement RSA encryption, and it runs but when I try to encrypt a message it crashes. When I bebug it won't show any error, What is the issue,How Can I fix the program?

#include<iostream>
#include<cmath>
using namespace std;
#define MAXLENGTH 500  

int size = 0;
int p, q;   //the two prime numbers
int n, phi; //n = p * q,phi = (p-1) * (q-1) 
int e;      //{e, n}the public key
int d;      //{d, n}the private key
int i, j ;
int clear[MAXLENGTH], Ciphertext[MAXLENGTH];//used to store add // dense before the bright // text and encrypted ciphertext
int DecryptionText[MAXLENGTH];//Store the decrypted plaintext


void Encryption()
{
    //encrypt method
    cout << " enter two prime number p and q:" ;
    cin >> p >> q ;
    cout << " p = " << p << ", q = " << q << endl;
    n = p * q;//compute  n,
    phi = (p - 1) * ( q - 1 );//compute the totient of the product
    cout << " n = " << n << ", phi = " << phi << endl;
    cout << " choose number between[0," << phi - 1 << "] " << phi << " coprime to 3120:";
    cin >> e;
    float d0;
    for( int i = 1; ; i++)
    {///compute modular multiplicative inverse of  e * d ≡ 1 (mod phi) 
        d0 = (float)(phi*i+1) / e;
        if( d0 - (int)d0 == 0 )
            break;
    }
    d = (int)d0;
    cout << endl;
    cout << " e = " << e << ", d = " << d << endl;
    cout << " The public key is {e,n} = {" << e << "," << n << "}" << endl;
    cout << " The private key is {d,n} = {" << d << "," << n << "}" << endl;
    cout << endl;

    cout << " enter a prime number to encrypt less than " << n << " positive integer( -1 to end):" << endl;
    cout << " The plaintext before encryption:";
    for( i = 0; i < MAXLENGTH; i++)
        Ciphertext[i] = 1;
    int count;
    for(int j = 0; j<MAXLENGTH; j++)
    {
        cin >> clear[j];
        if( clear[j] == -1 )
            break;
        count = e;
        while(count > 0)
        {
            //to encrypt Ciphertext =(clear)^ e mod n
            Ciphertext[j] = (Ciphertext[j] * clear[j]) % n;

            count-- ;
        }
    }
    cout << " the Ciphertext is:" ;
    size = j;//the length of ciphertext
    for(int k=0; k<j; k ++)
        cout << Ciphertext[k] << " ";
    cout << endl ;
}

void Decryption()
{
    //decrypt method
    for(int i = 0; i < MAXLENGTH; i++)
        DecryptionText[i] = 1;
    int count;
    for(int j = 0; j < size; j++)
    {
        count = d;
        while(count > 0)
        {//compute DecryptionText =(Ciphertext)^ d (mod n)
            DecryptionText[j] = ((DecryptionText[j] * Ciphertext[j]) %n);
            count-- ;
        }
    }

    cout << " :";
    int k;
    for( int k = 0; k < size; k ++)
        cout << DecryptionText[k] << " ";
    cout << endl ;
        cout << " The plaintext before encryption:";
    for( k = 0; k < size; k++)
        cout << clear[k] << " ";
    cout << endl;
}
void main()
{
    Encryption();
    char c;
    cout << endl;
    cout << "want decrypt (Y or N): ";
    cin >> c;
    if(c == 'y' || c == 'Y')
        Decryption();
    else
        return ;
}

Try running it in a debugger so you can get a stack trace when it crashes. Then, you can look at where it crashed, and what your variables were set to. This sort of problem is often due to improperly initialized variables, or buffer overruns.

It looks to me that without an upper limit to i in line 28 you have the potential for an infinite loop

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.