LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 925|回复: 2

c语言中,用MD5生成openldap密码的问题

[复制链接]
发表于 2005-10-19 10:36:06 | 显示全部楼层 |阅读模式
我在c语言中对openldap进行操作时在保存用户密码时我用了MD5的算法,其中我查看了phpldapAdmin的php代码发现它的md5密码生成方式是:先加密为md5然后转换成16进制新式再用base64输出,于是我用C语言实现了上述过程,其中md5编码后转换为16进制后的编码与php中的结果是一致的,但是在base64编码后却与php中的编码结果不一致,是否是我的base64编码有问题,高手帮我看看。
以下是我的代码:
php代码
printf("md5=%s\n", md5("123456") );
printf("pack=%s\n",pack( 'H*' , md5("123456") ) );
printf("base=%s\n",base64_encode(pack( 'H*' , md5("123456") ) ) );


c代码:
#include <stdio.h>
#include <openssl/md5.h>
#include <string.h>
#include <stdlib.h>


int Base64Enc(char *buf, char*text,int size) ;

int main()
{
unsigned char result[16];
char rs[33];
char  base[64];
MD5_CTX ctx;
char * TEST_STRING = "123456";
MD5_Init(&ctx);
MD5_Update(&ctx, TEST_STRING, strlen(TEST_STRING));
MD5_Final(result, &ctx);
printf("md5= %s\n",result);
sprintf((char*)rs,"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0],result[1],result[2],result[3],result[4],result[5],
result[6],result[7],result[8],result[9],result[10],result[11],
result[12],result[13],result[14],result[15]);

printf("rs= %s\n",rs);
Base64Enc(base,(char*)rs,16);
printf("base= %s\n",base);
}

int Base64Enc(char *buf, char*text,int size)
{
static char *base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int buflen = 0;

while(size>0)
{
*buf++ = base64_encoding[ (text[0] >> 2 ) & 0x3f];
if(size>2)
{
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0xF) << 2) | (text[2] >> 6)];
*buf++ = base64_encoding[text[2] & 0x3F];
}
else
{
switch(size)
{
case 1:
*buf++ = base64_encoding[(text[0] & 3) << 4 ];
*buf++ = '=';
*buf++ = '=';
break;
case 2:
*buf++ = base64_encoding[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = base64_encoding[((text[1] & 0x0F) << 2) | (text[2] >> 6)];
*buf++ = '=';
break;
}
}

text +=3;
size -=3;
buflen +=4;
}

*buf = 0;
return buflen;
}
发表于 2005-10-19 10:50:32 | 显示全部楼层

  1. text[0] >> 2
复制代码

所有的类似这样的地方都应该加上(unsigned char)做强制类型转换,不然会出问题的。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-10-20 10:13:31 | 显示全部楼层
Post by 小锁

  1. text[0] >> 2
复制代码

所有的类似这样的地方都应该加上(unsigned char)做强制类型转换,不然会出问题的。


谢谢你的提醒,我全部加上(unsigned char) ,但结果没有变化。
修改后的Base64Enc函数如下:

int Base64Enc(char *buf, char*text,int size)
{
        static char *base64_encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        int buflen = 0;
       
        while(size>0)
        {
                *buf++ = base64_encoding[ ((unsigned char)text[0] >> 2 ) & 0x3f];
                if(size>2)
                {
                        *buf++ = base64_encoding[(((unsigned char)text[0] & 3) << 4) | ((unsigned char)text[1] >> 4)];
                        *buf++ = base64_encoding[(((unsigned char)text[1] & 0xF) << 2) | ((unsigned char)text[2] >> 6)];
                        *buf++ = base64_encoding[(unsigned char)text[2] & 0x3F];
                }
                else
                {
                        switch(size)
                        {
                                case 1:
                                        *buf++ = base64_encoding[((unsigned char)text[0] & 3) << 4 ];
                                        *buf++ = '=';
                                        *buf++ = '=';
                                break;
                                case 2:
                                        *buf++ = base64_encoding[(((unsigned char)text[0] & 3) << 4) | ((unsigned char)text[1] >> 4)];
                                        *buf++ = base64_encoding[(((unsigned char)(unsigned char)text[1] & 0x0F) << 2) | ((unsigned char)text[2] >> 6)];
                                        *buf++ = '=';
                                break;
                        }
                }
       
                text +=3;
                size -=3;
                buflen +=4;
        }
       
        *buf = 0;
        return buflen;
}
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表