base64로 변환 (인코딩/디코딩) 하는 함수 > 기술자료 | 해피정닷컴

base64로 변환 (인코딩/디코딩) 하는 함수 > 기술자료

본문 바로가기

사이트 내 전체검색

base64로 변환 (인코딩/디코딩) 하는 함수 > 기술자료

ClassicASP base64로 변환 (인코딩/디코딩) 하는 함수

페이지 정보


본문

Option Explicit

Dim sBASE_64_CHARACTERS, sBASE_64_CHARACTERSansi
sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
sBASE_64_CHARACTERSansi = strUnicode2Ansi(sBASE_64_CHARACTERS)


Function strUnicodeLen(asContents)
Dim asContents1 : asContents1 ="a" & asContents
Dim Len1 : Len1=Len(asContents1)
Dim K : K=0
Dim I, Asc1

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)
Dim Len1 : Len1 = Len(asContents)
Dim I, VarCHAR, VarASC, VarHEX, VarLOW, VarHIGH

strUnicode2Ansi = ""

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)
Dim Len1 : Len1 = LenB(asContents)
Dim VarCHAR, VarASC, I

strAnsi2Unicode = ""

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)
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 M3, 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_CHARACTERSansi, ((Byte1 And 252) \ 4) + 1, 1)
Char2 = MidB(sBASE_64_CHARACTERSansi, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = MidB(sBASE_64_CHARACTERSansi, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
Char4 = MidB(sBASE_64_CHARACTERSansi, (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_CHARACTERSansi, ((Byte1 And 252) \ 4) + 1, 1)
Char2 = MidB(sBASE_64_CHARACTERSansi, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = MidB(sBASE_64_CHARACTERSansi, (((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)
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
Base64decode = ""
Exit Function
End IF

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_CHARACTERSansi, MidB(lsGroup64, 1, 1)) - 1
Char2 = InStrB(sBASE_64_CHARACTERSansi, MidB(lsGroup64, 2, 1)) - 1
Char3 = InStrB(sBASE_64_CHARACTERSansi, MidB(lsGroup64, 3, 1)) - 1
Char4 = InStrB(sBASE_64_CHARACTERSansi, 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)
IF M4=2 Then
lsGroup64 = lsGroup64 & chrB(65)
End IF
Char1 = InStrB(sBASE_64_CHARACTERSansi, MidB(lsGroup64, 1, 1)) - 1
Char2 = InStrB(sBASE_64_CHARACTERSansi, MidB(lsGroup64, 2, 1)) - 1
Char3 = InStrB(sBASE_64_CHARACTERSansi, MidB(lsGroup64, 3, 1)) - 1
Char4 = InStrB(sBASE_64_CHARACTERSansi, 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


'################################## 사용 샘플 ##################################
Dim ORIGNvalue : ORIGNvalue = "하완어환오라ㅓ고ㅓ엉ㅁㄹ22ㅁㅁ@@ㅇㅁㄹ;ㄴㅇaabcsdfaeerdfadf"
Dim EncodeA : EncodeA = strAnsi2Unicode(Base64encode(strUnicode2Ansi(ORIGNvalue)))
Dim DecodeA : DecodeA = strAnsi2Unicode(Base64decode(strUnicode2Ansi(EncodeA)))

response.write "------------------------------------------------------------------------<BR>"
response.write "lsResult : " & ORIGNvalue & "<BR>"
response.write "lsResult : " & EncodeA & "<BR>"
response.write "lsResult : " & DecodeA & "<BR>"
response.write "------------------------------------------------------------------------<BR>"

댓글목록

등록된 댓글이 없습니다.


Total 198건 3 페이지
  • RSS
기술자료 목록
158
ClassicASP   18167  2013-04-26 09:13  
157
ClassicASP   21942  2013-04-07 02:12  
156
ClassicASP   15640  2013-03-26 22:55 ~ 2020-05-05 13:10  
155
ClassicASP   22278  2013-03-07 22:16 ~ 2015-12-02 00:00  
154
ClassicASP   17256  2013-02-13 21:34  
153
ClassicASP   16452  2013-02-05 11:38 ~ 2014-04-28 00:00  
152
ClassicASP   23143  2013-02-04 17:56  
151
ClassicASP   22304  2013-02-02 14:28  
150
ClassicASP   19462  2013-02-01 20:56  
149
ClassicASP   17325  2013-01-31 02:14  
148
ClassicASP   14899  2012-11-16 21:44 ~ 2012-11-17 00:00  
147
ClassicASP   13210  2012-11-16 21:20  
146
ClassicASP   20281  2012-11-14 20:00  
145
ClassicASP   21648  2012-11-14 19:52 ~ 2016-01-26 00:00  
144
ClassicASP   16535  2012-10-23 00:30 ~ 2014-08-13 00:00  
143
ClassicASP   21191  2012-09-07 19:53  
열람
ClassicASP   18006  2012-09-07 19:38  
141
ClassicASP   23687  2012-08-23 19:16 ~ 2013-12-20 00:00  
140
ClassicASP   13958  2012-06-23 23:01  
139
ClassicASP   18715  2012-06-13 13:37  

검색

해피정닷컴 정보

회사소개 회사연혁 협력사 오시는길 서비스 이용약관 개인정보 처리방침

회사명: 해피정닷컴   대표: 정창용   전화: 070-7600-3500   팩스: 042-670-8272
주소: (34368) 대전시 대덕구 대화로 160 대전산업용재유통단지 1동 222호
개인정보보호책임자: 정창용   사업자번호: 119-05-36414
통신판매업신고: 제2024-대전대덕-0405호 [사업자등록확인]  
Copyright 2001~2024 해피정닷컴. All Rights Reserved.