Homework help - File encrpytion/decryption problem...

Discussion in 'C++' started by Chad, May 8, 2012.

  1. Chad

    Chad Guest

    The following program is supposed to encrypt and decrpyt a file. The
    professor told use that we have to use XOR (exclusive or) when
    encrypting and decrypting the file.

    //Homework 8, question 2

    #include <iostream>
    #include <math.h>
    #include <string>
    #include <fstream>
    #include <cstring>
    #define MAX 40
    #define HASHSIZE 101
    using namespace std;

    unsigned hash(char *s)
    {
    unsigned hashval;
    for (hashval = 0; *s; s++)
    {
    hashval = *s + 31 * hashval;
    }

    return hashval % HASHSIZE;
    }

    int main(void)
    {
    unsigned int value;
    char epassword[MAX];
    char dpassword[MAX];
    char inputFilename[MAX];
    char outputFilename[MAX];
    char c;

    cout << "Enter a password: ";
    cin.getline(epassword, MAX);
    cout << "Enter an input filename: ";
    cin >> inputFilename;
    cout << "Enter an output filename: ";
    cin >> outputFilename;

    srand(hash(epassword));
    value = rand();
    //value = hash(epassword);

    ifstream IS(inputFilename, ios::in);
    ofstream OS(outputFilename, ios::eek:ut);

    if (!IS) exit(1);
    if (!OS) exit(1);

    //encrpyt the file
    while (IS.read(&c, 1))
    {
    c = value ^ c;
    OS << c;
    }

    IS.close();
    OS.close();

    cout << "\nThe encrypted file is\n...";
    ifstream IS1(outputFilename, ios::in);
    if (!IS1) exit(1);

    //display the encrypted file
    while (IS1 >> c) cout << c;

    cout << endl;
    IS1.close();

    while (1)
    {
    cout << "Enter the password to decrpyt the file: ";
    cin >> dpassword;
    if( strcmp(dpassword, epassword) == 0) break;
    else cout << "Invalid password\n";
    }
    ifstream IS2(outputFilename, ios::in);
    if (!IS2) exit(1);

    //display the decrypted file
    while (IS2 >> c)
    {
    c = c ^ value;
    cout << c;
    }
    cout << endl;

    IS2.close();
    //system("pause");
    return 0;
    }

    My input.dat file is..
    This is some text

    The program appears to works fine for some passwords like "sex" and
    "alcohol". However, when I use a password like "underagegirls", the
    decrpyted file, output.dat comes out as..

    This is se text


    Why is the output getting truncated?

    Chad
    Chad, May 8, 2012
    #1
    1. Advertising

  2. Chad

    Luca Risolia Guest

    On 08/05/2012 16:28, Chad wrote:
    > The following program is supposed to encrypt and decrpyt a file. The
    > professor told use that we have to use XOR (exclusive or) when
    > encrypting and decrypting the file.


    I wrote a simple example showing encryption and decryption using some
    manipulators for istream's and ostream's. The encryption algorithm uses
    the xor operator on a given passphrase. You may want to look at it to
    have an idea:

    http://www.linux-projects.org/listing/cpp_solutions/21.22/main.cpp

    Because of the reasons Paavo Helde has mentioned in a previous post,
    these (simple) manipulators have to keep track of the number of chars
    encrypted or to decrypt.

    I hope it helps.
    Luca Risolia, May 9, 2012
    #2
    1. Advertising

  3. Chad <> wrote:
    > //encrpyt the file
    > while (IS.read(&c, 1))
    > {
    > c = value ^ c;
    > OS << c;
    > }


    As a side note, I hope you understand that this is approximately the
    poorest form of "encryption" possible.
    Juha Nieminen, May 9, 2012
    #3
  4. Chad

    osmium Guest

    "Juha Nieminen" wrote:

    > Chad <> wrote:
    >> //encrpyt the file
    >> while (IS.read(&c, 1))
    >> {
    >> c = value ^ c;
    >> OS << c;
    >> }

    >
    > As a side note, I hope you understand that this is approximately the
    > poorest form of "encryption" possible.


    To the OP:
    When you get this working perhaps you could post a brief message and Juha
    could break it for us.thus demonstrating just how poor the method is.
    osmium, May 9, 2012
    #4
  5. osmium <> wrote:
    > "Juha Nieminen" wrote:
    >
    >> Chad <> wrote:
    >>> //encrpyt the file
    >>> while (IS.read(&c, 1))
    >>> {
    >>> c = value ^ c;
    >>> OS << c;
    >>> }

    >>
    >> As a side note, I hope you understand that this is approximately the
    >> poorest form of "encryption" possible.

    >
    > To the OP:
    > When you get this working perhaps you could post a brief message and Juha
    > could break it for us.thus demonstrating just how poor the method is.


    Well, it's completely trivial because there are only 256 possible
    encryption keys. Just "decrypt" the message with all of them and look
    which one results in clear text. (If the message were very large, you
    wouldn't even need to decrypt it all. It would be enough to decrypt
    the first few dozens of bytes, and see if it forms readable text.)

    Even if that weren't possible, it would still be quite easy in another
    way: Since a space is the most common character in a written piece of text,
    just look at a value that repeats in average each 3 to 6 bytes in the
    "encrypted" text, calculate its XOR with the character ' ' and then use
    that to decrypt the entire message.

    This form of "encryption" is incredibly poor.
    Juha Nieminen, May 10, 2012
    #5
  6. Chad

    Guest

    On Thursday, May 10, 2012 1:49:50 AM UTC-5, Juha Nieminen wrote:
    > osmium <> wrote:
    > > "Juha Nieminen" wrote:
    > >
    > >> Chad <> wrote:
    > >>> //encrpyt the file
    > >>> while (IS.read(&c, 1))
    > >>> {
    > >>> c = value ^ c;
    > >>> OS << c;
    > >>> }
    > >>
    > >> As a side note, I hope you understand that this is approximately the
    > >> poorest form of "encryption" possible.

    > >
    > > To the OP:
    > > When you get this working perhaps you could post a brief message and Juha
    > > could break it for us.thus demonstrating just how poor the method is.

    >
    > Well, it's completely trivial because there are only 256 possible
    > encryption keys. Just "decrypt" the message with all of them and look
    > which one results in clear text. (If the message were very large, you
    > wouldn't even need to decrypt it all. It would be enough to decrypt
    > the first few dozens of bytes, and see if it forms readable text.)
    >
    > Even if that weren't possible, it would still be quite easy in another
    > way: Since a space is the most common character in a written piece of text,
    > just look at a value that repeats in average each 3 to 6 bytes in the
    > "encrypted" text, calculate its XOR with the character ' ' and then use
    > that to decrypt the entire message.
    >
    > This form of "encryption" is incredibly poor.


    I've been wondering what people think of this:
    http://www.cprogramming.com/tutorial/xor.html



    Ebenezer Enterprises
    http://webEbenezer.net
    , May 11, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drs

    encryption/decryption help

    drs, Jan 12, 2005, in forum: Python
    Replies:
    4
    Views:
    498
    Jorgen Grahn
    Jan 12, 2005
  2. Philippe C. Martin

    Re: encryption/decryption help

    Philippe C. Martin, Jan 12, 2005, in forum: Python
    Replies:
    1
    Views:
    297
  3. imlalit
    Replies:
    0
    Views:
    237
    imlalit
    Aug 7, 2009
  4. Venkat Bagam

    file encryption/decryption needed

    Venkat Bagam, Sep 18, 2007, in forum: Ruby
    Replies:
    3
    Views:
    100
    Venkat Bagam
    Sep 19, 2007
  5. ThePacific
    Replies:
    0
    Views:
    357
    ThePacific
    Sep 26, 2012
Loading...

Share This Page