Thread: Copying Chars

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    1

    Unhappy Copying Chars

    Hey all, new to the boards and to C in general.

    I have this code below that I modified. It once took a file, encrypted it and wrote it out to another file and vise versa. I've changed it to not use files but rather use passed values instead. Well, I think I messed something up with it as it's not working properly.

    For the most part it works, but when I decrypt I can sometimes see the orginal value but it has lots of other characheters after it. I think I've got something wrong with the my use of the strcpy functions.

    Can somebody review this code and let me know if you see some obvious errors that may cause unexpected results? Thanks!

    Code:
    /* encrypt V1.1 Fri Oct 18 04:28:03 NZDT 2002 */
    /* File de/encryption, using libtomcrypt */
    /* Written by Daniel Richards <[email protected]> */
    /* Help from Tom St Denis with various bits */
    /* This code is public domain, no rights reserved. */
    /* Encrypts by default, -d flag enables decryption */
    /* ie: ./encrypt blowfish story.txt story.ct */
    /* ./encrypt -d blowfish story.ct story.pt */
    
    #include "tomcrypt.h"
    #include "f2c/fglExt.h"
    #include <ctype.h>
    #include <string.h>
    
    int errno;
    
    
    unsigned char *toHex(unsigned char *dst, const unsigned char *src)
    {
       static const unsigned char nibble[] =
       {
          '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
       };
       unsigned char *start = dst;
       while ( *src )
       {
          *dst++ = nibble [ *src >> 4    ];
          *dst++ = nibble [ *src++ & 0xF ];
       }
       *dst = '\0';
       return start;
    }
    
    unsigned char *fromHex(unsigned char *dst, const unsigned char *src)
    {
       unsigned char *start = dst;
       while ( *src )
       {
          if ( isdigit(*src) )
          {
             *dst = *src++ - '0';
          }
          else
          {
             *dst = *src++ - 'A' + 10;
          }
          *dst <<= 4;
          if ( isdigit(*src) )
          {
             *dst++ += *src++ - '0';
          }
          else
          {
             *dst++ += *src++ - 'A' + 10;
          }
       }
       *dst = '\0';
       return start;
    }
    
    
    int aescrypt(int n)
    {
       unsigned char plaintext[512],ciphertext[512], vsData[512];
       unsigned char tmpkey[512], key[MAXBLOCKSIZE], IV[MAXBLOCKSIZE];
       unsigned long outlen, y, ivsize, x;
       unsigned char over [ sizeof vsData * 2  - 1 ], back [ sizeof vsData ];
       int decrypt;
      symmetric_CTR ctr;
       int cipher_idx, hash_idx, ks;
       char *infile, *outfile, *cipher;
       prng_state prng;
       FILE *fdkey;
       unsigned char hexval[512], retHex[512];
    
       /* register algs, so they can be printed */
       register_algs();
    
       popvchar((char*)vsData, sizeof(vsData));
       /* popquote(vsData, sizeof(vsData)); */
       popint(&decrypt);
    
       cipher = "aes";
    
       if (decrypt) {
          printf("Decrypt Data Length=%d\n", strlen(vsData));
          fromHex(retHex, vsData);
       }
    
    
       /*
       if (!strcmp(argv[1], "-d")) {
          decrypt = 1;
          cipher  = argv[2];
          infile  = argv[3];
          outfile = argv[4];
       } else {
          decrypt = 0;
          cipher  = argv[1];
          infile  = argv[2];
          outfile = argv[3];
       }
       */
    
    
       cipher_idx = find_cipher(cipher);
       if (cipher_idx == -1) {
          printf("Invalid cipher specified.\n");
          exit(-1);
      }
    
       hash_idx = find_hash("sha256");
       if (hash_idx == -1) {
          printf("Invalid cipher specified.\n");
          exit(-1);
      }
    
       hash_idx = find_hash("sha256");
       if (hash_idx == -1) {
          printf("LTC_SHA256 not found...?\n");
          exit(-1);
       }
    
       ivsize = cipher_descriptor[cipher_idx].block_length;
       ks = hash_descriptor[hash_idx].hashsize;
       if (cipher_descriptor[cipher_idx].keysize(&ks) != CRYPT_OK) {
          printf("Invalid keysize???\n");
          exit(-1);
       }
    
       /*
       printf("\nEnter key: ");
       fgets((char *)tmpkey,sizeof(tmpkey), stdin);
       */
       fdkey = fopen("/data3/cc.key","rb");
       if (fdkey == NULL) {
          perror("Can't open key file");
          exit(-1);
       }
       if (fread(tmpkey,1,sizeof(tmpkey),fdkey) != sizeof(tmpkey)) {
       }
       fclose(fdkey);
    
       outlen = sizeof(key);
       if ((errno = hash_memory(hash_idx,tmpkey,strlen((char *)tmpkey),key,&outlen)) != CRYPT_OK) {
          printf("Error hashing key: %s\n", error_to_string(errno));
          exit(-1);
       }
    
       if (decrypt) {
          /* Need to read in IV from the inputted string*/
          strncpy(IV, retHex, ivsize);
          IV[ivsize] = '\0';
    
          if (strlen(IV) != ivsize) {
             printf("Error reading IV from input.\n");
             exit(-1);
          }
    
    
          if ((errno = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
             printf("ctr_start error: %s\n",error_to_string(errno));
             exit(-1);
          }
    
          /* get everything after the IV from the encrypted data */
          strncpy(ciphertext, &retHex[ivsize], strlen(retHex)-ivsize);
          *(ciphertext + strlen(retHex)-ivsize) = '\0';
    
          /* IV done -do decryption */
          if ((errno = ctr_decrypt(ciphertext,plaintext,sizeof(plaintext),&ctr)) != CRYPT_OK) {
             printf("ctr_decrypt error: %s\n", error_to_string(errno));
             exit(-1);
          }
    
    
          printf("Decrypt: %s\n", plaintext);
          pushquote(plaintext, strlen(plaintext));
    
       } else {  /* encrypt */
          /* Setup yarrow for random bytes for IV */
    
          if ((errno = rng_make_prng(128, find_prng("yarrow"), &prng, NULL)) != CRYPT_OK) {
             printf("Error setting up PRNG, %s\n", error_to_string(errno));
          }
    
          /* You can use rng_get_bytes on platforms that support it */
          /* x = rng_get_bytes(IV,ivsize,NULL);*/
          x = yarrow_read(IV,ivsize,&prng);
          if (x != ivsize) {
             printf("Error reading PRNG for IV required.\n");
             exit(-1);
          }
    
    
          /* Save the IV */
          strcpy(retHex, IV);
         /* do the encryption */
          if ((errno = ctr_start(cipher_idx,IV,key,ks,0,CTR_COUNTER_LITTLE_ENDIAN,&ctr)) != CRYPT_OK) {
             printf("ctr_start error: %s\n",error_to_string(errno));
             exit(-1);
          }
    
          if ((errno = ctr_encrypt(vsData, ciphertext,strlen(vsData),&ctr)) != CRYPT_OK) {
             printf("ctr_encrypt error: %s\n", error_to_string(errno));
             exit(-1);
          }
    
    
          /* append the ciphertext to the IV */
          strcat(retHex, ciphertext);
    
    
          /* convert the encrypted string to hex */
          toHex(over, retHex);
    
          pushvchar(over, strlen(over));
       }
    
    
    
       return 1;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Using fread() to read from a file doesn't necessarily append a \0 to your data (which the commented out fgets() will do).

    So later strlen() calls for example will break.

    Unless your key file is always say 512 bytes, then the success test is also wrong.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Copying - Random chars at the end
    By Govalant in forum C Programming
    Replies: 4
    Last Post: 05-26-2007, 07:33 AM
  2. access violation.. copying chars
    By trill3k in forum C Programming
    Replies: 2
    Last Post: 05-15-2006, 09:40 AM
  3. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  4. really got stuck with unsigned chars
    By Abdi in forum C Programming
    Replies: 7
    Last Post: 06-11-2002, 12:47 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM