实例
加密文本
加密文件
其中n_func_file功能为读写文件,使用和源代码参见另一篇文章PB读写文件
源代码
位运算计算器
代码拷贝到文本编辑器,另存为 n_bit_calc.sru,导入pbl
发现BUG请留言或私信,以便修正(QQ:768310524 TEL:18649713925)
$PBExportHeader$n_bit_calc.sru
forward
global type n_bit_calc from nonvisualobject
end type
end forward
global type n_bit_calc from nonvisualobject autoinstantiate
end type
type variables
private:
CONSTANT INTEGER BIT = 32
end variables
forward prototypes
public function string to_bin (long x)
public function string to_hex (long x)
public function long from_hex (string hex)
public function long from_bin (string bin)
public function string hex (unsignedlong x)
public function string bin (unsignedlong x)
public function string bit_sum (string bin[])
public function string bit_and (string x, string y)
public function string bit_or (string x, string y)
public function string bit_lshift (string x, integer y)
public function string bit_rshift (string x, integer y)
public function string bit_urshift (string x, integer y)
public function long bit_and (long x, long y)
public function long bit_or (long x, long y)
public function long bit_xor (long x, long y)
public function string bit_not (string x)
public function long bit_not (long x)
public function long bit_lshift (long x, integer y)
public function long bit_urshift (long x, integer y)
public function long bit_rshift (long x, integer y)
public function string bit_xor (string x, string y)
end prototypes
public function string to_bin (long x);string ls_bin
//传入负数赋值给ulong,通过溢出直接计算出补码
ls_bin = bin(x)
return fill('0',BIT - len(ls_bin)) + ls_bin
end function
public function string to_hex (long x);string ls_hex
//传入负数赋值给ulong,通过溢出直接计算出补码
ls_hex = hex(x)
return fill('0',BIT / 4 - len(ls_hex)) + ls_hex
end function
public function long from_hex (string hex);ulong lul_z
char lc[]
int i
lc = hex
for i = 1 to upperbound(lc)
if i > 1 then lul_z *= 16
if isnumber(lc[i]) then
lul_z += integer(lc[i])
else
lul_z += asc(lc[i]) - 55
end if
next
return lul_z
end function
public function long from_bin (string bin);ulong lul_z
char lc[]
int i
//二进制符号位为1的,通过赋值给long溢出得到负数
lc = bin
for i = 1 to upperbound(lc)
if i > 1 then lul_z *= 2
if lc[i] = '1' then lul_z += 1
next
return lul_z
end function
public function string hex (unsignedlong x);int li_r
string ls_hex
//除16取余,逆序拼接
do while x > 15
li_r = mod(x,16)
x /= 16
if li_r < 10 then
ls_hex = string(li_r) + ls_hex
else
ls_hex = char(li_r + 55) + ls_hex
end if
loop
if x < 10 then
ls_hex = string(x) + ls_hex
else
ls_hex = char(x + 55) + ls_hex
end if
return ls_hex
end function
public function string bin (unsignedlong x);ulong lul_r
string ls_bin
//除2取余,逆序拼接
do while x > 1
lul_r = mod(x,2)
x /= 2
ls_bin = string(lul_r) + ls_bin
loop
ls_bin = string(x) + ls_bin
return ls_bin
end function
public function string bit_sum (string bin[]);int i
long ll_z
for i = 1 to upperbound(bin)
ll_z += from_bin(bin[i])
next
return to_bin(ll_z)
end function
public function string bit_and (string x, string y);char lcx[],lcy[]
string z
int i
lcx = fill('0',BIT - len(x)) + x
lcy = fill('0',BIT - len(y)) + y
for i = 1 to BIT
if lcx[i] = '1' and lcy[i] = '1' then
z += '1'
else
z += '0'
end if
next
return z
end function
public function string bit_or (string x, string y);char lcx[],lcy[]
string z
int i
lcx = fill('0',BIT - len(x)) + x
lcy = fill('0',BIT - len(y)) + y
for i = 1 to BIT
if lcx[i] = '1' or lcy[i] = '1' then
z += '1'
else
z += '0'
end if
next
return z
end function
public function string bit_lshift (string x, integer y);x = fill('0',BIT - len(x)) + x
y = mod(y,BIT)
if y < 0 then y += BIT
return mid(x,y + 1) + fill('0',y)
end function
public function string bit_rshift (string x, integer y);x = fill('0',BIT - len(x)) + x
y = mod(y,BIT)
if y < 0 then y += BIT
return fill(left(x,1),y) + left(x,BIT - y)
end function
public function string bit_urshift (string x, integer y);x = fill('0',BIT - len(x)) + x
y = mod(y,BIT)
if y < 0 then y += BIT
return fill('0',y) + left(x,BIT - y)
end function
public function long bit_and (long x, long y);return from_bin(bit_and(to_bin(x),to_bin(y)))
end function
public function long bit_or (long x, long y);return from_bin(bit_or(to_bin(x),to_bin(y)))
end function
public function long bit_xor (long x, long y);return from_bin(bit_xor(to_bin(x),to_bin(y)))
end function
public function string bit_not (string x);char lcx[]
string z
int i
lcx = fill('0',BIT - len(x)) + x
for i = 1 to BIT
if lcx[i] = '1' then
z += '0'
else
z += '1'
end if
next
return z
end function
public function long bit_not (long x);return from_bin(bit_not(to_bin(x)))
end function
public function long bit_lshift (long x, integer y);return from_bin(bit_lshift(to_bin(x),y))
end function
public function long bit_urshift (long x, integer y);return from_bin(bit_urshift(to_bin(x),y))
end function
public function long bit_rshift (long x, integer y);return from_bin(bit_rshift(to_bin(x),y))
end function
public function string bit_xor (string x, string y);char lcx[],lcy[]
string z
int i
lcx = fill('0',BIT - len(x)) + x
lcy = fill('0',BIT - len(y)) + y
for i = 1 to BIT
if lcx[i] = lcy[i] then
z += '0'
else
z += '1'
end if
next
return z
end function
on n_bit_calc.create
call super::create
TriggerEvent( this, "constructor" )
end on
on n_bit_calc.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on
MD5算法
算法参考百度百科MD5词条中JAVA实现部分
代码拷贝到文本编辑器,另存为 n_func_file.sru,导入pbl
发现BUG请留言或私信,以便修正(QQ:768310524 TEL:18649713925)
$PBExportHeader$n_md5.sru
forward
global type n_md5 from nonvisualobject
end type
end forward
global type n_md5 from nonvisualobject autoinstantiate
end type
type prototypes
end prototypes
type variables
PRIVATE:
CONSTANT STRING initA='01100111010001010010001100000001' //0x67452301
CONSTANT STRING initB='11101111110011011010101110001001' //0xefcdab89
CONSTANT STRING initC='10011000101110101101110011111110' //0x98badcfe
CONSTANT STRING initD='00010000001100100101010001110110' //0x10325476
STRING tempA,tempB,tempC,tempD
// 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
// 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,0x698098d8,
// 0x8b44f7af,0xffff5bb1,0x895cd7be,0x6b901122,0xfd987193,
// 0xa679438e,0x49b40821,0xf61e2562,0xc040b340,0x265e5a51,
// 0xe9b6c7aa,0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
// 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,0xa9e3e905,
// 0xfcefa3f8,0x676f02d9,0x8d2a4c8a,0xfffa3942,0x8771f681,
// 0x6d9d6122,0xfde5380c,0xa4beea44,0x4bdecfa9,0xf6bb4b60,
// 0xbebfbc70,0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
// 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,0xf4292244,
// 0x432aff97,0xab9423a7,0xfc93a039,0x655b59c3,0x8f0ccc92,
// 0xffeff47d,0x85845dd1,0x6fa87e4f,0xfe2ce6e0,0xa3014314,
// 0x4e0811a1,0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391
STRING K[]={'11010111011010101010010001111000',&
'11101000110001111011011101010110',&
'00100100001000000111000011011011',&
'11000001101111011100111011101110',&
'11110101011111000000111110101111',&
'01000111100001111100011000101010',&
'10101000001100000100011000010011',&
'11111101010001101001010100000001',&
'01101001100000001001100011011000',&
'10001011010001001111011110101111',&
'11111111111111110101101110110001',&
'10001001010111001101011110111110',&
'01101011100100000001000100100010',&
'11111101100110000111000110010011',&
'10100110011110010100001110001110',&
'01001001101101000000100000100001',&
'11110110000111100010010101100010',&
'11000000010000001011001101000000',&
'00100110010111100101101001010001',&
'11101001101101101100011110101010',&
'11010110001011110001000001011101',&
'00000010010001000001010001010011',&
'11011000101000011110011010000001',&
'11100111110100111111101111001000',&
'00100001111000011100110111100110',&
'11000011001101110000011111010110',&
'11110100110101010000110110000111',&
'01000101010110100001010011101101',&
'10101001111000111110100100000101',&
'11111100111011111010001111111000',&
'01100111011011110000001011011001',&
'10001101001010100100110010001010',&
'11111111111110100011100101000010',&
'10000111011100011111011010000001',&
'01101101100111010110000100100010',&
'11111101111001010011100000001100',&
'10100100101111101110101001000100',&
'01001011110111101100111110101001',&
'11110110101110110100101101100000',&
'10111110101111111011110001110000',&
'00101000100110110111111011000110',&
'11101010101000010010011111111010',&
'11010100111011110011000010000101',&
'00000100100010000001110100000101',&
'11011001110101001101000000111001',&
'11100110110110111001100111100101',&
'00011111101000100111110011111000',&
'11000100101011000101011001100101',&
'11110100001010010010001001000100',&
'01000011001010101111111110010111',&
'10101011100101000010001110100111',&
'11111100100100111010000000111001',&
'01100101010110110101100111000011',&
'10001111000011001100110010010010',&
'11111111111011111111010001111101',&
'10000101100001000101110111010001',&
'01101111101010000111111001001111',&
'11111110001011001110011011100000',&
'10100011000000010100001100010100',&
'01001110000010000001000110100001',&
'11110111010100110111111010000010',&
'10111101001110101111001000110101',&
'00101010110101111101001010111011',&
'11101011100001101101001110010001'}
INT s[]={7,12,17,22,7,12,17,22,7,12,17,22,7,&
12,17,22,5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,&
4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,6,10,&
15,21,6,10,15,21,6,10,15,21,6,10,15,21}
n_bit_calc calc
end variables
forward prototypes
private subroutine init ()
private function string shift (string a, integer i)
private subroutine mainloop (string bin[])
private function string changehex (string a)
public function string getmd5 (blob ablb)
private subroutine add (blob ablb, ref string strb[])
end prototypes
private subroutine init ();tempA=initA
tempB=initB
tempC=initC
tempD=initD
end subroutine
private function string shift (string a, integer i);return calc.bit_or(calc.bit_lshift(a,i),calc.bit_urshift(a,32 - i))
end function
private subroutine mainloop (string bin[]);string F
int g
string a,b,c,d
int i
string tmp
a = tempA
b = tempB
c = tempC
d = tempD
for i = 0 to 63
if i < 16 then
F = calc.bit_or(calc.bit_and(b,c),calc.bit_and(calc.bit_not(b),d))
g = i
elseif i < 32 then
F = calc.bit_or(calc.bit_and(d,b),calc.bit_and(calc.bit_not(d),c))
g = mod(5 * i + 1,16)
elseif i < 48 then
F = calc.bit_xor(calc.bit_xor(b,c),d)
g = mod(3 * i + 5,16)
else
F = calc.bit_xor(c,calc.bit_or(b,calc.bit_not(d)))
g = mod(7 * i,16)
end if
tmp = d
d = c
c = b
b = calc.bit_sum({b,shift(calc.bit_sum({a,F,K[i + 1],bin[g + 1]}),s[i + 1])})
a = tmp
next
tempA = calc.bit_sum({a,tempA})
tempB = calc.bit_sum({b,tempB})
tempC = calc.bit_sum({c,tempC})
tempD = calc.bit_sum({d,tempD})
return
end subroutine
private function string changehex (string a);string str,ls_hex
int i
for i = 0 to 3
ls_hex = calc.hex(calc.bit_and(mod(calc.from_bin(calc.bit_rshift(a,i * 8)),256),255))
ls_hex = fill('0',2 - len(ls_hex)) + ls_hex
str += ls_hex
next
return str
end function
public function string getmd5 (blob ablb);string strByte[]
string num[],nil[]
int i,j
init()
add(ablb,strByte)
for i = 0 to upperbound(strByte) / 16 - 1
num = nil
for j = 1 to 16
num[j] = strByte[i * 16 + j]
next
mainLoop(num)
next
return changeHex(tempA) + changeHex(tempB) + changeHex(tempC) + changeHex(tempD)
end function
private subroutine add (blob ablb, ref string strb[]);byte lbyte[]
long ll_len,num
string strByte[]
int i,j
lbyte = getByteArray(ablb)
ll_len = upperbound(lbyte)
num = (ll_len + 8) / 64 + 1
for i = 1 to num * 16
strByte[i] = fill('0',32)
next
for i = 0 to ll_len - 1
j = calc.bit_rshift(i,2)
strByte[j + 1] = calc.bit_or(strByte[j + 1],calc.to_bin(calc.bit_lshift(lbyte[i + 1],mod(i,4) * 8)))
next
j = calc.bit_rshift(i,2)
strByte[j + 1] = calc.bit_or(strByte[j + 1],calc.to_bin(calc.bit_lshift(128,mod(i,4) * 8)))
strByte[num * 16 - 1] = calc.to_bin(ll_len * 8)
strb = strByte
return
end subroutine
on n_md5.create
call super::create
TriggerEvent( this, "constructor" )
end on
on n_md5.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on