Purebasic exposes your text strings to anyone who wishes to open your .exe in an editor such as notepad++ or a hex editor.
Sometimes we don't want people to see what strings are present in our program.
Eventually most professionals use an outside tool to compile local strings into data,
then decompress/decode them at runtime. For everyone else, I present some trivial
encoding and decoding examples.
hmmmm....
It wouldn't be difficult to have the compiler do the encoding for you in a
manner that encodes your strings when compiling, and then at runtime
decodes them (during a string assignment)...
as in:
f$ = "error message that you cannot hide" <--- what you see is what you get
vs.
f$ = obfuscate("error message you dont want to expose") <---encoded at compile, decoded at run
I think maybe it's a useful trick with more applications...
Anyway, here is an example of simple rolling encryption on ascii text.
Once you've encoded the text, you can cut and paste and assign it to a variable,
so that it is hidden in plain sight but also easily decoded at run time.
2016-04-08 : Edited to show examples of procedures for this. The learner version is still below...
Code: Select all
Procedure.s rc_encode(string$,salt)
For i = 1 To Len(string$)
encoded$ + Right("00"+UCase(Hex(Asc(Mid(string$,i,1)) ! ((salt+i)%256),#PB_Byte)),2)
Next
ProcedureReturn encoded$
EndProcedure
Procedure.s rc_decode(string$,salt)
For i = 1 To (Len(string$)/2)
ptr = ((i-1)*2)+1 : decoded$ + Chr(Val("$"+Mid(string$,ptr,2)) ! ((salt+i)%256))
Next
ProcedureReturn decoded$
EndProcedure
# example of usage - just cut and paste to see how it works
salt = 65342 ; can be any int from 0-65535
input$ = "It might be useful..."
junk$ = rc_encode(input$,salt)
output$ = rc_decode(junk$,salt)
Debug "SOURCE: "+input$
Debug "CIPHER: "+junk$
Debug "OUTPUT: "+output$
The code is just a way to illustrate the concept. You could also use this to save strings to disk,
or to send them in plaintext via email, web or network transfer. Enjoy!
Warning: This is not crypto... it's just a way to make plain text not so plain. It's not suitable for important data.
This is VERY vulnerable to anyone with even basic crypto knowledge or even if they've heard of Alan Turing.
ALL it does is hide plain text in plain sight!
Code: Select all
string$ = "I am just a simple test of an inefficient algorithm on a string with an embedded CR inside! "+Chr(13)+"Yes, a test!"
Debug "Let's get started!"
Debug "Text to encode. Notice that the example has two lines..."
Debug "---"
Debug string$
Debug "---"
; ---- begin encoding
salt = 21762 ; should be a hash but 0-65535 is fine
strlen = Len(string$)
charcount = 0
output$=""
For i = 1 To strlen ; this is just an example - we don't care
; what the internal representation is.
ab$ = Mid(string$,charcount,1) ; use word instead of bytes for unicode
a.b = Asc(ab$)
b.b = ((salt+i) % 256) ; weak salt makes it harder to decode
c = a ! b ; (in assembly language this is trivial!)
d$ = Right("00"+UCase(Hex(c,#PB_Byte )),2)
output$ + d$
Next
; ---- all done encoding
Debug "encodng "+Str(strlen)+" chars with rolling XOR encryption and a weak salt."
Debug "--------"
Debug "Here's what comes out. This is what would be "
Debug "seen by a user poking around in the .exe: "
Debug makeitpretty(output$)+Chr(13)
Debug "--------"
string$ = output$ ; copy encoded string into input
output$ = ""; ; and reset output
; ---- let's decode this
strlen = Int((Len(string$)+1)/2)
charcount = 0
For i = 0 To strlen ; reverse the process
pos = 1 + (i*2)
ab$ = "$"+Mid(string$,pos,2) ; word instead of bytes for unicode
a.b = Val(ab$)
b.b = ((salt+i+1) % 256) ; n % 65536 for unicode.
c = a ! b
output$ + Chr(c)
Next
; ---- all done decoding
Debug "This is the decoded output, ready for use!:"
Debug output$