标题:MD5的实现
只看楼主
辰星睿
Rank: 2
等 级:论坛游民
威 望:3
帖 子:33
专家分:63
注 册:2010-12-24
结帖率:50%
 问题点数:0 回复次数:6 
MD5的实现
别人的代码 但是似乎结果不正确  我觉得是数字可能不对 但是没有找到 希望大家帮忙找一下
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#define SINGLE_ONE_BIT 0x80
#define BLOCK_SIZE 512
#define MOD_SIZE 448
#define APP_SIZE 64
#define BITS 8

#define A 0x67452301
#define B 0xefcdab89
#define C 0x98badcfe
#define D 0x10325476

#ifdef UINT64
#undef UINT64
#endif

#ifdef UINT32
#undef UINT32
#endif

typedef unsigned long UINT64;
typedef unsigned long UINT32;
typedef unsigned char UINT8;

typedef struct
{
    char *message;
    UINT64 length;
}STRING;

const UINT32 X[4][2]={{0,1},{1,5},{5,3},{0,7}};

const UINT32 S[4][4]={
    {7,12,17,22},
    {5,9,14,20},
    {4,11,16,23},
    {6,10,15,21}
};

UINT32 F(UINT32 X,UINT32 Y,UINT32 Z)
{
    return (X&Y)|(~X&Z);
}
UINT32 G(UINT32 X,UINT32 Y,UINT32 Z)
{
    return (X&Z)|(Y&~Z);
}
UINT32 H(UINT32 X,UINT32 Y,UINT32 Z)
{
    return X^Y^Z;
}
UINT32 I(UINT32 X,UINT32 Y,UINT32 Z)
{
    return Y^(X|~Z);
}

UINT32 rotate_left(UINT32 x,UINT32 s)
{
    return (x<<s)|(x>>(32-s));
}
UINT32 count_padding_bits(UINT32 length)
{
    UINT32 div=length*BITS/BLOCK_SIZE;
    UINT32 mod=length*BITS%BLOCK_SIZE;
    UINT32 c_bits;
    if(mod==0)
    {
        c_bits=MOD_SIZE;
    }
    else
    {
        c_bits=(MOD_SIZE+BLOCK_SIZE-mod)%BLOCK_SIZE;
    }
    return c_bits/BITS;
}
STRING append_padding_bits(char *argv)
{
    UINT32 msg_length=strlen(argv);
    UINT32 bit_length=count_padding_bits(msg_length);
    UINT64 app_length=msg_length *BITS;
    STRING string;
    string.message=(char*)malloc(msg_length+bit_length+APP_SIZE/BITS);
    strncpy(string.message,argv,msg_length);
    memset(string.message+msg_length,0,bit_length);
    string.message[msg_length]=SINGLE_ONE_BIT;//(char)
    memmove(string.message+msg_length+bit_length,(char*)&app_length,sizeof(UINT64));
    string.length=msg_length+bit_length+sizeof(UINT64);
    return string;
}

int main(int argc,char *argv[])
{
    STRING string;
    UINT32 w[16];
    UINT32 chain[4];
    UINT32 state[4];
    UINT8 r[16];
    UINT32 (*auxi[4])(UINT32,UINT32,UINT32)={F,G,H,I};
    int roundldx;
    int argldx;
    int sldx;
    int wldx;
    int i;
    int j;
    if(argc<2)
    {
        fprintf(stderr,"usage:%s string...\n",argv[0]);
        return EXIT_FAILURE;
    }
    for(argldx=1;argldx<argc;argldx++)
    {
        string=append_padding_bits(argv[argldx]);
        chain[0]=A;
        chain[1]=B;
        chain[2]=C;
        chain[3]=D;
        int len=string.length;
        for(j=0;j<len;j+=BLOCK_SIZE/BITS)
        {
            memmove((char*)w,string.message+j,BLOCK_SIZE/BITS);
            memmove(state,chain,sizeof(chain));
            for(roundldx=0;roundldx<4;roundldx++)
            {
                wldx=X[roundldx][0];
                sldx=0;
                for(i=0;i<16;i++)
                {
                    state[sldx]=state[(sldx+1)%4]+
                        rotate_left(state[sldx]+
                    (*auxi[roundldx])
                    (state[(sldx+1)%4],state[(sldx+2)%4],state[(sldx+3)%4])
                    +w[wldx]
                    +(UINT32)floor((1UL<<32)*fabs(sin(roundldx*16+i+1))),
                    S[roundldx][i%4]);
                    sldx=(sldx+3)%4;
                    wldx=(wldx+X[roundldx][1])&0xF;////////////
                }
            }
            chain[0]+=state[0];
            chain[1]+=state[1];
            chain[2]+=state[2];
            chain[3]+=state[3];
        }
        memmove(r+0,(char*)&chain[0],sizeof(UINT32));
        memmove(r+4,(char*)&chain[1],sizeof(UINT32));
        memmove(r+8,(char*)&chain[2],sizeof(UINT32));
        memmove(r+12,(char*)&chain[3],sizeof(UINT32));
        for(i=0;i<16;i++)
            printf("%02x",r[i]);
        putchar('\n');
    }
    return EXIT_SUCCESS;
}
搜索更多相关主题的帖子: long 
2011-05-19 20:11
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
得分:0 
我是看不懂  帮顶

                                         
===========深入<----------------->浅出============
2011-05-19 20:14
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
得分:0 
就我这智商能看懂吗? 一点注释都没有。

[ 本帖最后由 lz1091914999 于 2011-5-20 09:25 编辑 ]

My life is brilliant
2011-05-20 09:10
其实、不想说
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:156
注 册:2011-3-3
得分:0 
水平不够啊!!
2011-05-20 09:17
liujigen
Rank: 2
等 级:论坛游民
帖 子:32
专家分:63
注 册:2010-8-24
得分:0 
你需要的是达到什么结果,没有说明,谁知道你想干什么,可以用debug慢慢的调试吧。
2011-05-20 12:00
辰星睿
Rank: 2
等 级:论坛游民
威 望:3
帖 子:33
专家分:63
注 册:2010-12-24
得分:0 
呃。。。已经明白了  嘿嘿
2011-05-20 17:43
bccn_1990525
Rank: 1
等 级:新手上路
帖 子:6
专家分:1
注 册:2011-5-20
得分:0 
没注释,不看,
2011-05-20 18:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-340471-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016798 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved