FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Slight problem with C code





ChrisCh
Hi there. I'm trying to write a program in C that takes command line arguments and encrypts them, and appends them to a text file (ie. the first argument would be the file name, and everything after that would be the text to be encrypted). I've got two working programs - one to take plain text and append it to a text file, and another to encrypt a predefined line... But I'm trying to make 1 program that will take the text, encrypt it, then append it to a text file, and am having no luck. Here is what I've written in the combined program:

Code:
#include<stdio.h>
#include<string.h>

void encrypt(unsigned long*, unsigned long*, int);

int main(int argc, char *argv[]) {
    FILE *fp;
    unsigned long key[4] = {1111, 2222, 3333, 4444};
    int x;
   
    if(fp = fopen(argv[1], "a")) {
          for(x = 0; x < strlen(argv[x]); x += 8)
             encrypt((unsigned long*) argv[x], key, x / 4);
         
          for(x = 2; x < argc; x++) {
             fputs(argv[x], fp);
             fputs(" ", fp);
          }
          fputs("\n", fp);
          fclose(fp);
    }
   
    return 0;
    system("pause");
}

 void encrypt(unsigned long* v, unsigned long* k, int start) {
     unsigned long v0=v[start], v1=v[start + 1], sum=0, i;           /* set up */
     unsigned long delta=0x9e3779b9;                     /* a key schedule constant */
     unsigned long k0=k[0], k1=k[1], k2=k[2], k3=k[3];   /* cache key */
     for (i=0; i < 32; i++) {                            /* basic cycle start */
         sum += delta;
         v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
         v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);   /* end cycle */
     }
     v[start]=v0; v[start + 1]=v1;
 }


The program crashes in Windows... So I'm thinking that I may have to something fancy to argv[x] to actually make it work... Just not sure what! (of course I could be wrong about changing argv[x])

If you have any ideas, I'd love to hear them! Many thanks Very Happy
JayBee
Try
Code:
argv[1][x]

insted of
Code:
argv[x]


You want to access next character in the first commandline parameter, not the first, second, ... commandline parameter.
In your case, you want indexes of array argv which doesn't exists.
(I didn't test it, but I hope it helps you)


Or, If you want encrypt all parameters from commandline, you shuld use
Code:
argc
insted of
Code:
strlen(argv[x])
You have mass in this. I don't know what you exactly need.



You should also test number of parameters (number in argc) and not to read argv[1] if there is no parametr.
ChrisCh
Hi there. Thanks for your suggestion, but I tried adding the [1]'s in, and still no success. Since I was a bit unclear in my first message, this is what the program is supposed to do:

*Take a filename and text via command line arguments passed to it via a VB program
*Encrypt *most* of argv - with the exception of the first argument which is the name of the file that it's encrypting it to
*Append the encrypted text to the textfile as defined by the first argument

Fingers crossed that that made more sense :p
JayBee
I have tried to correct it, but there are so many mistakes.

1. You should encrypt parameters like this pseudo code
Code:
foreach(parameter) {
    foreach(four letters of parameter) {
        encrypt(this four letters)
    }
}


2. there is problem with encryption
You don't have parameters aligned with 4B. When it encrypts char \0 (which is string termination) you should get in problems. This routine is bad for strings with length differs from 4, 8, 12, ....
If you want to solve this, you should fill all parameters with dummy character and put the ending zero \0 after these characters. But this memory is not yours (seg fault) so you should copy all parameters to your space.
Related topics
Languages in c++
C++ TUTORIAL : INTRODUCTION TO C++
Header("Location: PROBLEM");
What's wrong with this C code???
C++
Tutorial on visual C++
C++ code help
This C code is little unclear to me.
Funny with C code.
Handling /dev/dsp in C
C++ code problem
10 frih$ contest: what does this c++ code do?
PHP Whos Online Code OR Page refresh..
Machine Problem No.2 [Pt. 1]
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.