Introduction to Crypt Encryption functions (C language)

Source: Internet
Author: User
Tags crypt function definition key string

Defining functions

char * crypt (const char *key,const char * salt);

function Description

Crypt is a cryptographic function that is based on the data encryption Standard (DES) algorithm. Crypt is only available for use in passwords and is not suitable for data encryption.

Crypt () encrypts the string that the parameter key refers to, and the key string length takes only the first 8 characters, and the character beyond this length is meaningless. The parameter salt is a two-character string consisting of A-Z, A-Z, 0-9, "." and "/", which is used to determine the use of 4096 (A-Z, A-Z, 0-9, ".") and "/" a total of 64 characters, 64 squared to 4096, which one of the different built-in tables. When a function executes successfully, it returns a pointer to the encoded string, and the string that the parameter key refers to does not change. The encoded string has a length of 13 characters, and the first two characters are strings represented by the parameter salt.

return value

Returns a string that points to a null-terminated password.

Note Compile-time to add the-lcrypt option at the end.

/**

* Gcc-o Crypt Crypt.c-lcrypt

*/

#include <unistd.h>

#include <crypt.h>

#include <string.h>

int main (void)

{

Char passwd[13];

Char *key;

Char slat[2];

Key = Getpass ("Input-Password:");

Slat[0] = key[0];

SLAT[1] = key[1];

strcpy (Passwd,crypt (Key,slat));

Key = Getpass ("Input Second Password:");

Slat[0] = passwd[0];

SLAT[1] = passwd[1];

printf ("After Crypt (), 1st passwd:%s/n", passwd);

printf ("After Crypt (), 2nd passwd:%s/n", Crypt (Key,slat));

return 0;

}

Program Run Result: (Note: two times as input)

Input Password:

Input Second Password:

After crypt (), 1st passwd:asZvhAtTX.i7g

After crypt (), 2nd passwd:asZvhAtTX.i7g

Related functions: Crypt
Table header file: #include <unistd.h>
function definition: char *getpass (const char *prompt)
Function Description: Getpass () Displays the string that the parameter prompt refers to, and then reads the password entered from the/dev/tty, and then reads the password from the standard input device if it cannot be read from the/dev/tty. The password length entered is limited to 128 characters, contains the ending character null, characters longer than the length and the newline character/n will be ignored. When you enter a password, Getpass () turns off the character response and ignores signals such as ctrl-c or ctrl-z
Return value: Returns a string that points to a null-terminated password
Additional note: For system security considerations, in the same way after using Getpass () to enter a password, the password is best processed as soon as possible, and then clear the password string
Example parameter crypt ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.