Help with a C program
Link |
by Frost-Heart
on 2007-05-13 22:31:57 (edited 2007-05-13 23:23:32)
|
Hi, i'm looking for some help with a C program of mine that i'm writing. Fairly simple i guess, it's just to encrypt a 5 character string using an integer key and then decrypt it back. Problem is...When i compile it, no problem. When i run it and input the string and the key, they display alright, but then i get 'Segmentation Error' at which point my program stops running. Not quite sure if it's allowable to post the code but...oh well XD {it's not too big, really}. Oh, by the way, i have to use pointers. Which is why the functions are 'void'.#include <stdio.h> void getPassword (char *Pass); void getKey (int *Key); void encrypt (char *Pass, int *Key); void decrypt (char *Pass, int *Key); int main(){ char * password[6]; int * key; getPassword(password[6]); printf("Password: %sn", password); getKey(key); printf("Key: %dn", key); encrypt(password[6], key); printf("Encrypted password: %sn", password); decrypt(password[6], key); printf("Decrypted password: %sn", password); return 0; } void getPassword (char *Pass){ printf("Enter password: "); scanf("%5s", &Pass); scanf("%[^n]"); scanf("%*c"); } void getKey (int *Key){ printf("Enter Key: "); scanf("%d", &Key); scanf("%[^n]"); scanf("%*c"); } void encrypt (char *Pass, int *Key){ int i; for (i=0 ; i<5 ; i++){ Pass[i]=(((Pass[i]-65)+*Key)%26)+65; } } void decrypt (char *Pass, int *Key){ int i; for (i=0 ; i<5 ; i++){ Pass[i]=(((Pass[i]+65)-*Key)%26)-65; } } |
Re: Help with a C program
Link |
by
on 2007-05-13 23:38:31 (edited 2007-05-13 23:54:14)
|
you want to declare char password[6]; and not char*, because the [6] makes it a character array already. when you pass password as an argument, you are already passing a pointer, and must leave OFF the [6]. example: getPassword(password); You have one key, and you need to properly instantiate it as an int, not an int*: int key;! Below is the edited program, but it appears your encryption algorithm is flawed. #include <stdio.h> void getPassword(char *pass); void getKey(int *Key); void encrypt(char *pass, int *key); void decrypt(char *pass, int *key); int main() { char password[6]; int key; getPassword(password); printf("Password: %sn", password); getKey(&key); printf("Key: %dn", key); encrypt(password, &key); printf("Encrypted password: %sn", password); decrypt(password, &key); printf("Decrypted password: %sn", password); return 0; } void getPassword(char *pass) { printf("Enter password: "); scanf("%5s", pass); scanf("%[^n]"); scanf("%*c"); } void getKey(int* key) { printf("Enter key: "); scanf("%d", key); scanf("%[^n]"); scanf("%*c"); } void encrypt(char *pass, int *key) { int i; for(i = 0 ; i < 5 ; i++) { pass[i] = (((pass[i] - 65) + *key) % 26) + 65; } } void decrypt(char *pass, int *key) { int i; for(i = 0 ; i < 5 ; i++) { pass[i] = (((pass[i] - 65) - *key + 26) % 26) + 65; } } EDIT: I fixed your encryption/decryption algorithm. You need to reverse the sign on the KEY but not the +/- 65! Also, you won't get good numbers if you % on a negative number, so just add 26. |
Re: Help with a C program
Link |
by Frost-Heart
on 2007-05-14 02:14:23
|
Ahh, i see, i see ^^ Thanks alot, This is extremelly helpful. Hmm, i see what you mean about string (password[6]) and the int Key. I keep getting confused about what needs to be done with arrays when using them as pointers XD Thanks for fixing up me encrypt/decrypt as well. I figured i'd just reverse all the signs in decrypt XD Well anyway, many thanks ^^ (How'd you do that box ting? Nice ^^) |