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:
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
| 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
