Reputation: 9
I linked -lcrypt , the problems is that I get the same encryption no matter my command line argument. The encryption seems to only change if I change the salt. What in my code would lead to this flaw?
#define _XOPEN_SOURCE
#include <unistd.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *enc[])
{
if (argc != 2)
{
printf("Improper command-line arguments\n");
return 1;
}
char *salt = "ZA";
printf("%s \n", crypt(*enc, salt));
}
Upvotes: 0
Views: 717
Reputation: 3671
You nearly got it. only the commandline argument handling was wrong.
if your program is called prg and you call it like this:
prg teststring
than enc[1]
is "teststring"
#define _XOPEN_SOURCE
#include <unistd.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *enc[])
{
if (argc != 2)
{
printf("Improper command-line arguments\n");
return 1;
}
char *salt = "ZA";
printf("%s \n", crypt(enc[1], salt)); // <<----
}
usually the command line args are called argc and argv:
int main(int argc, char *argv[])
that would make the relevant line like this:
printf("%s \n", crypt(argv[1], salt));
Upvotes: 1
Reputation: 25855
In crypt(*enc, salt)
, you're encrypting your first argument, which is the name of the program, not the first actual argument. Try crypt(enc[1], salt)
instead.
Upvotes: 1