MD5加密用PB实现
将下面2个对象依次导入程序,然后声明nvo_ez_md5 lnv
ls_result = lnv.of_compute('abc')
-----------------------------------------------------------------
一、nvo_ez_bitwise
$PBExportHeader$nvo_ez_bitwise.sru
$PBExportComments$二进制位操作
forward
global type nvo_ez_bitwise from nonvisualobject
end type
end forward
global type nvo_ez_bitwise from nonvisualobject autoinstantiate
end type
type prototypes
end prototypes
type variables
end variables
forward prototypes
public function string of_dec2bin (unsignedlong k)
public function ulong of_bin2dec (readonly string as_bin)
public subroutine of_dec2bin (ulong k, ref ulong r)
public function string of_dec2hex (unsignedlong k)
public function ulong of_hex2dec (readonly string as_hex)
public function unsignedlong bitshr (unsignedlong k, integer bit)
public function unsignedlong bitshl (unsignedlong k, integer bit)
public function unsignedlong bitnot (unsignedlong k)
public function unsignedlong bitrol (unsignedlong k, unsignedlong bit)
public function ulong bitand (ulong k1, ulong k2)
public function ulong bitor (ulong k1, ulong k2)
public function ulong bitxor (ulong k1, ulong k2)
end prototypes
public function string of_dec2bin (unsignedlong k);string ls
long i, ld
ld = 1
for i = 1 to 31
if mod(k,ld + ld) >= ld then
ls = '1' + ls
else
ls = '0' + ls
end if
ld += ld
next
if k >= ld then return '1' + ls
return '0' + ls
end function
public function ulong of_bin2dec (readonly string as_bin);ulong lr, ld = 1
long i
for i = 32 to 1 step -1
if mid( as_bin, i, 1) = '1' then
lr += ld
end if
ld += ld
next
return lr
end function
public subroutine of_dec2bin (ulong k, ref ulong r);long i
ulong ll
ll = 1
for i = 1 to 31
if mod(k,ll + ll) >= ll then r = ll
ll += ll
next
if k >= ll then r = ll
end subroutine
public function string of_dec2hex (unsignedlong k);char Hex = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}
string ls
do while k > 0
ls = Hex + ls
k /= 16
loop
return ls
end function
public function ulong of_hex2dec (readonly string as_hex);constant int C0 = asc('0')
constant int CA = asc('A')
constant int CAs = asc('a')
ulong k, ld = 1
int lc, i
for i = len(as_hex) to 1 step -1
lc = asc(mid(as_hex,i,1))
if lc > CA then
k += ld * (lc - CA + 10)
elseif lc > CAs then
k += ld * (lc - CAs + 10)
else
k += ld * (lc - C0)
end if
ld *= 16
next
return k
end function
public function unsignedlong bitshr (unsignedlong k, integer bit);return k / (2^bit)
end function
public function unsignedlong bitshl (unsignedlong k, integer bit);return k * 2^bit
end function
public function unsignedlong bitnot (unsignedlong k);return 4294967295 - k
end function
public function unsignedlong bitrol (unsignedlong k, unsignedlong bit);return BitShl(k,bit) + BitShr(k,32 - bit)
end function
public function ulong bitand (ulong k1, ulong k2);ulong lm = 1, lr
do
if mod(k1,lm + lm) >= lm then
if mod(k2,lm + lm) >= lm then lr += lm
end if
lm += lm
if k1 < lm then return lr
if k2 < lm then return lr
if lm = 2147483648 then return lr + lm
loop while true
end function
public function ulong bitor (ulong k1, ulong k2);ulong lm = 1, lr
do
if mod(k1,lm + lm) >= lm then
lr += lm
elseif mod(k2,lm + lm) >= lm then
lr += lm
end if
lm += lm
if k1 < lm then
if k2 < lm then return lr
end if
if lm = 2147483648 then return lr + lm
loop while true
end function
public function ulong bitxor (ulong k1, ulong k2);ulong lm = 1, lr
do
if (mod(k1,lm + lm) >= lm) <> (mod(k2,lm + lm) >= lm) then lr += lm
lm += lm
if k1 < lm then
if k2 < lm then return lr
end if
if lm = 2147483648 then
if (k1 < lm) <> (k2 < lm) then return lr + lm
return lr
end if
loop while true
end function
on nvo_ez_bitwise.create
TriggerEvent( this, "constructor" )
end on
on nvo_ez_bitwise.destroy
TriggerEvent( this, "destructor" )
end on
二、nvo_ez_md5$PBExportHeader$nvo_ez_md5.sru
$PBExportComments$MD5加密算法
forward
global type nvo_ez_md5 from nvo_ez_bitwise
end type
end forward
global type nvo_ez_md5 from nvo_ez_bitwise
end type
type variables
constant long MD5_BYTESIZE = 64
constant long MD5_WORDSIZE = 16
constant ulong cAA = 1732584193
constant ulong cBB = 4023233417
constant ulong cCC = 2562383102
constant ulong cDD = 271733878
constant ulong CF0 = 3614090360;
constant ulong CF1 = 3905402710;
constant ulong CF2 = 606105819;
constant ulong CF3 = 3250441966;
constant ulong CF4 = 4118548399;
constant ulong CF5 = 1200080426;
constant ulong CF6 = 2821735955;
constant ulong CF7 = 4249261313;
constant ulong CF8 = 1770035416;
constant ulong CF9 = 2336552879;
constant ulong CF10 = 4294925233;
constant ulong CF11 = 2304563134;
constant ulong CF12 = 1804603682;
constant ulong CF13 = 4254626195;
constant ulong CF14 = 2792965006;
constant ulong CF15 = 1236535329;
constant ulong CG0 = 4129170786;
constant ulong CG1 = 3225465664;
constant ulong CG2 = 643717713;
constant ulong CG3 = 3921069994;
constant ulong CG4 = 3593408605;
constant ulong CG5 = 38016083;
constant ulong CG6 = 3634488961;
constant ulong CG7 = 3889429448;
constant ulong CG8 = 568446438;
constant ulong CG9 = 3275163606;
constant ulong CG10 = 4107603335;
constant ulong CG11 = 1163531501;
constant ulong CG12 = 2850285829;
constant ulong CG13 = 4243563512;
constant ulong CG14 = 1735328473;
constant ulong CG15 = 2368359562;
constant ulong CH0 = 4294588738;
constant ulong CH1 = 2272392833;
constant ulong CH2 = 1839030562;
constant ulong CH3 = 4259657740;
constant ulong CH4 = 2763975236;
constant ulong CH5 = 1272893353;
constant ulong CH6 = 4139469664;
constant ulong CH7 = 3200236656;
constant ulong CH8 = 681279174;
constant ulong CH9 = 3936430074;
constant ulong CH10 = 3572445317;
constant ulong CH11 = 76029189;
constant ulong CH12 = 3654602809;
constant ulong CH13 = 3873151461;
constant ulong CH14 = 530742520;
constant ulong CH15 = 3299628645;
constant ulong CI0 = 4096336452;
constant ulong CI1 = 1126891415;
constant ulong CI2 = 2878612391;
constant ulong CI3 = 4237533241;
constant ulong CI4 = 1700485571;
constant ulong CI5 = 2399980690;
constant ulong CI6 = 4293915773;
constant ulong CI7 = 2240044497;
constant ulong CI8 = 1873313359;
constant ulong CI9 = 4264355552;
constant ulong CI10 = 2734768916;
constant ulong CI11 = 1309151649;
constant ulong CI12 = 4149444226;
constant ulong CI13 = 3174756917;
constant ulong CI14 = 718787259;
constant ulong CI15 = 3951481745;
end variables
forward prototypes
public function string of_compute (string as_password)
public subroutine ii (ref unsignedlong la, unsignedlong lb, unsignedlong lc, unsignedlong ld, unsignedlong mj, unsignedlong s, unsignedlong ti)
public subroutine gg (ref unsignedlong la, unsignedlong lb, unsignedlong lc, unsignedlong ld, unsignedlong mj, unsignedlong s, unsignedlong ti)
public subroutine ff (ref unsignedlong la, unsignedlong lb, unsignedlong lc, unsignedlong ld, unsignedlong mj, unsignedlong s, unsignedlong ti)
public subroutine hh (ref unsignedlong la, unsignedlong lb, unsignedlong lc, unsignedlong ld, unsignedlong mj, unsignedlong s, unsignedlong ti)
protected function string pof_hex (unsignedlong k)
end prototypes
public function string of_compute (string as_password);ulong m[], lv
long ll_len, ll_full
long i, j, k
/*首先需要对信息进行填充,使其字节长度对512求余的结果等于448。
因此,信息的字节长度(bits length)将被扩展至n*512+448,即n*64+56个字节(bytes),
n为一个正整数。填充的方法如下,在信息的后面填充一个1和无数个0,
直到满足上面的条件时才停止用0对信息的填充。
然后,在在这个结果后面附加一个以64位二进制表示的填充前信息长度。
经过这两步的处理,现在的信息字节长度=n*512+448+64=(n+1)*512,即长度恰好是512的整数倍。
*/
ll_len = len(as_password)
ll_full = long(ll_len / MD5_BYTESIZE) * MD5_BYTESIZE + MD5_BYTESIZE
k = mod(ll_len,MD5_BYTESIZE)
if k > 56 then ll_full += MD5_BYTESIZE
if k <> 56 then as_password = as_password + char(128)
m = BitShl( ll_len, 3)
m = BitShr( ll_len, 29)
//
k = 1
ll_len = len(as_password)
for i = 1 to ll_len
m = BitShl(asc(mid(as_password,i,1)),j) + m
j += 8
if j = 32 then
k ++
j = 0
end if
next
//
ulong a, b, c, d, aa, bb, cc, dd
k = 1
a = CAA; b = CBB; c = CCC; d = CDD
for i = 1 to ll_full / MD5_BYTESIZE
aa = a; bb = b; cc = c; dd = d;
ff(a,b,c,d,m, 7,CF0);
ff(d,a,b,c,m, 12,CF1);
ff(c,d,a,b,m, 17,CF2);
ff(b,c,d,a,m, 22,CF3);
ff(a,b,c,d,m, 7,CF4);
ff(d,a,b,c,m, 12,CF5);
ff(c,d,a,b,m, 17,CF6);
ff(b,c,d,a,m, 22,CF7);
ff(a,b,c,d,m, 7,CF8);
ff(d,a,b,c,m, 12,CF9);
ff(c,d,a,b,m,17,CF10);
ff(b,c,d,a,m,22,CF11);
ff(a,b,c,d,m, 7,CF12);
ff(d,a,b,c,m,12,CF13);
ff(c,d,a,b,m,17,CF14);
ff(b,c,d,a,m,22,CF15);
//second round
gg(a,b,c,d,m, 5,CG0);
gg(d,a,b,c,m, 9,CG1);
gg(c,d,a,b,m,14,CG2);
gg(b,c,d,a,m, 20,CG3);
gg(a,b,c,d,m, 5,CG4);
gg(d,a,b,c,m, 9,CG5);
gg(c,d,a,b,m,14,CG6);
gg(b,c,d,a,m, 20,CG7);
gg(a,b,c,d,m, 5,CG8);
gg(d,a,b,c,m, 9,CG9);
gg(c,d,a,b,m, 14,CG10);
gg(b,c,d,a,m, 20,CG11);
gg(a,b,c,d,m, 5,CG12);
gg(d,a,b,c,m, 9,CG13);
gg(c,d,a,b,m, 14,CG14);
gg(b,c,d,a,m,20,CG15);
// 第三轮
hh(a,b,c,d,m, 4,CH0);
hh(d,a,b,c,m, 11,CH1);
hh(c,d,a,b,m,16,CH2);
hh(b,c,d,a,m,23,CH3);
hh(a,b,c,d,m, 4,CH4);
hh(d,a,b,c,m, 11,CH5);
hh(c,d,a,b,m, 16,CH6);
hh(b,c,d,a,m,23,CH7);
hh(a,b,c,d,m, 4,CH8);
hh(d,a,b,c,m, 11,CH9);
hh(c,d,a,b,m, 16,CH10);
hh(b,c,d,a,m, 23,CH11);
hh(a,b,c,d,m, 4,CH12);
hh(d,a,b,c,m,11,CH13);
hh(c,d,a,b,m,16,CH14);
hh(b,c,d,a,m, 23,CH15);
// 第四轮
ii(a,b,c,d,m, 6,CI0);
ii(d,a,b,c,m, 10,CI1);
ii(c,d,a,b,m,15,CI2);
ii(b,c,d,a,m, 21,CI3);
ii(a,b,c,d,m, 6,CI4);
ii(d,a,b,c,m, 10,CI5);
ii(c,d,a,b,m,15,CI6);
ii(b,c,d,a,m, 21,CI7);
ii(a,b,c,d,m, 6,CI8);
ii(d,a,b,c,m,10,CI9);
ii(c,d,a,b,m, 15,CI10);
ii(b,c,d,a,m,21,CI11);
ii(a,b,c,d,m, 6,CI12);
ii(d,a,b,c,m,10,CI13);
ii(c,d,a,b,m, 15,CI14);
ii(b,c,d,a,m, 21,CI15);
a += aa; b += bb; c += cc; d += dd
k += 16
next
return lower(pof_hex(a) + pof_hex(b) + pof_hex(c) + pof_hex(d))
end function
public subroutine ii (ref unsignedlong la, unsignedlong lb, unsignedlong lc, unsignedlong ld, unsignedlong mj, unsignedlong s, unsignedlong ti);//ly xor ( lx or (not lz));
la += BitXor( lc, BitOr( lb, BitNot(ld) ) ) + mj + ti
la = lb + BitRol(la,s)
end subroutine
public subroutine gg (ref unsignedlong la, unsignedlong lb, unsignedlong lc, unsignedlong ld, unsignedlong mj, unsignedlong s, unsignedlong ti);//(lb and ld) or (lc and(not ld));
la += BitOr( BitAnd(lb, ld), BitAnd(lc,BitNot(ld)) ) + mj + ti
la = lb + BitRol(la,s)
end subroutine
public subroutine ff (ref unsignedlong la, unsignedlong lb, unsignedlong lc, unsignedlong ld, unsignedlong mj, unsignedlong s, unsignedlong ti);la += BitOr( BitAnd(lb,lc), BitAnd( BitNot(lb), ld) ) + mj + ti
la = lb + BitRol(la,s)
end subroutine
public subroutine hh (ref unsignedlong la, unsignedlong lb, unsignedlong lc, unsignedlong ld, unsignedlong mj, unsignedlong s, unsignedlong ti);//lx xor ly xor lz;
la += BitXor( BitXor( lb, lc ), ld ) + mj + ti
la = lb + BitRol(la,s)
end subroutine
protected function string pof_hex (unsignedlong k);string lr
//交换位置
lr = of_dec2hex(k)
lr = mid(lr,7,2) + mid(lr,5,2) + mid(lr,3,2) + mid(lr,1,2)
return lr
end function
on nvo_ez_md5.create
TriggerEvent( this, "constructor" )
end on
on nvo_ez_md5.destroy
TriggerEvent( this, "destructor" )
end on
---------- Compiler: Errors (09:48:09)
(nvo_ez_bitwise).nvo_ez_bitwise.of_dec2bin.6: Error C0070: Mixing array and non-array in assignment statement.
(nvo_ez_bitwise).nvo_ez_bitwise.of_dec2bin.9: Error C0003: Condition for if statement must be a boolean.
---------- Finished Errors (09:48:09)
页:
[1]