PB实现BASE64加解密

时间:2024-04-14 07:59:42

实例

文本加解密

1.加密文本:PB实现BASE64加解密
PB实现BASE64加解密
得到的密文为:UELlrp7njrBCQVNFNjTliqDop6Plr4Y=

2.解密文本:解密上一步得到的密文
PB实现BASE64加解密
得到的明文为:PB实现BASE64加解密

文件加解密

1.加密文件:
PB实现BASE64加解密
PB实现BASE64加解密
文件大小706KB,加密操在作代码调试状态耗时约3000ms,在程序运行状态耗时约500ms
PB实现BASE64加解密
2.解密文件:解密上一步得到的密文
PB实现BASE64加解密
密文长度为963336,解密操在作代码调试状态耗时约4400ms,在程序运行状态耗时约1600ms
PB实现BASE64加解密
解密得到的文件与原文件完全一致,可以正常打开
PB实现BASE64加解密
PB实现BASE64加解密

源代码

代码拷贝到文本编辑器,另存为 n_base64.sru,导入pbl
发现BUG请留言或私信,以便修正(QQ:768310524 TEL:18649713925)

forward
global type n_base64 from nonvisualobject
end type
end forward

global type n_base64 from nonvisualobject autoinstantiate
end type

type variables
private:
	char code[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
	
end variables
forward prototypes
public function string encode (blob plain)
public function blob decode (string cypher)
end prototypes

public function string encode (blob plain);byte lbyte[]
char lc_cypher[]
long i,j,ll_loop
ulong lul_len,lul_tmp
int li_mod

lbyte = getByteArray(plain)
lul_len = upperbound(lbyte)
li_mod = mod(lul_len,3)
choose case li_mod
	case 1
		lbyte[lul_len + 1] = 0
		lbyte[lul_len + 2] = 0
	case 2
		lbyte[lul_len + 1] = 0
end choose

ll_loop =upperbound(lbyte) / 3

for i = 1 to ll_loop
	lul_tmp = lbyte[3 * i - 2] * 65536 + lbyte[3 * i - 1] * 256 + lbyte[3 * i]
	j += 1
	lc_cypher[j] = code[lul_tmp / 262144 + 1]
	lul_tmp = mod(lul_tmp,262144)
	j += 1
	lc_cypher[j] = code[lul_tmp / 4096 + 1]
	lul_tmp = mod(lul_tmp,4096)
	j += 1
	lc_cypher[j] = code[lul_tmp / 64 + 1]
	lul_tmp = mod(lul_tmp,64)
	j += 1
	lc_cypher[j] = code[lul_tmp + 1]
next

choose case li_mod
	case 1
		lc_cypher[j] = '='
		lc_cypher[j - 1] = '='
	case 2
		lc_cypher[j] = '='
end choose

return lc_cypher

end function

public function blob decode (string cypher);blob lblb_plain
long i,j,ll_loop
char lc_cypher[]
ulong lul_tmp,lul_len
byte lbyte[]
int li_mod

lc_cypher = cypher
lul_len = upperbound(lc_cypher)
if lc_cypher[lul_len] = '=' then
	li_mod = 2
	lc_cypher[lul_len] = 'A'
end if
if lc_cypher[lul_len - 1] = '=' then
	li_mod = 1
	lc_cypher[lul_len - 1] = 'A'
end if

ll_loop = upperbound(lc_cypher) / 4
for i = 1 to ll_loop
	lul_tmp = (pos(code,lc_cypher[4 * i - 3]) - 1) * 262144 +&
				 (pos(code,lc_cypher[4 * i - 2]) - 1) * 4096 +&
				 (pos(code,lc_cypher[4 * i - 1]) - 1) * 64 +&
				 (pos(code,lc_cypher[4 * i]) - 1)
	
	j += 1
	lbyte[j] = lul_tmp / 65536
	lul_tmp = mod(lul_tmp,65536)
	j += 1
	lbyte[j] = lul_tmp / 256
	lul_tmp = mod(lul_tmp,256)
	j += 1
	lbyte[j] = lul_tmp
next

lblb_plain = blob(lbyte)
choose case li_mod
	case 1
		lblb_plain = blobmid(lblb_plain,1,len(lblb_plain) - 2)
	case 2
		lblb_plain = blobmid(lblb_plain,1,len(lblb_plain) - 1)
end choose

return lblb_plain
end function

on n_base64.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_base64.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on