Thread: Encryption program.... just doesn't work.

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    52

    Encryption program.... just doesn't work.

    Yeah.....
    It compiles fine, so I don't know what's wrong with it.

    Don't steal my source.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    //Pretty self-explanatory.
    //Reminders: 0 is success, other is error.
    //(I get those mixed up.....)
    //1 is true, 0 is false
    
    void showusage(char* progname) {
      printf(
      "Usage: %s [options] file\n"
      "  -e = encrypt file\n"
      "  -d = decrypt file\n"
      "  -v = value to use for encryption / decryption.\n"
      "    default is 3.\n"
      "  -h = print this help page\n"
      "  OPTIONS ARE CASE SENSITIVE\n"
      "  Example:\n"
      "  %s -e -v 25 blah.txt\n", *progname, *progname);
    }
    
    int encrypt(int blah, int val) {
      if ((blah + val) > 255) {
        return (blah + val - 255);
      }
      else {
        return blah + val;
      }
    }
    
    int decrypt(int blah, int val) {
      if ((blah - val) < 0) {
        return (blah - val + 255);
      }
      else {
        return blah - val;
      }
    }
    
    int main(int argc, char *argv[]) {
      int value, action;
      FILE *file;
      int optnum;
      char readfromfile, writetofile;
      fpos_t position;
      for (optnum = 1; argc - 2 > optnum; optnum++) {
        switch (argv[optnum][2]) {
          case 'e':
            action = 1;
            break;
          case 'E':
            action = 1;
            break;
          case 'd':
            action = 0;
            break;
          case 'D':
            action = 0;
            break;
          case 'v':
            value = atoi(argv[(optnum + 1)]);
            optnum++;
            break;
          case 'V':
            value = atoi(argv[(optnum + 1)]);
            optnum++;
            break;
          case 'h':
            showusage(argv[0]);
            break;
          case 'H':
            showusage(argv[0]);
            break;
          default:
            showusage(argv[0]);
            break;
          }
      }
      file = fopen(argv[(argc - 1)], "w+");
      if (file == NULL) {
        showusage(argv[0]);
        exit(1);
      }
      while (!feof(file)) {
        fgetpos(file, &position);
        readfromfile = fgetc(file);
        fsetpos(file, &position);
        if (action == 1) {
          writetofile = encrypt((int)readfromfile, value);
        }
        if (action == 0) {
          writetofile = decrypt((int)readfromfile, value);
        }
        fputc(writetofile, file);
      }
      if (fclose(file) == 0) {
        return 0;
        exit(0);
      }
      return 1;
    }

    ...yeah....

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    oops, i forgot to ask my question:

    just what is wrong with this?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Why are you trying to make it harder by reading and writing the same file?

    Between writing the file, and reading it again, you should call fflush()

    > while (!feof(file))
    Why you should'nt use feof() in control loops is in the FAQ

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Asking, just "what is wrong with it" is too broad a question.
    Some might rightly say that the method of encryption is too weak, and they'd be quite correct, but that's probably not what you are considering is the problem.

    Putting it another way, what is the program doing that you don't believe is correct? (You shouldn't be expecting anyone to run the program in order to figure out what you should be telling us )

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You didn't forget.
    Don't steal my source.
    We only steal things worth stealing.
    Code:
    switch (argv[optnum][2]) {
    Are you sure that's what you want? Assuming all your arguments are like "-x", you'll want [1]. Remember, arrays start counting at zero.
    Code:
    void showusage(char* progname) {
      printf(
      "Usage: %s [options] file\n"
      "  -e = encrypt file\n"
      "  -d = decrypt file\n"
      "  -v = value to use for encryption / decryption.\n"
      "    default is 3.\n"
      "  -h = print this help page\n"
      "  OPTIONS ARE CASE SENSITIVE\n"
      "  Example:\n"
      "  %s -e -v 25 blah.txt\n", *progname, *progname);
    }
    Change your *prognames to prognames.

    Code:
          case 'h':
            showusage(argv[0]);
            break;
          case 'H':
            showusage(argv[0]);
            break;
          default:
            showusage(argv[0]);
            break;
    You can use the fall-through behavior of switchs to your advantage:
    Code:
          case 'h':
          case 'H':
          default:
            showusage(argv[0]);
    (And the last case doesn't need a break.)

    Code:
    while (!feof(file)) {
    This is not good. See the FAQ.

    You open file for mode "w+", which truncuates the file, and then try to read from it. There will never be anything in it because you've just erased it.

    Code:
      if (fclose(file) == 0) {
        return 0;
        exit(0);
      }
    That is completely redundant. The exit(0) will never happen, so why do you have it in in the first place?
    Code:
    //Reminders: 0 is success, other is error.
    //(I get those mixed up.....)
    Evidently. You return 1 from main to indicate success, and 0 for failure.

    Oh yeah, and in your switch:
    Code:
    switch (argv[optnum][2]) {
    you don't exit if you have to print the program's usage. I think you should, otherwise it will try to open a bogus file.

    decrypt() and encrypt() return ints. You store their values in chars. chars, no less, not unsigned chars or anything.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You don't handle -V very well, and -v could be a fall-through.

    No, I don't think I want to steal your source.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    52
    a few things:
    1)I didn't say you wanted to. If you don't like it, then I don't care, I wrote this for simple encryption for temporary purposes.
    2)It's not supposed to be foolproof encryption, just a quick encryption to prevent a file from being opened.
    3)ah - i thought w+ opened it so that you could both read and write to an existing file.
    4)right, i'll look up using fflush() and not feof()
    5)oops about the [2].... my mistake :-D
    6)so anyways, i'm not sure what's wrong with it. I just want it to accept the arguments, be able to showusage(), and encrypt or decrypt by adding to the ASCII values of characters read from files.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    1)I didn't say you wanted to. If you don't like it, then I don't care, I wrote this for simple encryption for temporary purposes.
    It's just funny (bordering on hilarious, really) that someone would post such a mangled piece of crap and have the nerve to demand that people not steal their code, when people that actually know what they're doing post good code here all the time without any such statement.

    It just makes it seem like you think we're all a bunch of bad guys. And not just bad, but stupid, for wanting to steal code that doesn't even work. I mean, come on...
    Last edited by itsme86; 12-28-2005 at 03:48 PM.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    3)ah - i thought w+ opened it so that you could both read and write to an existing file.
    You were probably thinking of mode r+. http://www.cplusplus.com/ref/cstdio/fopen.html
    4)right, i'll look up using fflush() and not feof()
    This FAQ has explanations of this.
    fflush: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    !feof: http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    6)so anyways, i'm not sure what's wrong with it...
    Well, how about reading the last few posts and doing what they suggest? The w+ is a big problem. It assures that you don't have an input file.
    ...I just want it to accept the arguments, be able to showusage(), and encrypt or decrypt by adding to the ASCII values of characters read from files.
    Well, change the arguments thing for one and post the results. "It doesn't work" isn't good enough.

    Let me guess -- you end up with a file, one byte in size.
    Last edited by dwks; 12-28-2005 at 04:29 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your code doesn't measure up to the usage:
    Code:
    void showusage(char* progname) {
      printf(
      "Usage: %s [options] file\n"
      "  -e = encrypt file\n"
      "  -d = decrypt file\n"
      "  -v = value to use for encryption / decryption.\n"  // no mention of how the number is passed, ie like -v3 or -v 5
      "    default is 3.\n"  // there's no default
      "  -h = print this help page\n"
      "  OPTIONS ARE CASE SENSITIVE\n"  // that's usually understood, and besides, options are not case sensitive!
      "  Example:\n"
      "  %s -e -v 25 blah.txt\n", *progname, *progname);  // already mentioned this
    }
    If you want that "example" to work you have some programming to do.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by dwks
    I think this is the more appropriate FAQ:
    http://c-faq.com/stdio/fupdate.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 10:59 PM
  2. Encryption Program (help me please)
    By Arkanos in forum Windows Programming
    Replies: 7
    Last Post: 10-30-2005, 08:01 PM
  3. Problem with basic encryption program.
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 08-27-2005, 04:41 AM
  4. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 09:58 AM
  5. Encryption Program Problem
    By WhoCares in forum C++ Programming
    Replies: 0
    Last Post: 04-01-2002, 11:50 AM