|
Post by Rod on May 27, 2019 4:58:15 GMT -5
Base64 and Base85 are encryption methods to allow data to be sent over common communication channels. Graphic and numeric data occupy the full range of ASII values, 0-255. Many communication channels were established to move "text" about and so ASCII control characters are not guaranteed to arrive at the other end.
The two encryption processes take the full range data and convert it to a "safe" range. Usually packing 0-255 into the a-z A-Z range.
xcoder is asking for code examples. The problem I see is that there are multiple standards for each method. So we perhaps need more info on what systems are involved.
It may be that an API call exists to convert data.
|
|
|
Post by tsh73 on May 27, 2019 8:01:03 GMT -5
|
|
|
Post by Chris Iverson on May 27, 2019 14:39:06 GMT -5
Here's some native LB code for converting to/from Base85, with both of the major encodings supported.
str$ = Base85Encode$("testing") print str$ print Base85Decode$(str$) print str$ = Z85Encode$("testing") print str$ print Z85Decode$(str$)
Function Base85Encode$(dat$) ascii85$ = "!" + chr$(34) + "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstu"
Base85Encode$ = B85EncodeWithCode$(dat$, ascii85$) End Function
Function Base85Decode$(dat$) ascii85$ = "!" + chr$(34) + "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstu"
Base85Decode$ = B85DecodeWithCode$(dat$, ascii85$) End Function
Function Z85Encode$(dat$) Z85$ = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
Z85Encode$ = B85EncodeWithCode$(dat$, Z85$) End Function
Function Z85Decode$(dat$) Z85$ = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
Z85Decode$ = B85DecodeWithCode$(dat$, Z85$) End Function
Function B85EncodeWithCode$(dat$, encoding$) datLenMod = len(dat$) mod 4 if datLenMod then padding = 4 - datLenMod
for x = 1 to padding dat$ = dat$ + chr$(0) next x
For x = 1 to len(dat$) step 4 temp$ = mid$(dat$, x, 4)
For y = 1 to 4 conv = (conv * 256) + asc(mid$(temp$, y, 1)) Next y
while conv > 0 char = (conv mod 85) + 1 chunk$ = mid$(encoding$, char, 1) + chunk$ conv = int(conv / 85) wend
B85EncodeWithCode$ = B85EncodeWithCode$ + chunk$ chunk$ = "" Next x
B85EncodeWithCode$ = left$(B85EncodeWithCode$, len(B85EncodeWithCode$) - padding) End Function
Function B85DecodeWithCode$(dat$, encoding$) padChr$ = right$(encoding$, 1)
datLenMod = len(dat$) mod 5 if datLenMod then padding = 5 - datLenMod
For x = 1 to padding dat$ = dat$ + padChr$ next x
For x = 1 to len(dat$) step 5 temp$ = mid$(dat$, x, 5)
For y = 1 to 5 t$ = mid$(temp$, y, 1) conv = (conv * 85) + (instr(encoding$, t$) - 1) Next y
while conv > 0 char = conv mod 256 chunk$ = chr$(char) + chunk$ conv = int(conv / 256) wend
B85DecodeWithCode$ = B85DecodeWithCode$ + chunk$ chunk$ = "" Next x
B85DecodeWithCode$ = left$(B85DecodeWithCode$, len(B85DecodeWithCode$) - padding) End Function
Wouldn't be too hard to edit this to do Base64 instead, as well.
I made this a few days ago, when xcoder posted in my TLS DLL thread, but I never got/saw any response, so I never posted it.
|
|
|
Post by xcoder on Oct 30, 2020 19:52:46 GMT -5
Base64 and Base85 are encryption methods to allow data to be sent over common communication channels. Graphic and numeric data occupy the full range of ASII values, 0-255. Many communication channels were established to move "text" about and so ASCII control characters are not guaranteed to arrive at the other end. The two encryption processes take the full range data and convert it to a "safe" range. Usually packing 0-255 into the a-z A-Z range. xcoder is asking for code examples. The problem I see is that there are multiple standards for each method. So we perhaps need more info on what systems are involved. It may be that an API call exists to convert data. Both versions of the Base85 functions will not encode/decode chr$(0) to chr$(3) characters. Is there any way of correcting this problem?
|
|
|
Post by Chris Iverson on Oct 30, 2020 23:02:01 GMT -5
Yeah, force the conversion/deconversion loops to run the proper number of times instead of stopping at zero.
My bad, good catch on that.
inp$ = chr$(0) + chr$(1) + chr$(2) + chr$(3) call printAsciiVals inp$
str$ = Base85Encode$(inp$) print str$ call printAsciiVals Base85Decode$(str$) print str$ = Z85Encode$("testing") print str$ print Z85Decode$(str$)
Function Base85Encode$(dat$) ascii85$ = "!" + chr$(34) + "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstu"
Base85Encode$ = B85EncodeWithCode$(dat$, ascii85$) End Function
Function Base85Decode$(dat$) ascii85$ = "!" + chr$(34) + "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstu"
Base85Decode$ = B85DecodeWithCode$(dat$, ascii85$) End Function
Function Z85Encode$(dat$) Z85$ = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
Z85Encode$ = B85EncodeWithCode$(dat$, Z85$) End Function
Function Z85Decode$(dat$) Z85$ = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"
Z85Decode$ = B85DecodeWithCode$(dat$, Z85$) End Function
Function B85EncodeWithCode$(dat$, encoding$) datLenMod = len(dat$) mod 4 if datLenMod then padding = 4 - datLenMod
for x = 1 to padding dat$ = dat$ + chr$(0) next x
For x = 1 to len(dat$) step 4 temp$ = mid$(dat$, x, 4)
For y = 1 to 4 conv = (conv * 256) + asc(mid$(temp$, y, 1)) Next y
for convCount = 1 to 5 char = (conv mod 85) + 1 chunk$ = mid$(encoding$, char, 1) + chunk$ conv = int(conv / 85) next convCount
B85EncodeWithCode$ = B85EncodeWithCode$ + chunk$ chunk$ = "" Next x
B85EncodeWithCode$ = left$(B85EncodeWithCode$, len(B85EncodeWithCode$) - padding) End Function
Function B85DecodeWithCode$(dat$, encoding$) padChr$ = right$(encoding$, 1)
datLenMod = len(dat$) mod 5 if datLenMod then padding = 5 - datLenMod
For x = 1 to padding dat$ = dat$ + padChr$ next x
For x = 1 to len(dat$) step 5 temp$ = mid$(dat$, x, 5)
For y = 1 to 5 t$ = mid$(temp$, y, 1) conv = (conv * 85) + (instr(encoding$, t$) - 1) Next y
for deconvCount = 1 to 4 char = conv mod 256 chunk$ = chr$(char) + chunk$ conv = int(conv / 256) next deconvCount
B85DecodeWithCode$ = B85DecodeWithCode$ + chunk$ chunk$ = "" Next x
B85DecodeWithCode$ = left$(B85DecodeWithCode$, len(B85DecodeWithCode$) - padding) End Function
Sub printAsciiVals inp$ for x = 1 to len(inp$) print dechex$(asc(mid$(inp$, x, 1))) next x
End Sub
|
|
|
Post by xcoder on Nov 5, 2020 1:25:17 GMT -5
Thank you. That is exactly what I needed. Now check out my Hexidecimal String Modem: rem Main Program Print "######################################" for i = 0 to 255 char$ = char$ + chr$(i) next i print char$ print a$ = text2Hex$(char$) print a$ print b$ = hex2Text$(a$) print b$
print:print print "End of Code" End
function text2Hex$(text$) for i=1 to len(text$) hex$ = dechex$(asc(mid$(text$, i, 1))) fullHex$ = fullHex$ + trueHex$(hex$) next i text2Hex$ = fullHex$ end function
function trueHex$(a$) if a$="0" then a$="00" if a$="1" then a$="01" if a$="2" then a$="02" if a$="3" then a$="03" if a$="4" then a$="04" if a$="5" then a$="05" if a$="6" then a$="06" if a$="7" then a$="07" if a$="8" then a$="08" if a$="9" then a$="09" if a$="A" then a$="0A" if a$="B" then a$="0B" if a$="C" then a$="0C" if a$="D" then a$="0D" if a$="E" then a$="0E" if a$="F" then a$="0F" trueHex$=a$ end function
function hex2Text$(hex$) for i=1 to len(hex$)step 2 chunk$ = mid$(hex$,i,2) ascii = hexDec(chunk$) char$ = char$ + chr$(ascii) next i hex2Text$ = char$ end function
|
|
|
Post by Chris Iverson on Nov 5, 2020 1:57:15 GMT -5
You could replace the whole trueHex$() function with this:
right$("00" + hex$, 2)
i.e.
fullHex$ = fullHex$ + right$("00" + hex$, 2)
|
|
|
Post by xcoder on Nov 5, 2020 2:30:11 GMT -5
Damn Dude, you are really skilled. I am going to use base85 and Hex String with the RC4 cipher for copy and paste in emails.
|
|