- UID
- 675494
- 积分
- 7
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2013-5-1
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
在网上搜了一段Base64编码解码的VBS代码,VBS能正确运行,于是想把它移植到Lisp中来,移到Lisp中却不成功,请高飞鸟版主给看看,是什么原因?谢谢!下面是网上搜来的VBS代码
- '/*=========================================================================
- ' * Intro Base64编码解码函数,Base64加密解密函数
- ' * FileName Base64Test.vbs
- ' * Author yongfa365
- ' * Version v1.0
- ' * WEB http://www.yongfa365.com
- ' * Email yongfa365[at]qq.com
- ' * FirstWrite http://www.yongfa365.com/Item/Base64Test.vbs.html
- ' * MadeTime 2007-12-31 23:44:36
- ' * LastModify 2007-12-31 23:44:36
- ' *==========================================================================*/
-
-
- sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
- sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS)
-
-
- Function strUnicode2Ansi(asContents)
- '将Unicode编码的字符串,转换成Ansi编码的字符串
- strUnicode2Ansi = ""
- len1 = Len(asContents)
- For i = 1 To len1
- varchar = Mid(asContents, i, 1)
- varasc = Asc(varchar)
- If varasc<0 Then varasc = varasc + 65536
- If varasc>255 Then
- varHex = Hex(varasc)
- varlow = Left(varHex, 2)
- varhigh = Right(varHex, 2)
- strUnicode2Ansi = strUnicode2Ansi & chrb("&H" & varlow ) & chrb("&H" & varhigh )
- Else
- strUnicode2Ansi = strUnicode2Ansi & chrb(varasc)
- End If
- Next
- End Function
-
- Function strAnsi2Unicode(asContents)
- '将Ansi编码的字符串,转换成Unicode编码的字符串
- strAnsi2Unicode = ""
- len1 = lenb(asContents)
- If len1 = 0 Then Exit Function
- For i = 1 To len1
- varchar = midb(asContents, i, 1)
- varasc = ascb(varchar)
- If varasc > 127 Then
- strAnsi2Unicode = strAnsi2Unicode & Chr(ascw(midb(asContents, i + 1, 1) & varchar))
- i = i + 1
- Else
- strAnsi2Unicode = strAnsi2Unicode & Chr(varasc)
- End If
- Next
- End Function
-
- Function Base64encode(asContents)
- '将Ansi编码的字符串进行Base64编码
- 'asContents应当是ANSI编码的字符串(二进制的字符串也可以)
- Dim lnPosition
- Dim lsResult
- Dim Char1
- Dim Char2
- Dim Char3
- Dim Char4
- Dim Byte1
- Dim Byte2
- Dim Byte3
- Dim SaveBits1
- Dim SaveBits2
- Dim lsGroupBinary
- Dim lsGroup64
- Dim m4, len1, len2
-
- len1 = Lenb(asContents)
- If len1<1 Then
- Base64encode = ""
- Exit Function
- End If
-
- m3 = Len1 Mod 3
- If M3 > 0 Then asContents = asContents & String(3 - M3, chrb(0))
- '补足位数是为了便于计算
-
- If m3 > 0 Then
- len1 = len1 + (3 - m3)
- len2 = len1 -3
- Else
- len2 = len1
- End If
-
- lsResult = ""
-
- For lnPosition = 1 To len2 Step 3
- lsGroup64 = ""
- lsGroupBinary = Midb(asContents, lnPosition, 3)
-
- Byte1 = Ascb(Midb(lsGroupBinary, 1, 1))
- SaveBits1 = Byte1 And 3
- Byte2 = Ascb(Midb(lsGroupBinary, 2, 1))
- SaveBits2 = Byte2 And 15
- Byte3 = Ascb(Midb(lsGroupBinary, 3, 1))
-
- Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) / 4) + 1, 1)
- Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) / 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
- Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) / 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
- Char4 = Midb(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)
- lsGroup64 = Char1 & Char2 & Char3 & Char4
-
- lsResult = lsResult & lsGroup64
- Next
-
- '处理最后剩余的几个字符
- If M3 > 0 Then
- lsGroup64 = ""
- lsGroupBinary = Midb(asContents, len2 + 1, 3)
-
- Byte1 = Ascb(Midb(lsGroupBinary, 1, 1))
- SaveBits1 = Byte1 And 3
- Byte2 = Ascb(Midb(lsGroupBinary, 2, 1))
- SaveBits2 = Byte2 And 15
- Byte3 = Ascb(Midb(lsGroupBinary, 3, 1))
-
- Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) / 4) + 1, 1)
- Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) / 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
- Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) / 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
-
- If M3 = 1 Then
- lsGroup64 = Char1 & Char2 & ChrB(61) & ChrB(61) '用=号补足位数
- Else
- lsGroup64 = Char1 & Char2 & Char3 & ChrB(61) '用=号补足位数
- End If
-
- lsResult = lsResult & lsGroup64
- End If
-
- Base64encode = lsResult
-
- End Function
-
-
- Function Base64decode(asContents)
- '将Base64编码字符串转换成Ansi编码的字符串
- 'asContents应当也是ANSI编码的字符串(二进制的字符串也可以)
- Dim lsResult
- Dim lnPosition
- Dim lsGroup64, lsGroupBinary
- Dim Char1, Char2, Char3, Char4
- Dim Byte1, Byte2, Byte3
- Dim M4, len1, len2
-
- len1 = Lenb(asContents)
- M4 = len1 Mod 4
-
- If len1 < 1 Or M4 > 0 Then
- '字符串长度应当是4的倍数
- Base64decode = ""
- Exit Function
- End If
-
- '判断最后一位是不是 = 号
- '判断倒数第二位是不是 = 号
- '这里m4表示最后剩余的需要单独处理的字符个数
- If midb(asContents, len1, 1) = chrb(61) Then m4 = 3
- If midb(asContents, len1 -1, 1) = chrb(61) Then m4 = 2
-
- If m4 = 0 Then
- len2 = len1
- Else
- len2 = len1 -4
- End If
-
- For lnPosition = 1 To Len2 Step 4
- lsGroupBinary = ""
- lsGroup64 = Midb(asContents, lnPosition, 4)
- Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1
- Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1
- Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1
- Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1
- Byte1 = Chrb(((Char2 And 48) / 16) Or (Char1 * 4) And &HFF)
- Byte2 = lsGroupBinary & Chrb(((Char3 And 60) / 4) Or (Char2 * 16) And &HFF)
- Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
- lsGroupBinary = Byte1 & Byte2 & Byte3
-
- lsResult = lsResult & lsGroupBinary
- Next
-
- '处理最后剩余的几个字符
- If M4 > 0 Then
- lsGroupBinary = ""
- lsGroup64 = Midb(asContents, len2 + 1, m4) & chrB(65) 'chr(65)=A,转换成值为0
- If M4 = 2 Then '补足4位,是为了便于计算
- lsGroup64 = lsGroup64 & chrB(65)
- End If
- Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1
- Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1
- Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1
- Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1
- Byte1 = Chrb(((Char2 And 48) / 16) Or (Char1 * 4) And &HFF)
- Byte2 = lsGroupBinary & Chrb(((Char3 And 60) / 4) Or (Char2 * 16) And &HFF)
- Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63))
-
- If M4 = 2 Then
- lsGroupBinary = Byte1
- ElseIf M4 = 3 Then
- lsGroupBinary = Byte1 & Byte2
- End If
-
- lsResult = lsResult & lsGroupBinary
- End If
-
- Base64decode = lsResult
-
- End Function
-
-
-
- '因为Base64的编码与解码过程都是对Ansi编码进行操作的,所以在编码或解码前得先把我们平时看到的Unicode编码转换为Ansi编码
- 'Base64编码过程:Unicode-->Ansi-->Base64编码-->Unicode
- MsgBox strUnicode2Ansi("精彩blog http://www.yongfa365.com/"), , "Unicode转成Ansi,显示乱码为正常"
- MsgBox Base64encode(strUnicode2Ansi("精彩blog http://www.yongfa365.com/")), , "对Ansi进行Base64编码,显示乱码为正常"
- MsgBox strAnsi2Unicode(Base64encode(strUnicode2Ansi("精彩blog http://www.yongfa365.com/"))), , "编码后再由Ansi转为Unicode,显示正常"
-
- 'Base64解码过程:Unicode-->Ansi-->Base64解码-->Unicode
- MsgBox strAnsi2Unicode(Base64decode(strUnicode2Ansi("vquyymJsb2cgaHR0cDovL3d3dy55b25nZmEzNjUuY29tLw=="))), , "Base64解码过程"
-
- 'Base64编码解码过程:Unicode-->Ansi-->Base64编码-->Base64解码-->Unicode
- MsgBox strAnsi2Unicode(Base64decode(Base64encode(strUnicode2Ansi("精彩blog http://www.yongfa365.com/")))), , "Base64编码解码过程"
- ;;参数 buffer = 字串
- ;; encode = t 编码 = nil 解码
- (defun Base64code (buffer encode / wsObj FuncStr)
- (if (setq wsObj (vlax-create-object "ScriptControl"))
- (progn
- (vlax-put wsObj "language" "VBS")
- (setq Funcstr
- (list
- "'/*========================================================================= "
- "' * Intro Base64编码解码函数,Base64加密解密函数 "
- "' * FileName Base64Test.vbs "
- "' * Author yongfa365 "
- "' * Version v1.0 "
- "' * WEB http://www.yongfa365.com "
- "' * Email yongfa365[at]qq.com "
- "' * FirstWrite http://www.yongfa365.com/Item/Base64Test.vbs.html "
- "' * MadeTime 2007-12-31 23:44:36 "
- "' * LastModify 2007-12-31 23:44:36 "
- "' *==========================================================================*/ "
- ""
- "sBASE_64_CHARACTERS = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\""
- "sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS) "
- ""
- "Function strUnicode2Ansi(asContents) "
- " '将Unicode编码的字符串,转换成Ansi编码的字符串 "
- " strUnicode2Ansi = \"\""
- " len1 = Len(asContents) "
- " For i = 1 To len1 "
- " varchar = Mid(asContents, i, 1) "
- " varasc = Asc(varchar) "
- " If varasc<0 Then varasc = varasc + 65536 "
- " If varasc>255 Then "
- " varHex = Hex(varasc) "
- " varlow = Left(varHex, 2) "
- " varhigh = Right(varHex, 2) "
- " strUnicode2Ansi = strUnicode2Ansi & chrb(\"&H\" & varlow ) & chrb(\"&H\" & varhigh ) "
- " Else "
- " strUnicode2Ansi = strUnicode2Ansi & chrb(varasc) "
- " End If "
- " Next "
- "End Function "
- ""
- "Function strAnsi2Unicode(asContents) "
- " '将Ansi编码的字符串,转换成Unicode编码的字符串 "
- " strAnsi2Unicode = \"\""
- " len1 = lenb(asContents) "
- " If len1 = 0 Then Exit Function "
- " For i = 1 To len1 "
- " varchar = midb(asContents, i, 1) "
- " varasc = ascb(varchar) "
- " If varasc > 127 Then "
- " strAnsi2Unicode = strAnsi2Unicode & Chr(ascw(midb(asContents, i + 1, 1) & varchar)) "
- " i = i + 1 "
- " Else "
- " strAnsi2Unicode = strAnsi2Unicode & Chr(varasc) "
- " End If "
- " Next "
- "End Function "
- ""
- "Function Base64encode(asContents) "
- " '将Ansi编码的字符串进行Base64编码 "
- " 'asContents应当是ANSI编码的字符串(二进制的字符串也可以) "
- " Dim lnPosition "
- " Dim lsResult "
- " Dim Char1 "
- " Dim Char2 "
- " Dim Char3 "
- " Dim Char4 "
- " Dim Byte1 "
- " Dim Byte2 "
- " Dim Byte3 "
- " Dim SaveBits1 "
- " Dim SaveBits2 "
- " Dim lsGroupBinary "
- " Dim lsGroup64 "
- " Dim m4, len1, len2 "
- " "
- " len1 = Lenb(asContents) "
- " If len1<1 Then "
- " Base64encode = \"\""
- " Exit Function "
- " End If "
- " "
- " m3 = Len1 Mod 3 "
- " If M3 > 0 Then asContents = asContents & String(3 - M3, chrb(0)) "
- " '补足位数是为了便于计算 "
- " "
- " If m3 > 0 Then "
- " len1 = len1 + (3 - m3) "
- " len2 = len1 -3 "
- " Else "
- " len2 = len1 "
- " End If "
- " "
- " lsResult = \"\""
- " "
- " For lnPosition = 1 To len2 Step 3 "
- " lsGroup64 = \"\""
- " lsGroupBinary = Midb(asContents, lnPosition, 3) "
- " "
- " Byte1 = Ascb(Midb(lsGroupBinary, 1, 1)) "
- " SaveBits1 = Byte1 And 3 "
- " Byte2 = Ascb(Midb(lsGroupBinary, 2, 1)) "
- " SaveBits2 = Byte2 And 15 "
- " Byte3 = Ascb(Midb(lsGroupBinary, 3, 1)) "
- " "
- " Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) / 4) + 1, 1) "
- " Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) / 16) Or (SaveBits1 * 16) And &HFF) + 1, 1) "
- " Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) / 64) Or (SaveBits2 * 4) And &HFF) + 1, 1) "
- " Char4 = Midb(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1) "
- " lsGroup64 = Char1 & Char2 & Char3 & Char4 "
- " "
- " lsResult = lsResult & lsGroup64 "
- " Next "
- " "
- " '处理最后剩余的几个字符 "
- " If M3 > 0 Then "
- " lsGroup64 = \"\""
- " lsGroupBinary = Midb(asContents, len2 + 1, 3) "
- " "
- " Byte1 = Ascb(Midb(lsGroupBinary, 1, 1)) "
- " SaveBits1 = Byte1 And 3 "
- " Byte2 = Ascb(Midb(lsGroupBinary, 2, 1)) "
- " SaveBits2 = Byte2 And 15 "
- " Byte3 = Ascb(Midb(lsGroupBinary, 3, 1)) "
- " "
- " Char1 = Midb(sBASE_64_CHARACTERS, ((Byte1 And 252) / 4) + 1, 1) "
- " Char2 = Midb(sBASE_64_CHARACTERS, (((Byte2 And 240) / 16) Or (SaveBits1 * 16) And &HFF) + 1, 1) "
- " Char3 = Midb(sBASE_64_CHARACTERS, (((Byte3 And 192) / 64) Or (SaveBits2 * 4) And &HFF) + 1, 1) "
- " "
- " If M3 = 1 Then "
- " lsGroup64 = Char1 & Char2 & ChrB(61) & ChrB(61) '用=号补足位数 "
- " Else "
- " lsGroup64 = Char1 & Char2 & Char3 & ChrB(61) '用=号补足位数 "
- " End If "
- " "
- " lsResult = lsResult & lsGroup64 "
- " End If "
- " "
- " Base64encode = lsResult "
- " "
- "End Function "
- ""
- ""
- "Function Base64decode(asContents) "
- " '将Base64编码字符串转换成Ansi编码的字符串 "
- " 'asContents应当也是ANSI编码的字符串(二进制的字符串也可以) "
- " Dim lsResult "
- " Dim lnPosition "
- " Dim lsGroup64, lsGroupBinary "
- " Dim Char1, Char2, Char3, Char4 "
- " Dim Byte1, Byte2, Byte3 "
- " Dim M4, len1, len2 "
- " "
- " len1 = Lenb(asContents) "
- " M4 = len1 Mod 4 "
- " "
- " If len1 < 1 Or M4 > 0 Then "
- " '字符串长度应当是4的倍数 "
- " Base64decode = \"\""
- " Exit Function "
- " End If "
- " "
- " '判断最后一位是不是 = 号 "
- " '判断倒数第二位是不是 = 号 "
- " '这里m4表示最后剩余的需要单独处理的字符个数 "
- " If midb(asContents, len1, 1) = chrb(61) Then m4 = 3 "
- " If midb(asContents, len1 -1, 1) = chrb(61) Then m4 = 2 "
- " "
- " If m4 = 0 Then "
- " len2 = len1 "
- " Else "
- " len2 = len1 -4 "
- " End If "
- " "
- " For lnPosition = 1 To Len2 Step 4 "
- " lsGroupBinary = \"\""
- " lsGroup64 = Midb(asContents, lnPosition, 4) "
- " Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1 "
- " Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1 "
- " Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1 "
- " Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1 "
- " Byte1 = Chrb(((Char2 And 48) / 16) Or (Char1 * 4) And &HFF) "
- " Byte2 = lsGroupBinary & Chrb(((Char3 And 60) / 4) Or (Char2 * 16) And &HFF) "
- " Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63)) "
- " lsGroupBinary = Byte1 & Byte2 & Byte3 "
- " "
- " lsResult = lsResult & lsGroupBinary "
- " Next "
- " "
- " '处理最后剩余的几个字符 "
- " If M4 > 0 Then "
- " lsGroupBinary = \"\""
- " lsGroup64 = Midb(asContents, len2 + 1, m4) & chrB(65) 'chr(65)=A,转换成值为0 "
- " If M4 = 2 Then '补足4位,是为了便于计算 "
- " lsGroup64 = lsGroup64 & chrB(65) "
- " End If "
- " Char1 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 1, 1)) - 1 "
- " Char2 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 2, 1)) - 1 "
- " Char3 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 3, 1)) - 1 "
- " Char4 = InStrb(sBASE_64_CHARACTERS, Midb(lsGroup64, 4, 1)) - 1 "
- " Byte1 = Chrb(((Char2 And 48) / 16) Or (Char1 * 4) And &HFF) "
- " Byte2 = lsGroupBinary & Chrb(((Char3 And 60) / 4) Or (Char2 * 16) And &HFF) "
- " Byte3 = Chrb((((Char3 And 3) * 64) And &HFF) Or (Char4 And 63)) "
- " "
- " If M4 = 2 Then "
- " lsGroupBinary = Byte1 "
- " ElseIf M4 = 3 Then "
- " lsGroupBinary = Byte1 & Byte2 "
- " End If "
- " "
- " lsResult = lsResult & lsGroupBinary "
- " End If "
- " "
- " Base64decode = lsResult "
- " "
- "End Function "
- )
- )
- (vlax-invoke wsObj 'ExecuteStatement (apply 'strcat (mapcar '(lambda (x) (strcat x "\n")) FuncStr)))
- (if encode
- (setq buffer
- (vlax-invoke wsObj 'run "strAnsi2Unicode"
- (vlax-invoke wsObj 'run "Base64encode"
- (vlax-invoke wsObj 'run "strUnicode2Ansi" buffer)
- )
- )
- ) ;_ 编码
- (setq buffer
- (vlax-invoke wsObj 'run "strAnsi2Unicode"
- (vlax-invoke wsObj 'run "Base64decode"
- (vlax-invoke wsObj 'run "strUnicode2Ansi" buffer)
- )
- )
- ) ;_ 解码
- )
-
- (vlax-release-object wsObj)
- buffer
- )
- )
- )
- ;; 编码测试 ,结果不正确
- (Base64code "精彩blog http://www.yongfa365.com/" t)
- ;;解码测试,提示 错误: Microsoft VBScript 运行时错误: 无效的过程调用或参数: 'ascw'
- (Base64code "vquyymJsb2cgaHR0cDovL3d3dy55b25nZmEzNjUuY29tLw==" nil)
|
|