Data encryption / decryption function

Post Reply
Posts: 793
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Location: Groningen, Netherlands
Flag: Netherlands

Data encryption / decryption function

Post by Henko »

option base 1
print crypt$("intext","this-key","outtext",1)
pause 2
print ! print
print crypt$("outtext","this-key","wastebin",-1)

' This function will encrypt a text-file which must be present
' in the map where the program is started.
' Encryption is done with mode=1, the result is written to the
' output-file and is given back by the function as a string.
' Decryption to readable tekst must be done with mode=-1, and
' of course with the same key. When trying to decrypt with a
' different key, the program may crash, which might be a desirable
' feature in this special case.
' The key may consist of any caracters, but normally it is a easy
' to remember word. Maximum length is 15 letters (change the dim-
' statement if you want more). Preferable lengths are prime numbers
' such as 7 or 13.
' The encrypted text cannot easily be deciphered without the key,
' as any specific letter in the text is replaced by different
' tokens in the encrypted text.
' The input-file must be enclosed in double quotes to ensure that
' the entire file is regarded as one (1) string.
' the function works for either option base 0 or 1
def crypt$(file_in$,key$,file_out$,mode)
base=substr$("10",1,1) ' base=option base x
dim key(15)
file file_in$ input in$
lk=len(key$) ! lt=len(in$) ! out$=""
for i=base to lk-1+base ! key(i)=asc(substr$(key$,i,i)) ! next i
for i=base to lt-1+base
out$=out$ & chr$(asc(substr$(in$,i,i))+mode*key(base+mod(i,lk)))
next i
file file_out$ delete ! file file_out$ print """" & out$ & """"
end def

def mod(a,m)
d=a/m ! mod=m*(d-floor(d))
end def

Post Reply