The lack of a true random number is vital to encrypt/decrypt. To encrypt using a random number you use a specific random seed, create the encryption key, and encrypt. When you want to decrypt you use the same seed, create another encryption key and decrypt. If the seed is the same the decryption key created will be exactly the same and thus work to decrypt properly.
` Text Encryption
set display mode 800,600,32
global Key1$
global Key2$
a$="This is a test of encrypting/decrypting text."
` Create an encryption key
CreateEncryptionKey(543)
` Make Encrypt1$ equal the encrypted version of a$
Encrypt1$=Encrypt(a$)
` Save the encryption key
OldKey1$=Key2$
` Create a new encryption key using a different seed
CreateEncryptionKey(433)
` Try to decrypt Encrypt1$ with new key
Decrypt1$=Decrypt(Encrypt1$)
` Save the second encryption key
OldKey2$=Key2$
` Create the same encryption key using the same seed as the first
CreateEncryptionKey(543)
` Decrypt Encrypt1$ using the newest encryption key
Decrypt2$=Decrypt(Encrypt1$)
print "Text to encrypt/decrypt = "+a$
print ""
print ""
print "First Encryption Key Seed #543 = "+OldKey1$
print ""
print "Second Encryption Key Seed #433 = "+OldKey2$
print ""
print "Third Encryption Key Seed #543 = "+Key2$
print ""
print ""
print "First Encryption of text = "+Encrypt1$
print ""
print ""
print "Decryption of text using wrong seed = "+Decrypt1$
print ""
print "Decryption of text using right seed = "+Decrypt2$
print ""
wait key
end
` Creates an encryption key
function CreateEncryptionKey(Seed)
randomize Seed
Key1$="" ` Normal Key
Key2$="" ` Encryption Key
` Create the Normal Key
for t=32 to 126
Key1$=Key1$+chr$(t)
next t
` Create the Encryption Key
for t=1 to 95
a=rnd(len(Key1$)-1)+1 ` Pick a character in Key1$
Key2$=Key2$+mid$(Key1$,a) ` Add mid$(key1$,a) to Key2$
Key1$ = left$(Key1$,a-1) + right$(Key1$,len(Key1$)-a) ` Remove the picked character from Key1$
next t
` Reset the Normal Key
for t=32 to 126
Key1$=Key1$+chr$(t)
next t
endfunction
` Encrypt text using Key1$ (normal key) and Key2$ (encryption key)
function Encrypt(Tex$)
for t=1 to len(Tex$)
a$=mid$(Tex$,t)
for t2=1 to len(Key1$)
if a$=mid$(Key1$,t2)
Enc$=Enc$+mid$(Key2$,t2)
endif
next t2
next t
endfunction Enc$
` Decrypt text using Key1$ (normal key) and Key2$ (encryption key)
function Decrypt(Tex$)
for t=1 to len(Tex$)
a$=mid$(Tex$,t)
for t2=1 to len(Key2$)
if a$=mid$(Key2$,t2)
Decr$=Decr$+mid$(Key1$,t2)
endif
next t2
next t
endfunction Decr$
