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

This is a discussion on I'm building my xor encryptor, help with nul character within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by _izua_ darn I have to make boost work in my IDE. I'm missing out so many cool ...

  1. #16
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    Posts
    311

    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?

    Code:
    // 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)
        usage();
    
      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)) {
            perror(fname);
            fseek(fp, 0, SEEK_SET);
            fwrite(tmpbuf, 1, getsize(fp), fp);
          }
          free(tmpbuf);
        }
        fclose(fp);
      }         
    }
    
    void usage()
    {
      fprintf(stderr, "usage: xorc <key> <file(s)>\n");
      exit(1);
    }

  3. #18
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #19
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    311
    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.

Page 2 of 2 FirstFirst 12
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, 04:32 PM
  4. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 09:31 PM
  5. Replies: 12
    Last Post: 01-12-2002, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21