当前位置: 技术问答>linux和unix
crytp函数怎么用呀?
来源: 互联网 发布时间:2014-12-01
本文导语: 我怎么对数据进行MD5的加密? | crypt是一种单向加密算法,加密解密使用同一函数。 crypt函式的用法如下: char *crypt(const char *key, const char *setting) setting若是以DES做salt,經crypt...
我怎么对数据进行MD5的加密?
|
crypt是一种单向加密算法,加密解密使用同一函数。
crypt函式的用法如下:
char
*crypt(const char *key, const char *setting)
setting若是以DES做salt,經crypt產生的就是DES
這裡提供一個範例程式,讓大家熟悉一下crypt的運作方式,稍加修改,
即可用在密碼轉換。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
void
to64(s, v, n)
char *s;
long v;
int n;
{
while (--n >= 0) {
*s++ = itoa64[v&0x3f];
v >>= 6;
}
}
main(){
char buf[80];
char dessalt[10],md5salt[10];
struct timeval tv;
printf("nPlease input code to encrypt:");
scanf("%s",buf);
/* Make a good size salt for algoritms that can use it. */
gettimeofday(&tv,0);
/* DES Salt */
to64(&dessalt[0], random(), 3);
to64(&dessalt[3], tv.tv_usec, 3);
to64(&dessalt[6], tv.tv_sec, 2);
dessalt[8] = '';
/* MD5 Salt */
strncpy(&md5salt[0], "$1$", 3);
to64(&md5salt[3], random(), 3);
to64(&md5salt[6], tv.tv_usec, 3);
md5salt[8] = '';
printf("n DES of %s: %s",buf,crypt(buf,dessalt));
printf("n MD5 of %s: %s n",buf,crypt(buf,md5salt));
}
crypt函式的用法如下:
char
*crypt(const char *key, const char *setting)
setting若是以DES做salt,經crypt產生的就是DES
這裡提供一個範例程式,讓大家熟悉一下crypt的運作方式,稍加修改,
即可用在密碼轉換。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
void
to64(s, v, n)
char *s;
long v;
int n;
{
while (--n >= 0) {
*s++ = itoa64[v&0x3f];
v >>= 6;
}
}
main(){
char buf[80];
char dessalt[10],md5salt[10];
struct timeval tv;
printf("nPlease input code to encrypt:");
scanf("%s",buf);
/* Make a good size salt for algoritms that can use it. */
gettimeofday(&tv,0);
/* DES Salt */
to64(&dessalt[0], random(), 3);
to64(&dessalt[3], tv.tv_usec, 3);
to64(&dessalt[6], tv.tv_sec, 2);
dessalt[8] = '';
/* MD5 Salt */
strncpy(&md5salt[0], "$1$", 3);
to64(&md5salt[3], random(), 3);
to64(&md5salt[6], tv.tv_usec, 3);
md5salt[8] = '';
printf("n DES of %s: %s",buf,crypt(buf,dessalt));
printf("n MD5 of %s: %s n",buf,crypt(buf,md5salt));
}
|
crypt这个函数和系统有关。 linux下的时这样的CRYPT(3) Library functions CRYPT(3)
NAME
crypt - password and data encryption
SYNOPSIS
#define _XOPEN_SOURCE
#include
char *crypt(const char *key, const char *salt);
DESCRIPTION
crypt is the password encryption function. It is based on
the Data Encryption Standard algorithm with variations
intended (among other things) to discourage use of hard?
ware implementations of a key search.
key is a user's typed password.
salt is a two-character string chosen from the set
[a-zA-Z0-9./]. This string is used to perturb the algo?
rithm in one of 4096 different ways.
By taking the lowest 7 bit of each character of the key, a
56-bit key is obtained. This 56-bit key is used to
encrypt repeatedly a constant string (usually a string
consisting of all zeros). The returned value points to
the encrypted password, a series of 13 printable ASCII
characters (the first two characters represent the salt
itself). The return value points to static data whose
content is overwritten by each call.
Warning: The key space consists of 2**56 equal 7.2e16 pos?
sible values. Exhaustive searches of this key space are
possible using massively parallel computers. Software,
such as crack(1), is available which will search the por?
tion of this key space that is generally used by humans
for passwords. Hence, password selection should, at mini?
mum, avoid common words and names. The use of a passwd(1)
program that checks for crackable passwords during the
selection process is recommended.
The DES algorithm itself has a few quirks which make the
thing other than password authentication. If you are
planning on using the crypt(3) interface for a cryptogra?
phy project, don't do it: get a good book on encryption
and one of the widely available DES libraries.
CONFORMING TO
SVID, X/OPEN, BSD 4.3
use of the crypt(3) interface a very poor choice for any?
NAME
crypt - password and data encryption
SYNOPSIS
#define _XOPEN_SOURCE
#include
char *crypt(const char *key, const char *salt);
DESCRIPTION
crypt is the password encryption function. It is based on
the Data Encryption Standard algorithm with variations
intended (among other things) to discourage use of hard?
ware implementations of a key search.
key is a user's typed password.
salt is a two-character string chosen from the set
[a-zA-Z0-9./]. This string is used to perturb the algo?
rithm in one of 4096 different ways.
By taking the lowest 7 bit of each character of the key, a
56-bit key is obtained. This 56-bit key is used to
encrypt repeatedly a constant string (usually a string
consisting of all zeros). The returned value points to
the encrypted password, a series of 13 printable ASCII
characters (the first two characters represent the salt
itself). The return value points to static data whose
content is overwritten by each call.
Warning: The key space consists of 2**56 equal 7.2e16 pos?
sible values. Exhaustive searches of this key space are
possible using massively parallel computers. Software,
such as crack(1), is available which will search the por?
tion of this key space that is generally used by humans
for passwords. Hence, password selection should, at mini?
mum, avoid common words and names. The use of a passwd(1)
program that checks for crackable passwords during the
selection process is recommended.
The DES algorithm itself has a few quirks which make the
thing other than password authentication. If you are
planning on using the crypt(3) interface for a cryptogra?
phy project, don't do it: get a good book on encryption
and one of the widely available DES libraries.
CONFORMING TO
SVID, X/OPEN, BSD 4.3
use of the crypt(3) interface a very poor choice for any?
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。