您的位置:程序门 -> vb ->



vb中如何用md5给中文加密?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb中如何用md5给中文加密?[已结贴,结贴人:ljq0239]
发表于:2007-06-27 01:20:05 楼主
调用digeststrtohexstr( "谢谢 ")
这样就会溢出?怎么弄才可以给中文加密?


发表于:2007-06-27 01:21:021楼 得分:0
private   const   offset_4   =   4294967296#
private   const   maxint_4   =   2147483647

private   const   s11   =   7
private   const   s12   =   12
private   const   s13   =   17
private   const   s14   =   22
private   const   s21   =   5
private   const   s22   =   9
private   const   s23   =   14
private   const   s24   =   20
private   const   s31   =   4
private   const   s32   =   11
private   const   s33   =   16
private   const   s34   =   23
private   const   s41   =   6
private   const   s42   =   10
private   const   s43   =   15
private   const   s44   =   21


'=
'=   class   variables
'=
private   state(4)   as   long
private   bytecounter   as   long
private   bytebuffer(63)   as   byte


'=
'=   class   properties
'=
property   get   registera()   as   string
        registera   =   state(1)
end   property

property   get   registerb()   as   string
        registerb   =   state(2)
end   property

property   get   registerc()   as   string
        registerc   =   state(3)
end   property

property   get   registerd()   as   string
        registerd   =   state(4)
end   property


'=
'=   class   functions
'=

'
'   function   to   quickly   digest   a   file   into   a   hex   string
'
public   function   digestfiletohexstr(filename   as   string)   as   string
        open   filename   for   binary   access   read   as   #1
        md5init
        do   while   not   eof(1)
                get   #1,   ,   bytebuffer
                if   loc(1)   <   lof(1)   then
                        bytecounter   =   bytecounter   +   64
                        md5transform   bytebuffer
                end   if
        loop
        bytecounter   =   bytecounter   +   (lof(1)   mod   64)
        close   #1
        md5final
        digestfiletohexstr   =   getvalues
end   function

'
'   function   to   digest   a   text   string   and   output   the   result   as   a   string
'   of   hexadecimal   characters.
'
public   function   digeststrtohexstr(sourcestring   as   string)   as   string
        md5init
        md5update   len(sourcestring),   stringtoarray(sourcestring)
        md5final
        digeststrtohexstr   =   getvalues
end   function

'
'   a   utility   function   which   converts   a   string   into   an   array   of
'   bytes.
'
private   function   stringtoarray(instring   as   string)   as   byte()
        dim   i   as   integer
        dim   bytbuffer()   as   byte
        redim   bytbuffer(len(instring))
        for   i   =   0   to   len(instring)   -   1
                bytbuffer(i)   =   asc(mid(instring,   i   +   1,   1))
        next   i
        stringtoarray   =   bytbuffer
end   function

'
'   concatenate   the   four   state   vaules   into   one   string
'
public   function   getvalues()   as   string
        getvalues   =   longtostring(state(1))   &   longtostring(state(2))   &   longtostring(state(3))   &   longtostring(state(4))
end   function

'
'   convert   a   long   to   a   hex   string
'
private   function   longtostring(num   as   long)   as   string
                dim   a   as   byte
                dim   b   as   byte
                dim   c   as   byte
                dim   d   as   byte
               
                a   =   num   and   &hff&
                if   a   <   16   then
                        longtostring   =   "0 "   &   hex(a)
                else
                        longtostring   =   hex(a)
                end   if
                             
                b   =   (num   and   &hff00&)   \   256
                if   b   <   16   then
                        longtostring   =   longtostring   &   "0 "   &   hex(b)
                else
                        longtostring   =   longtostring   &   hex(b)
                end   if
               
                c   =   (num   and   &hff0000)   \   65536
                if   c   <   16   then
                        longtostring   =   longtostring   &   "0 "   &   hex(c)
                else
                        longtostring   =   longtostring   &   hex(c)
                end   if
             
                if   num   <   0   then
                        d   =   ((num   and   &h7f000000)   \   16777216)   or   &h80&
                else
                        d   =   (num   and   &hff000000)   \   16777216
                end   if
               
                if   d   <   16   then
                        longtostring   =   longtostring   &   "0 "   &   hex(d)
                else
                        longtostring   =   longtostring   &   hex(d)
                end   if
       
