求base64编码解码的pb程序

freemanxm84 2007-11-06 04:26:47
如题。
我有网上找了个编码的程序,但是没有解码的程序。大家帮帮忙哈,我刚学PB的

forward
global type nvo_base64 from nonvisualobject
end type
end forward

global type nvo_base64 from nonvisualobject
end type
global nvo_base64 nvo_base64

forward prototypes
public function string of_oct (long vl_dec)
public function integer of_myasc (character onechar)
public function string of_b64e (string vs_in)
public function string of_nchar (character vchar, long vl_count)
public function integer of_oct2dec (string vs_oct)
end prototypes

public function string of_oct (long vl_dec);
long i,li_shang
char li_yu[]
li_shang = vl_dec/8
li_yu[1] = string(mod(vl_dec,8))
i = 2

do while li_shang > 0
li_yu[i] = string(mod(li_shang,8))
i ++
li_shang = li_shang/8
loop

return Reverse(li_yu)
end function

public function integer of_myasc (character onechar);
IF OneChar = "" THEN
RETURN 0
ELSE
RETURN Asc(OneChar)
END IF


end function

public function string of_b64e (string vs_in);

Constant string Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
string ls_out
int i

For i = 1 To Len(vs_in) Step 3
Long ll_Group
string ls_Group,ls_pOut

ll_Group = 65536 * Asc(Mid(vs_in, i, 1)) + 256 * of_MyAsc(Mid(vs_in, i + 1, 1)) + of_MyAsc(Mid(vs_in, i + 2, 1))
ls_Group = of_Oct(ll_Group)
ls_Group = of_nChar('0',8 - Len(ls_Group)) + ls_Group
ls_pOut = Mid(Base64, of_oct2dec(Mid(ls_Group, 1, 2)) + 1, 1) + Mid(Base64, of_oct2dec(Mid(ls_Group, 3, 2)) + 1, 1) + Mid(Base64, of_oct2dec(Mid(ls_Group, 5, 2)) + 1, 1) + Mid(Base64, of_oct2dec(Mid(ls_Group, 7, 2)) + 1, 1)
ls_Out = ls_Out + ls_pOut
If mod( (i + 2),57) = 0 Then ls_Out = ls_Out + "~r~n"
Next

choose Case mod( Len(vs_in), 3)
Case 1
ls_Out = Left(ls_Out, Len(ls_Out) - 2) + "=="
Case 2
ls_Out = Left(ls_Out, Len(ls_Out) - 1) + "="
End choose
return ls_Out


end function

public function string of_nchar (character vchar, long vl_count);
Char ls_out[]
Long i
FOR i = 1 TO vl_count
ls_out[i] = vchar
NEXT
RETURN ls_out


end function

public function integer of_oct2dec (string vs_oct);
string ls_oct
integer i,length
long result = 0

length = len(vs_oct)
ls_oct = Upper(vs_oct)
FOR i = 1 to length
result += &
(Pos ('1234567', mid(ls_oct, i, 1)) * &
( 8 ^ ( length - i ) ))
NEXT

RETURN result

end function

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

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

...全文
856 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
leio 2008-06-27
  • 打赏
  • 举报
回复
http://download.csdn.net/user/leio/

今天有个朋友说要用BASE64对图片进行加密存到数据库里,还要能取出并解密,

因此我给他写了这个BASE64加解密用户对象。

此用户对象可以对任意文件或文本进行加解密操作。

不过,PB实在不是干这种事的料,效率惨不忍睹。

如果只是对少量文本或很小的文件进行加解密还是能用用的。

大文本或大文件还是用别的语言写个DLL给PB调用吧。
meiZiNick 2008-05-01
  • 打赏
  • 举报
回复
都是很好的建议! 值得学习
freemanxm84 2007-11-06
  • 打赏
  • 举报
回复
哦 我有个C写的程序,但是我不晓得怎么把他改成可以供PB调用的DLL
我把代码贴出来,你帮下我哈,谢谢了,很急~!

/* Note:Your choice is C IDE */
#include <stdio.h>
#include <string.h>
#include <malloc.h>

char* ch64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

unsigned char *encode(unsigned char *src,int srclen)
{
int n,buflen,i,j;
int pading=0;
unsigned char *buf;
static unsigned char *dst;

buf=src;
buflen=n=srclen;
if(n%3!=0) /* pad with ''''='''' by using a temp buffer */
{
pading=1;
buflen=n+3-n%3;
buf=malloc(buflen+1);
memset(buf,0,buflen+1);
memcpy(buf,src,n);
for(i=0;i<3-n%3;i++)
buf[n+i]='\0';
}
dst=malloc(buflen*4/3+1);
memset(dst,0,buflen*4/3+1);
for(i=0,j=0;i<buflen;i+=3,j+=4)
{
dst[j]=(buf[i]&0xFC)>>2;
dst[j+1]=((buf[i]&0x03)<<4) + ((buf[i+1]&0xF0)>>4);
dst[j+2]=((buf[i+1]&0x0F)<<2) + ((buf[i+2]&0xC0)>>6);
dst[j+3]=buf[i+2]&0x3F;
}
for(i=0;i<buflen*4/3;i++) /* map 6 bit value to base64 ASCII character */
dst[i]=ch64[dst[i]];
if(pading)
free(buf);
return dst;
}

unsigned char *decode(unsigned char *src)
{
int n,i,j;
unsigned char *p;
static unsigned char *dst;

n=strlen(src);
for(i=0;i<n;i++) /* map base64 ASCII character to 6 bit value */
{
p=strchr(ch64,src[i]);
if(!p)
break;
src[i]=p-ch64;
}
dst=malloc(n*3/4+1);
memset(dst,0,n*3/4+1);
for(i=0,j=0;i<n;i+=4,j+=3)
{
dst[j]=(src[i]<<2) + ((src[i+1]&0x30)>>4);
dst[j+1]=((src[i+1]&0x0F)<<4) + ((src[i+2]&0x3C)>>2);
dst[j+2]=((src[i+2]&0x03)<<6) + src[i+3];
}
return dst;
}

void main()
{
char *src="123456789111";
//char src[]={'1','2','3',0,'a','b','*',0,'A','B','$'};
unsigned char *dst1;
unsigned char *dst2;
unsigned int i;

dst1=encode(src,12); /* the second parameter must accord with the first one */
printf("%s\n",dst1);

dst2=decode(dst1);
for(i=0;i<_msize(dst2);i++)
printf("%c",dst2[i]);

free(dst1);
free(dst2);
}

freemanxm84 2007-11-06
  • 打赏
  • 举报
回复
哦 这样啊,谢谢哈
AFIC 2007-11-06
  • 打赏
  • 举报
回复
base64全是按位操作,正是pb的软肋,
你要不找dll要不看连接的数据库有没有对应方法比较好。
freemanxm84 2007-11-06
  • 打赏
  • 举报
回复
急啊,那位高人解救下啊

1,075

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 相关问题讨论
社区管理员
  • 基础类社区
  • WorldMobile
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