麻烦各位高手指点下,字符串加密后,解密函数解出来为乱码。
加密函数:
String ls_case,ls_asc,ls_one,ls_repassword,ls_asc2,ls_char
Integer i,li_word
integer li_e, li_d, li_n,li_len,li_char
ls_case = trim(as_password)
IF len(ls_case) <= 0 THEN RETURN('')
FOR i = 1 TO Len(ls_case)
ls_one = String(Asc(Mid(ls_case,i,1)))
ls_asc = ls_asc + Right('000' + ls_one,3)
NEXT
//加密
li_e = 3 //设置指数E,加密密钥
li_d = 7 //解密密钥
li_n = 33 //两个素数得乘积
string ls_str,ls_group
ls_str = ls_asc
ulong lul_temp
lul_temp = 0
ulong lul_x, lul_y // lul_x 加密明文,lul_y 加密密文
do until ls_str = ""
ls_group = left(ls_str,2)
lul_temp = Integer(left(ls_str, 2))
if lul_temp >= li_n then
lul_temp = Integer(left(ls_str, 1))
ls_str = right(ls_str, len(ls_str)-1)
else
ls_str = right(ls_str, len(ls_str)-2)
end if
lul_y = 1
for I = 1 to li_e
lul_y = lul_y * lul_temp
next
lul_y = mod(lul_y, li_n)
IF left(ls_group,1) = '0' AND Len(ls_group) = 2 THEN
ls_repassword = trim(ls_repassword) + '00' + Right('00' + string(lul_y),2)
ELSE
ls_repassword = trim(ls_repassword) + Right('00' + string(lul_y),2)
END IF
loop
li_len = len(ls_repassword)
//将字符窜转换城整形作为ASC,再将ASC转换为字符。
ls_asc2 = ''
FOR i = 1 TO li_len
ls_char = char(integer(mid(ls_repassword,i,1)))
ls_asc2 = ls_asc2 + ls_char
NEXT
RETURN ls_asc2
解密函数:
String ls_case,ls_asc,ls_one
String ls_str,ls_str1,ls_char
ulong lul_temp
integer li_e, li_d, li_n,i,li_len
ulong lul_x0, lul_x1 ,lul_x,lul_y
ls_asc = ai_pass
li_len = len(ls_asc)
ls_char = ''
FOR i = 1 TO li_len
ls_char =ls_char + string(asc(mid(ls_asc,i,1)))
NEXT
ls_asc = ls_char
IF len(trim(ls_asc)) <= 0 THEN RETURN('')
li_e = 3
li_d = 7
li_n = 33
ls_str = Trim(ls_asc)
do until ls_str = ""
lul_temp = Integer(left(ls_str, 2))
if lul_temp >= li_n then
lul_temp = Integer(left(ls_str, 1))
ls_str = right(ls_str, len(ls_str)-1)
else
ls_str = right(ls_str, len(ls_str)-2)
end if
lul_x = 1
FOR i = 1 TO 7
lul_x = lul_x * lul_temp
IF i = 3 THEN
lul_x0 = lul_x
lul_x = 1
END IF
IF i = 6 THEN
lul_x1 = lul_x
lul_x = 1
END IF
NEXT
lul_y = mod((mod(lul_x0,33) * mod(lul_x1,33) * mod(lul_x,33)), 33)
ls_str1 = trim(ls_str1) + string(lul_y)
Loop
FOR i = 1 TO Len(ls_str1) step 3
ls_one = char(Integer(Mid(ls_str1,i,3)))
ls_case = ls_case + ls_one
NEXT
RETURN ls_case
------解决思路----------------------
看了一下问题在加密的算法里面这段:
FOR i = 1 TO li_len
ls_char = char(integer(mid(ls_repassword,i,1)))
ls_asc2 = ls_asc2 + ls_char
NEXT
原因你可以参见下面这段代码:
string ls_length,ls_length2
ls_length = 'Length'
ls_length2 = ls_length+char(0)+char(0)+char(0)+char(0)
messagebox(ls_length,'原始字符串长度:'+string(len(ls_length))+'~r修改后字符串长度:'+string(len(ls_length2)))
运行结果:
------解决思路----------------------
把上面那段改成:
FOR i = 1 TO li_len
if(mid(ls_repassword,i,1)='0')then
ls_char = char(20)
else
ls_char = char(integer(mid(ls_repassword,i,1)))
end if
ls_asc2 = ls_asc2 + ls_char
NEXT
解密中这段:
FOR i = 1 TO li_len
ls_char =ls_char + string(asc(mid(ls_asc,i,1)))
NEXT
改成:
FOR i = 1 TO li_len
if(mid(ls_asc,i,1)=char(20))then
ls_char += '0'
else
ls_char =ls_char + string(asc(mid(ls_asc,i,1)))
end if
NEXT
再运行:
string pass,enpass,depass
pass = '12asRE5#%'
enpass = uf_test_en(pass)
depass = uf_test_de(enpass)
messagebox(pass,'加密数据:'+pass+'~r加密后='+enpass+'~r解密后='+depass)
得到结果: