tags:

views:

166

answers:

5

Hi all,

I have used the crypt function in c to encrypt the given string. I have written the following code,

#include<stdio.h>
#include<unistd.h>

int main()
{
printf("%s\n",crypt("passwd",1000));
}

But the above code threw an error ,"undefined reference to `crypt'". What is the problem in the above code.

Thanks in advance.

A: 

You need to include crypt.h if you want to use crypt(). Below your other two includes, add:

#include <crypt.h>
Chad Birch
I have added this line into my code.After that also it is throwing same error.
kiruthika
A: 

The crypt function is non-standard, but is supplied as an extension by the GNU C library on Linux. It's defined in <crypt.h>

GMan
A: 

You need to put the following line before your includes:

#define _XOPEN_SOURCE
ar
+2  A: 

crypt() uses DES which is extremely insecure and probably older than you 12 years older than you.

I suggest you use a serious encryption algorithm, such as AES. Many libraries offer such encryption; OpenSSL (crypto.lib) is a good choice for example.

Not answering your actual question since a lot of people already did

Andreas Bonini
"The GNU C Library used by almost all Linux distributions provides an implementation of the crypt function which supports the DES, MD5, and SHA based hashing algorithms" (according to Wikipedia at least)
Joey
+3  A: 

If you want to use the crypt() function, you need to link to the crypt library. Add -lcrypt to your compile command (as the man page tells you to!).

caf
Thanks a lot..It works correctly.
kiruthika