ASP用于BASE64的编解码

我以前收藏的一段代码,ASP的是用于BASE64的编解码的:
<%
sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS)

'计算unicode字符串的Ansi编码的长度
Function strUnicodeLen(asContents)
 '计算unicode字符串的Ansi编码的长度
 asContents1="a"&asContents
 len1=len(asContents1)
 k=0
 for i=1 to len1
  asc1=asc(mid(asContents1,i,1))
  if asc1<0 then asc1=65536+asc1
  if asc1>255 then
   k=k+2
  else
   k=k+1
  end if
 next
 strUnicodeLen=k-1
End Function

'将Unicode编码的字符串,转换成Ansi编码的字符串
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

'将Ansi编码的字符串,转换成Unicode编码的字符串
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

'将Ansi编码的字符串进行Base64编码
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

'将Base64编码字符串转换成Ansi编码的字符串
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


'---------------------------------------------------------------------------------
response.write strUnicode2Ansi("abc我们d我们") & "<hr>"
response.write strAnsi2Unicode(Base64encode(strUnicode2Ansi("abc我们d我们"))) & "<hr>"

response.write strAnsi2Unicode(Base64decode(Base64encode(strUnicode2Ansi("abc我们d我们")))) & "<hr>"

sdecode="Sm1haWy1xNb30qqyzsr9wdCx7Q0K1/fV36O6dG9ueTEzMiAgMjAwMC0wNS0yNNXixqq45bz+sbvU"
sdecode=sdecode & "xLbBPT0xNTK0zg0KIA0KDQqhoaGh1/fV3yBb0ru/y10gDQqhoaGhIA0KoaGhoQ0KoaGhocu1w/ej"
sdecode=sdecode & "urj8tuC1xNDFz6LH67LOv7xKbWFpbMu1w/ejrM7Sz+vV4tLRvq3X47m708O1xMHLoaMNCqGhoaEN"

response.write replace(strAnsi2Unicode(Base64decode(strUnicode2Ansi(sdecode))),chr(13)&chr(10),"<br>")

response.end


%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP代码中的Base64加密和解密工具可以通过以下步骤实现。 加密: 1. 获取要加密的字符串。 2. 将字符串换为字节数组。 3. 使用ASP代码中的Base64编码函数对字节数组进行编码。 4. 将编码后的字符串作为加密结果返回。 解密: 1. 获取已经加密的Base64字符串。 2. 使用ASP代码中的Base64解码函数将字符串解码为字节数组。 3. 将解码后的字节数组换为字符串。 4. 将解密后的字符串作为解密结果返回。 示例代码如下: ``` ' 加密函数 Function Base64Encode(str) Dim bytes, enc ' 将字符串换为字节数组 bytes = StrToByteArray(str) ' 使用Base64编码函数进行编码 Set enc = Server.CreateObject("System.Text.Encoding") Base64Encode = enc.EncodeBytes_64(bytes) End Function ' 解密函数 Function Base64Decode(str) Dim bytes, enc ' 使用Base64解码函数进行解码 Set enc = Server.CreateObject("System.Text.Encoding") bytes = enc.DecodeBytes_64(str) ' 将字节数组换为字符串 Base64Decode = ByteArrayToStr(bytes) End Function ' 将字符串换为字节数组 Function StrToByteArray(str) Dim i, bytes ReDim bytes(Len(str) - 1) For i = 1 To Len(str) bytes(i - 1) = Asc(Mid(str, i, 1)) Next StrToByteArray = bytes End Function ' 将字节数组换为字符串 Function ByteArrayToStr(bytes) Dim i, str For i = LBound(bytes) To UBound(bytes) str = str & Chr(bytes(i)) Next ByteArrayToStr = str End Function ``` 使用时,可以调用`Base64Encode`函数进行加密,以及调用`Base64Decode`函数进行解密。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值