BASE64编码
标准编码
将任意字节流编码为字符串,每3字节转换为4个字符,用于编码的字符集合为如下64个字符:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
URL编码
标准编码的字符集合中,把+换成-,把/换成_,即编码的字符集合为:
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_
MIME编码
标准编码每76个字符换行一次
编码与解码测试
测试文本
Base64编码可用于在HTTP环境下传递较长的标识信息。例如,在Java Persistence系统Hibernate中,就采用了Base64来将一个较长的一个标识符(一般为128-bit的UUID)编码为一个字符串,用作HTTP表单和HTTP GET URL中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式。此时,采用Base64编码不仅比较简短,同时也具有不可读性,即所编码的数据不会被人用肉眼所直接看到。
URL模式
编码
n_base64 ln_base64
blob lblb_plain
lblb_plain = blob(mle_1.text,encodingUTF8!)
mle_2.text = ln_base64.urlencode(lblb_plain)
return 0
解码
n_base64 ln_base64
blob lblb_plain
lblb_plain = ln_base64.urldecode(mle_1.text)
mle_2.text = string(lblb_plain,encodingUTF8!)
return 0
MIME模式
编码
n_base64 ln_base64
blob lblb_plain
lblb_plain = blob(mle_1.text,encodingUTF8!)
mle_2.text = ln_base64.mimeencode(lblb_plain)
return 0
解码
n_base64 ln_base64
blob lblb_plain
lblb_plain = ln_base64.mimedecode(mle_1.text)
mle_2.text = string(lblb_plain,encodingUTF8!)
return 0
标准模式
编码
n_base64 ln_base64
blob lblb_plain
lblb_plain = blob(mle_1.text,encodingUTF8!)
mle_2.text = ln_base64.encode(lblb_plain)
return 0
解码
n_base64 ln_base64
blob lblb_plain
lblb_plain = ln_base64.decode(mle_1.text)
mle_2.text = string(lblb_plain,encodingUTF8!)
return 0
源代码
发现BUG请留言或私信,以便修正(QQ:768310524 TEL:18649713925)
代码拷贝到文本编辑器,另存为 n_base64.sru
$PBExportHeader$n_base64.sru
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[]
char standardCode[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
char urlCode[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
end variables
forward prototypes
public function string encode (blob plain)
public function blob decode (string cypher)
public function string urlencode (blob plain)
public function blob urldecode (string cypher)
public function string mimeencode (blob plain)
public function blob mimedecode (string cypher)
public function string _enc (blob plain)
public function blob _dec (string cypher)
end prototypes
public function string encode (blob plain);
code = standardCode
return _enc(plain)
end function
public function blob decode (string cypher);
code = standardCode
return _dec(cypher)
end function
public function string urlencode (blob plain);
code = urlCode
return _enc(plain)
end function
public function blob urldecode (string cypher);
code = urlCode
return _dec(cypher)
end function
public function string mimeencode (blob plain);long i,ll_loop
string ls_cypher
code = standardCode
ll_loop = ceiling(len(plain) / 57)
for i = 1 to ll_loop
ls_cypher += _enc(blobmid(plain,57 * i - 56,57)) + '~r~n'
next
return left(ls_cypher,len(ls_cypher) - 2)
end function
public function blob mimedecode (string cypher);char lch_cypher[],lch[]
long i,j,ll_len
code = standardCode
lch = cypher
ll_len = len(cypher)
for i = 1 to ll_len
if lch[i] <> '~r' and lch[i] <> '~n' then
j++
lch_cypher[j] = lch[i]
end if
next
return _dec(lch_cypher)
end function
public function string _enc (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 _dec (string cypher);blob lblb_plain
long i,j,ll_loop
char lc_cypher[]
ulong lul_tmp,lul_len
byte lbyte[]
int li_mod
string ls_codes
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
ls_codes = code
ll_loop = upperbound(lc_cypher) / 4
for i = 1 to ll_loop
lul_tmp = (pos(ls_codes,lc_cypher[4 * i - 3]) - 1) * 262144 +&
(pos(ls_codes,lc_cypher[4 * i - 2]) - 1) * 4096 +&
(pos(ls_codes,lc_cypher[4 * i - 1]) - 1) * 64 +&
(pos(ls_codes,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