end   function

'
'   initialize   the   class
'       this   must   be   called   before   a   digest   calculation   is   started
'
public   sub   md5init()
        bytecounter   =   0
        state(1)   =   unsignedtolong(1732584193#)
        state(2)   =   unsignedtolong(4023233417#)
        state(3)   =   unsignedtolong(2562383102#)
        state(4)   =   unsignedtolong(271733878#)
end   sub

'
'   md5   final
'
public   sub   md5final()
        dim   dblbits   as   double
       
        dim   padding(72)   as   byte
        dim   lngbytesbuffered   as   long
       
        padding(0)   =   &h80
       
        dblbits   =   bytecounter   *   8
       
        '   pad   out
        lngbytesbuffered   =   bytecounter   mod   64
        if   lngbytesbuffered   <=   56   then
                md5update   56   -   lngbytesbuffered,   padding
        else
                md5update   120   -   bytecounter,   padding
        end   if
       
       
        padding(0)   =   unsignedtolong(dblbits)   and   &hff&
        padding(1)   =   unsignedtolong(dblbits)   \   256   and   &hff&
        padding(2)   =   unsignedtolong(dblbits)   \   65536   and   &hff&
        padding(3)   =   unsignedtolong(dblbits)   \   16777216   and   &hff&
        padding(4)   =   0
        padding(5)   =   0
        padding(6)   =   0
        padding(7)   =   0
       
        md5update   8,   padding
end   sub
发表于:2007-06-27 01:21:492楼 得分:0
'
'   break   up   input   stream   into   64   byte   chunks
'
public   sub   md5update(inputlen   as   long,   inputbuffer()   as   byte)
        dim   ii   as   integer
        dim   i   as   integer
        dim   j   as   integer
        dim   k   as   integer
        dim   lngbufferedbytes   as   long
        dim   lngbufferremaining   as   long
        dim   lngrem   as   long
       
        lngbufferedbytes   =   bytecounter   mod   64
        lngbufferremaining   =   64   -   lngbufferedbytes
        bytecounter   =   bytecounter   +   inputlen
        '   use   up   old   buffer   results   first
        if   inputlen   > =   lngbufferremaining   then
                for   ii   =   0   to   lngbufferremaining   -   1
                        bytebuffer(lngbufferedbytes   +   ii)   =   inputbuffer(ii)
                next   ii
                md5transform   bytebuffer
               
                lngrem   =   (inputlen)   mod   64
                '   the   transfer   is   a   multiple   of   64   lets   do   some   transformations
                for   i   =   lngbufferremaining   to   inputlen   -   ii   -   lngrem   step   64
                        for   j   =   0   to   63
                                bytebuffer(j)   =   inputbuffer(i   +   j)
                        next   j
                        md5transform   bytebuffer
                next   i
                lngbufferedbytes   =   0
        else
            i   =   0
        end   if
       
        '   buffer   any   remaining   input
        for   k   =   0   to   inputlen   -   i   -   1
                bytebuffer(lngbufferedbytes   +   k)   =   inputbuffer(i   +   k)
        next   k
       
end   sub

'
'   md5   transform
'
private   sub   md5transform(buffer()   as   byte)
        dim   x(16)   as   long
        dim   a   as   long
        dim   b   as   long
        dim   c   as   long
        dim   d   as   long
       
        a   =   state(1)
        b   =   state(2)
        c   =   state(3)
        d   =   state(4)
       
        decode   64,   x,   buffer

        '   round   1
        ff   a,   b,   c,   d,   x(0),   s11,   -680876936
        ff   d,   a,   b,   c,   x(1),   s12,   -389564586
        ff   c,   d,   a,   b,   x(2),   s13,   606105819
        ff   b,   c,   d,   a,   x(3),   s14,   -1044525330
        ff   a,   b,   c,   d,   x(4),   s11,   -176418897
        ff   d,   a,   b,   c,   x(5),   s12,   1200080426
        ff   c,   d,   a,   b,   x(6),   s13,   -1473231341
        ff   b,   c,   d,   a,   x(7),   s14,   -45705983
        ff   a,   b,   c,   d,   x(8),   s11,   1770035416
        ff   d,   a,   b,   c,   x(9),   s12,   -1958414417
        ff   c,   d,   a,   b,   x(10),   s13,   -42063
        ff   b,   c,   d,   a,   x(11),   s14,   -1990404162
        ff   a,   b,   c,   d,   x(12),   s11,   1804603682
        ff   d,   a,   b,   c,   x(13),   s12,   -40341101
        ff   c,   d,   a,   b,   x(14),   s13,   -1502002290
        ff   b,   c,   d,   a,   x(15),   s14,   1236535329
       
        '   round   2
        gg   a,   b,   c,   d,   x(1),   s21,   -165796510
        gg   d,   a,   b,   c,   x(6),   s22,   -1069501632
        gg   c,   d,   a,   b,   x(11),   s23,   643717713
        gg   b,   c,   d,   a,   x(0),   s24,   -373897302
        gg   a,   b,   c,   d,   x(5),   s21,   -701558691
        gg   d,   a,   b,   c,   x(10),   s22,   38016083
        gg   c,   d,   a,   b,   x(15),   s23,   -660478335
        gg   b,   c,   d,   a,   x(4),   s24,   -405537848
        gg   a,   b,   c,   d,   x(9),   s21,   568446438
        gg   d,   a,   b,   c,   x(14),   s22,   -1019803690
        gg   c,   d,   a,   b,   x(3),   s23,   -187363961
        gg   b,   c,   d,   a,   x(8),   s24,   1163531501
        gg   a,   b,   c,   d,   x(13),   s21,   -1444681467
        gg   d,   a,   b,   c,   x(2),   s22,   -51403784
        gg   c,   d,   a,   b,   x(7),   s23,   1735328473
        gg   b,   c,   d,   a,   x(12),   s24,   -1926607734
       
        '   round   3
        hh   a,   b,   c,   d,   x(5),   s31,   -378558
        hh   d,   a,   b,   c,   x(8),   s32,   -2022574463
        hh   c,   d,   a,   b,   x(11),   s33,   1839030562
        hh   b,   c,   d,   a,   x(14),   s34,   -35309556
        hh   a,   b,   c,   d,   x(1),   s31,   -1530992060
        hh   d,   a,   b,   c,   x(4),   s32,   1272893353
        hh   c,   d,   a,   b,   x(7),   s33,   -155497632
        hh   b,   c,   d,   a,   x(10),   s34,   -1094730640
        hh   a,   b,   c,   d,   x(13),   s31,   681279174
        hh   d,   a,   b,   c,   x(0),   s32,   -358537222
        hh   c,   d,   a,   b,   x(3),   s33,   -722521979
        hh   b,   c,   d,   a,   x(6),   s34,   76029189
        hh   a,   b,   c,   d,   x(9),   s31,   -640364487
        hh   d,   a,   b,   c,   x(12),   s32,   -421815835
        hh   c,   d,   a,   b,   x(15),   s33,   530742520
        hh   b,   c,   d,   a,   x(2),   s34,   -995338651
       
        '   round   4
        ii   a,   b,   c,   d,   x(0),   s41,   -198630844
        ii   d,   a,   b,   c,   x(7),   s42,   1126891415
        ii   c,   d,   a,   b,   x(14),   s43,   -1416354905
        ii   b,   c,   d,   a,   x(5),   s44,   -57434055
        ii   a,   b,   c,   d,   x(12),   s41,   1700485571
        ii   d,   a,   b,   c,   x(3),   s42,   -1894986606
        ii   c,   d,   a,   b,   x(10),   s43,   -1051523
        ii   b,   c,   d,   a,   x(1),   s44,   -2054922799
        ii   a,   b,   c,   d,   x(8),   s41,   1873313359
        ii   d,   a,   b,   c,   x(15),   s42,   -30611744
        ii   c,   d,   a,   b,   x(6),   s43,   -1560198380
        ii   b,   c,   d,   a,   x(13),   s44,   1309151649
        ii   a,   b,   c,   d,   x(4),   s41,   -145523070
        ii   d,   a,   b,   c,   x(11),   s42,   -1120210379
        ii   c,   d,   a,   b,   x(2),   s43,   718787259
        ii   b,   c,   d,   a,   x(9),   s44,   -343485551
       
       
        state(1)   =   longoverflowadd(state(1),   a)
        state(2)   =   longoverflowadd(state(2),   b)
        state(3)   =   longoverflowadd(state(3),   c)
        state(4)   =   longoverflowadd(state(4),   d)

'     /*   zeroize   sensitive   information.
'*/
'     md5_memset   ((pointer)x,   0,   sizeof   (x));
       
end   sub

private   sub   decode(length   as   integer,   outputbuffer()   as   long,   inputbuffer()   as   byte)
        dim   intdblindex   as   integer
        dim   intbyteindex   as   integer
        dim   dblsum   as   double
       
        intdblindex   =   0
        for   intbyteindex   =   0   to   length   -   1   step   4
                dblsum   =   inputbuffer(intbyteindex)   +   _
                                                                        inputbuffer(intbyteindex   +   1)   *   256#   +   _
                                                                        inputbuffer(intbyteindex   +   2)   *   65536#   +   _
                                                                        inputbuffer(intbyteindex   +   3)   *   16777216#
                outputbuffer(intdblindex)   =   unsignedtolong(dblsum)
                intdblindex   =   intdblindex   +   1
        next   intbyteindex
end   sub

'
'   ff,   gg,   hh,   and   ii   transformations   for   rounds   1,   2,   3,   and   4.
'   rotation   is   separate   from   addition   to   prevent   recomputation.
'
private   function   ff(a   as   long,   _
                                        b   as   long,   _
                                        c   as   long,   _
                                        d   as   long,   _
                                        x   as   long,   _
                                        s   as   long,   _
                                        ac   as   long)   as   long
        a   =   longoverflowadd4(a,   (b   and   c)   or   (not   (b)   and   d),   x,   ac)
        a   =   longleftrotate(a,   s)
        a   =   longoverflowadd(a,   b)
end   function

private   function   gg(a   as   long,   _
                                        b   as   long,   _
                                        c   as   long,   _
                                        d   as   long,   _
                                        x   as   long,   _
                                        s   as   long,   _
                                        ac   as   long)   as   long
        a   =   longoverflowadd4(a,   (b   and   d)   or   (c   and   not   (d)),   x,   ac)
        a   =   longleftrotate(a,   s)
        a   =   longoverflowadd(a,   b)
end   function

private   function   hh(a   as   long,   _
                                        b   as   long,   _
                                        c   as   long,   _
                                        d   as   long,   _
                                        x   as   long,   _
                                        s   as   long,   _
                                        ac   as   long)   as   long
        a   =   longoverflowadd4(a,   b   xor   c   xor   d,   x,   ac)
        a   =   longleftrotate(a,   s)
        a   =   longoverflowadd(a,   b)
end   function

private   function   ii(a   as   long,   _
                                        b   as   long,   _
                                        c   as   long,   _
                                        d   as   long,   _
                                        x   as   long,   _
                                        s   as   long,   _
                                        ac   as   long)   as   long
        a   =   longoverflowadd4(a,   c   xor   (b   or   not   (d)),   x,   ac)
        a   =   longleftrotate(a,   s)
        a   =   longoverflowadd(a,   b)
end   function

'
'   rotate   a   long   to   the   right
'
function   longleftrotate(value   as   long,   bits   as   long)   as   long
        dim   lngsign   as   long
        dim   lngi   as   long
        bits   =   bits   mod   32
        if   bits   =   0   then   longleftrotate   =   value:   exit   function
        for   lngi   =   1   to   bits
                lngsign   =   value   and   &hc0000000
                value   =   (value   and   &h3fffffff)   *   2
                value   =   value   or   ((lngsign   <   0)   and   1)   or   (cbool(lngsign   and   _
                                &h40000000)   and   &h80000000)
        next
        longleftrotate   =   value
end   function

'
'   function   to   add   two   unsigned   numbers   together   as   in   c.
'   overflows   are   ignored!
'
private   function   longoverflowadd(val1   as   long,   val2   as   long)   as   long
        dim   lnghighword   as   long
        dim   lnglowword   as   long
        dim   lngoverflow   as   long

发表于:2007-06-27 01:22:013楼 得分:0
lnglowword   =   (val1   and   &hffff&)   +   (val2   and   &hffff&)
        lngoverflow   =   lnglowword   \   65536
        lnghighword   =   (((val1   and   &hffff0000)   \   65536)   +   ((val2   and   &hffff0000)   \   65536)   +   lngoverflow)   and   &hffff&
        longoverflowadd   =   unsignedtolong((lnghighword   *   65536#)   +   (lnglowword   and   &hffff&))
end   function

'
'   function   to   add   two   unsigned   numbers   together   as   in   c.
'   overflows   are   ignored!
'
private   function   longoverflowadd4(val1   as   long,   val2   as   long,   val3   as   long,   val4   as   long)   as   long
        dim   lnghighword   as   long
        dim   lnglowword   as   long
        dim   lngoverflow   as   long

        lnglowword   =   (val1   and   &hffff&)   +   (val2   and   &hffff&)   +   (val3   and   &hffff&)   +   (val4   and   &hffff&)
        lngoverflow   =   lnglowword   \   65536
        lnghighword   =   (((val1   and   &hffff0000)   \   65536)   +   _
                                      ((val2   and   &hffff0000)   \   65536)   +   _
                                      ((val3   and   &hffff0000)   \   65536)   +   _
                                      ((val4   and   &hffff0000)   \   65536)   +   _
                                      lngoverflow)   and   &hffff&
        longoverflowadd4   =   unsignedtolong((lnghighword   *   65536#)   +   (lnglowword   and   &hffff&))
end   function

'
'   convert   an   unsigned   double   into   a   long
'
private   function   unsignedtolong(value   as   double)   as   long
                if   value   <   0   or   value   > =   offset_4   then   error   6   '   overflow
                if   value   <=   maxint_4   then
                    unsignedtolong   =   value
                else
                    unsignedtolong   =   value   -   offset_4
                end   if
            end   function

'
'   convert   a   long   to   an   unsigned   double
'
private   function   longtounsigned(value   as   long)   as   double
                if   value   <   0   then
                    longtounsigned   =   value   +   offset_4
                else
                    longtounsigned   =   value
                end   if
end   function


--------
2,3,4楼为用的md5加密算法
发表于:2007-06-27 08:28:314楼 得分:2
学习
发表于:2007-07-04 09:07:365楼 得分:2
学习
发表于:2007-07-04 09:27:456楼 得分:2
学习
发表于:2007-07-04 10:53:277楼 得分:10
网上很多,找一下。不过要do   fully   testing。
发表于:2007-07-04 12:50:428楼 得分:40
这句有问题:
private   function   stringtoarray(instring   as   string)   as   byte()
dim   i   as   integer
dim   bytbuffer()   as   byte
redim   bytbuffer(len(instring))
for   i   =   0   to   len(instring)   -   1
bytbuffer(i)   =   asc(mid(instring,   i   +   1,   1))
next   i
stringtoarray   =   bytbuffer
end   function
发表于:2007-07-05 17:05:169楼 得分:2
太牛了,看不懂!
发表于:2007-07-05 17:18:3810楼 得分:40
stringtoarray   这个函数可以直接用strconv代替

stringtoarray     =   strconv(instring,vbfromunicode)
发表于:2007-07-05 17:30:2711楼 得分:2
学习


快速检索

最新资讯
热门点击