首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在MD5情况下理解crypt()的输出

在MD5情况下理解crypt()的输出
EN

Stack Overflow用户
提问于 2018-07-10 08:33:45
回答 1查看 945关注 0票数 0

crypt(3)函数的glibc版本支持MD5算法。根据文件:

代码语言:javascript
复制
If salt is a character string starting with the characters "$id$"
followed by a string optionally terminated by "$", then the result
has the form:

       $id$salt$encrypted

该文档还指出,对于MD5,$id必须为1,并且encrypted字符串的大小固定为22个字符。

但是MD5输出总是128位长.那么,为什么只有22个字符呢?

我认为它应该是be 64编码的,因为128位可以表示22个Base64字符。我们来验证一下我的假设。

下面是我编写的用crypt()调用MD5的简单代码,没有用于加密的salt:

代码语言:javascript
复制
#define _XOPEN_SOURCE
#include <unistd.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    char md5[] = "$1$";
    char *res;
    res = crypt("helloworld", md5);
    printf("res = %s\n", res);
    return 0;
}

运行它:

代码语言:javascript
复制
$ ./crypt
res = $1$$edK86ZB1Vvaz2eneY.itb.

根据MD5加密的helloworld值为edK86ZB1Vvaz2eneY.itb.

现在,通过MD5或md5sum计算helloworld在线工具散列。

代码语言:javascript
复制
$ echo -n helloworld | md5sum
fc5e038d38a57032085441e7fe7010b0  -

MD5哈希值为fc5e038d38a57032085441e7fe7010b0。通过Base64或以下命令将此十六进制字符串转换为这个在线转换器

代码语言:javascript
复制
$ echo "fc5e038d38a57032085441e7fe7010b0" | xxd -r -p | base64
/F4DjTilcDIIVEHn/nAQsA==

此值与crypt()上面生成的值不同。

那么,在这种情况下,crypt()是如何产生它的输出的,或者我应该如何解释它呢?我在这里错过了什么?

EN

回答 1

Stack Overflow用户

发布于 2018-07-10 09:07:50

它使用MD5算法,但不返回生成的MD5哈希。有许多奇怪的事情发生在源代码中,来自这里

代码语言:javascript
复制
/* The original implementation now does something weird: for every 1
   bit in the key the first 0 is added to the buffer, for every 0
   bit the first character of the key.  This does not seem to be
   what was intended but we have to follow this to be compatible.  */

代码语言:javascript
复制
/* Now comes another weirdness.  In fear of password crackers here
   comes a quite long loop which just processes the output of the
   previous round again.  We cannot ignore this here.  */

__md5_crypt_r是从__crypt_r这里调用的。

它使用MD5算法,但不返回用户字符串中的md5哈希。相反,它执行多个变换来“加密”输入字符串。

对于md5散列,请使用openssl/md5.h。我想我建议不要使用libc中的加密家族函数,除非编写一些非常小且不可移植的东西。来自html文档

..。DES只使用一个56位的密钥(加上8个奇偶位),并且在1998年建造了一台机器,它可以在大约6天内搜索所有可能的密钥,花费大约200000美元;如果有更多的钱,更快的搜索是可能的。这使得简单的DES在大多数情况下都不安全,NIST不再允许新的美国政府系统使用简单的DES。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51260621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档