hi i am getting a floating point error while im trying to complile the rc4 encryption in c++...i was wondering if anyone could help me out...i have the code pasted as below

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

unsigned char s[256];
unsigned int i, j;

void swap(unsigned char *s, unsigned int i, unsigned int j)
{
   unsigned char temp = s[i];
   s[i] = s[j];
   s[j] = temp;
}

void rc4_KSA (const char *key, unsigned int keylen)
{
    int a=0, b=0;
    for (a = 0; a < 256; a++)
    {
        s[a] = a;
    }

    for (a = 0, b = 0; a < 256; a++)
    {
        b = (b + key[a % keylen ] + s[a]) % 256;
        swap(s, a , b);
    }
}

unsigned char rc4_PRGA()
{
    i = (i + 1) % 256;
    j = (j + s[i]) % 256;
    swap(s, i, j);
    return s[(s[i] + s[j]) % 256];
}

int main(int argc, char* argv[] )
{
    string input;
    string plainText;
    do {
        cout << "Please enter a 16 character key (signifying 16 bytes):";
        cin >> input;
        if (input.length() > 16)
        {
            cout << "Entered key is more than 16 characters." <<endl;
        }
        else
        {
            break;
        }
    } while (true);

    const char* key = input.c_str();

    //create the keystream
    rc4_KSA(key, strlen(key));
    cout << "Generated Key stream:" << endl;
    for (int t = 0; t < 256; t++)
    {
        cout << s[t];
    }
    cout << endl;

    //plaintext is taken from user.
    cout << "Enter plain text (upto 1024 characters long):";
    cin >> plainText;

    int size = plainText.length();
    //allocate new memory blocks to hold the data
    const char* memblock = plainText.c_str();
    char* enblock = new char [size];

    //encrypt 
    for (int x = 0; x < size; x++)
    {
        enblock[x] = (memblock[x] ^ rc4_PRGA());
    }

    cout << "Generated Encrypted stream:" << endl;
    for (int y = 0; y < size; y++)
    {
        cout << enblock[y];
    }
    cout << endl;
    system("pause");

    //reset keyStream (s array)
    rc4_KSA(key,strlen(key));

    //decrypt
    for (int x = 0; x < size; x++)
    {
        enblock[x] = (enblock[x] ^ rc4_PRGA());
    }

    //print out the plaintext
    for (int x = 0; x < size; x++)
    {
        cout << enblock[x];
    }
    */

    delete[] memblock;
    delete[] enblock;
    return 0;
}

Recommended Answers

All 6 Replies

What compiler are you using? I don't get a floating point error.

In other news, you have an unmatched block comment end on line 104:

*/

i used codepad.org to compile...

the unmatched block seems to be a typo..

Ah, I see now. Without the typo, it actually compiles just fine. It's when codepad.org tries to run the program that there's a problem.

From the "output" section:

Please enter a 16 character key (signifying 16 bytes):
Floating point exception

My guess is that codepad.org is breaking when it gets to cin >> input--it looks like the site doesn't do interactive input.

If you want to make it work there, replace any cin lines with string literals. You'll also probably run into problems with system("pause").

Alternatively, you could set up a compiler on your local machine and run it from there.

thanks man....will try running it on a compiler on a local machine and will see how it goes....will give you a shout if i get stuck...apprecaite the help..

My guess is that codepad.org is breaking when it gets to cin >> input--it looks like the site doesn't do interactive input.

If you want to make it work there, replace any cin lines with string literals. You'll also probably run into problems with system("pause").

If the site doesn't do standard C++ commands like cin what makes you think it would do a system specific O/S calls like system("pause")?

The mind boggles... 8o]

If the site doesn't do standard C++ commands like cin what makes you think it would do a system specific O/S calls like system("pause")?

The site's author explains: "The strategy is to run everything under ptrace, with many system calls disallowed or ignored." Doesn't say which ones are which--system("pause") may well do something other than murder the running program. Maybe it throws an exception, maybe it simply returns a nonzero value. I didn't try it myself, so I don't know, and I'd rather not assume.

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.