asp base64加解密函数代码

代码如下:

<%

sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS)

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

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
%>

(0)

相关推荐

  • Base64加解密的实现方式实例详解

    Base64加解密的实现方式实例详解 本实现方式基于JDK 1.8 实现: import java.util.Base64; import java.util.Base64.Decoder; import java.util.Base64.Encoder; public class Main { static String src = "hello,sahadev"; public static void main(String[] args) { // 获取加密对象 Encoder

  • 使用 certutil 实现 Hex2Bin 和 Base64 加解密的方法

    应用到: Windows Server 2003, Windows Server 2003 R2, Windows Server 2003 with SP1, Windows Server 2003 with SP2 Certutil Certutil.exe 是作为 Windows Server 2003 家族中证书服务的一部分进行安装的命令行程序. 您还可以通过下载并安装 Windows Server 2003 管理工具包来获取 Certutil.exe (http://go.microso

  • ASP BASE64加解密(亲测可用)

    核心代码: <% ' OPTION EXPLICIT const BASE_64_MAP_INIT = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" dim newline dim Base64EncMap(63) dim Base64DecMap(127) '初始化函数 PUBLIC SUB initCodecs() ' 初始化变量 newline = "<P>&quo

  • asp base64加解密函数代码

    复制代码 代码如下: <% sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" sBASE_64_CHARACTERS = strUnicode2Ansi(sBASE_64_CHARACTERS) Function strUnicodeLen(asContents) '计算unicode字符串的Ansi编码的长度 asContents1="a&q

  • Java通过MySQL的加解密函数实现敏感字段存储

    java通过mysql的加解密函数实现敏感字段存储 1.AES加解密工具类: public class AESUtils { public static String encrypt(String password, String strKey) { try { SecretKey key = generateMySQLAESKey(strKey,"ASCII"); Cipher cipher = Cipher.getInstance("AES"); cipher.

  • 一个批量编码转换及ASP/JS加解密/简繁转换的工具

    详情看这里  http://hi.baidu.com/lael80/blog/item/6633d7fc89f9f282b801a061.html  http://www.gzyd.net/lael/coding.rar  文件编码转换/Screnc加解密/简繁转换 1.0  使用注意:  1.使用前请备份,使用本软件造成的损失自负:  2.进行编码转换前请确认所有文件的源编码一致并选择正确,否则可能出现错误.  3.对ASP代码进行加密或解密或简繁转换前须确认文件的编码为ANSI,否则可能出现

  • aspjpeg组件通用加水印函数代码

    aspjpeg组件实现加水印函数的调用方法: <%printwater "/images/水印图片.gif","/uploadfile/上传图片.jpg") %> 复制代码 代码如下: function printwater(waterpath,upfilepath) Dim Logobox,LogoPath,ogvbox LogoPath = Server.MapPath(waterpath) Set Logobox = Server.CreateOb

  • springboot中request和response的加解密实现代码

    目录 1.新建一个springboot工程,pom依赖如下 2.自定义加密.解密的注解 3.加密算法 4.对请求数据进行解密处理 5.对响应数据进行加密处理 6.加解密的key的配置类,从配置文件中读取 7.测试 在系统开发中,需要对请求和响应分别拦截下来进行解密和加密处理,在springboot中提供了RequestBodyAdviceAdapter和ResponseBodyAdvice,利用这两个工具可以非常方便的对请求和响应进行预处理. 1.新建一个springboot工程,pom依赖如下

  • ASP通用分页样式函数代码

    <% '****************************** '函数:MultiPage(Numbers,Perpage,Curpage,Url_Add) '参数:Numbers,总记录数:Perpage,每页记录数:Curpage,当前页:Url_Add,当前页其它参数如?action=list& '作者:阿里西西 '日期:2007/7/15 '描述:ASP通用分页样式函数 '示例:MultiPage(100,10,2,"?action=list&")

  • 具有时效性的php加密解密函数代码

    复制代码 代码如下: <?phpfunction encode_pass($tex,$key,$type="encode",$expiry=0){    $chrArr=array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',                  'A','B','C','D','E','F','G','

  • ASP FSO文件操作函数代码(复制文件、重命名文件、删除文件、替换字符串)

    FSO文件(File)对象属性 DateCreated 返回该文件夹的创建日期和时间 DateLastAccessed 返回最后一次访问该文件的日期和时间 DateLastModified 返回最后一次修改该文件的日期和时间 Drive 返回该文件所在的驱动器的Drive对象 Name 设定或返回文件的名字 ParentFolder 返回该文件的父文件夹的Folder对象 Path 返回文件的绝对路径,可使用长文件名 ShortName 返回DOS风格的8.3形式的文件名 ShortPath 返

随机推荐