set display mode 800,600,32
randomize timer()
sync on : sync rate 30
sync : sync
 
key$ = generateKey(4)
 
print "Current Key: ",key$
input "Message: ",msg$
 
sync
 
msg$ = cryptMessage(msg$,key$)
 
center text screen width()/2 , screen height()/2 - 100 , "Encrypted Message:"
center text screen width()/2 , screen height()/2 - 50 , msg$
 
sync
 
input "Enter Key: ",key$
 
sync
 
msg$ = cryptMessage(msg$,key$)
 
center text screen width()/2 , screen height()/2 + 50 , "Decrypted Message:"
center text screen width()/2 , screen height()/2 + 100 , msg$
 
sync
 
wait key
 
function generateKey(length)
   for i = 1 to length
      key$ = key$ + str$( rnd(999) )
   next i
endfunction key$
 
function cryptMessage(msg$, key$)
   length = len(key$)
   for i = 1 to len(msg$)
      thisMsg = asc( mid$( msg$ , i ) )
      thisKey = asc( mid$( key$, i mod length ) ) + 1
      eMsg$ = eMsg$ + chr$( thisMsg ~~ thisKey )
   next i
endfunction eMsg$