Thread: I'm building my xor encryptor, help with nul character

  1. #16
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    Quote Originally Posted by _izua_ View Post
    darn I have to make boost work in my IDE. I'm missing out so many cool features, starting with regular expressions
    If you want, you could rewrite my function to just call new[] and delete[] directly instead of using a scoped_array.

  2. #17
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    CA, USA

    Complete rewrite

    I did a complete re-write. I fixed some of the things that were mentioned.
    One question though, I can't figure out how to test if my "backup/restore" plan will work. I already tried deleting the file in the middle of an encryption and the original file disappears. Any ideas how to generate some file errors?

    // Simple XOR encryption program version 2.0
    // Command line usage:
    // xorc <key> <file(s)>
    // Program will encrypt and overwrite specified files
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    size_t getsize(FILE *fp);
    int fileopen(FILE **fp, char *mode, char *fname);
    void encrypt(char *fname, char *key);
    void usage();
    int main(int argc, char **argv)
      if (argc < 3)
      for (int i = 2; i < argc; ++i)
        encrypt(argv[i], argv[1]);
      return (EXIT_SUCCESS);
    size_t getsize(FILE *fp)
      size_t size;
      fseek(fp, 0, SEEK_END);
      size = ftell(fp);
      fseek(fp, 0, SEEK_SET);
      return size;
    int fileopen(FILE **fp, char *mode, char *fname)
      *fp = fopen(fname, mode);
      if (!fp)
        fprintf(stderr, "error: could not open %s.\n", fname);
      return (int)*fp;
    void encrypt(char *fname, char *key)
      FILE *fp;
      int k = 0;
      int stop = 0;
      size_t read;
      size_t keylen = strlen(key);
      char buf[BUFSIZ];
      char *tmpbuf;
      if (fileopen(&fp, "rb+", fname)) {
        if ((tmpbuf = malloc(getsize(fp)))) {
          while (!stop) {
            read = fread(buf, 1, BUFSIZ, fp);  
            if (read != BUFSIZ)
              stop = 1;
            for (int j = 0; j < read; ++j) {
              tmpbuf[j] = buf[j];
              buf[j] = buf[j] ^ key[k];
              k = k < keylen ? k + 1 : 0;
            fseek(fp, 0 - read, SEEK_CUR);
            if (fwrite(buf, 1, read, fp) != read)
              stop = 1;
          if (ferror(fp)) {
            fseek(fp, 0, SEEK_SET);
            fwrite(tmpbuf, 1, getsize(fp), fp);
    void usage()
      fprintf(stderr, "usage: xorc <key> <file(s)>\n");

  3. #18
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > tmpbuf[j] = buf[j];
    If this is meant to be holding the whole file, in case of failure, then it's wrong.
    It just keeps using the BUFSIZ start of the memory.
    Holding very large files in memory will just thrash the virtual memory.

    > if (!fp)
    Except the result is in *fp

    > return (int)*fp;
    Ugly, use
    return *fp != NULL;

    > fwrite(tmpbuf, 1, getsize(fp), fp);
    Maybe call getsize() just the once, given that it messes with the file pointer.
    Calling getsize() in the middle of file processing would be a disaster.

    > Any ideas how to generate some file errors?
    Which OS are you on?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #19
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    CA, USA
    Quote Originally Posted by Salem View Post
    Which OS are you on?
    Linux. Would probably help to fill out my profile, guess I'll do that now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  3. Character handling help
    By vandalay in forum C Programming
    Replies: 18
    Last Post: 03-29-2004, 05:32 PM
  4. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 10:31 PM
  5. Replies: 12
    Last Post: 01-12-2002, 09:57 AM