@ Tom0001:
Of course I wrote it myself. My first encryption/decryption program using a random seed was in QuickBasic long before Darkbasic existed.
It works because random numbers aren't really random. When you specify a particular seed it will always pick the same random numbers if it's given the same range to choose from. That's why it's perfect for encryption/decryption routines. I use IanMs commands because it makes it easier rather than making more FOR/NEXT loops to search for characters in the keys. This version also repeats code within the encryption and decryption routines because I wanted everything to be in the same place for Dartnub (make it a bit easier). Normally I put the random seed and encryption key creation in it's own function separate from the encryption and decryption functions.
All it does is create Key0$ as the base string for any character you'd normally type on the keyboard (so it's only good for text) using CHR$(32) through CHR$(126). Copies that key base to Key1$ and randomly picks characters from Key1$ to put into Key2$. I use three keys because Key1$ is slowly destroyed (each random character picked is removed) so that characters aren't repeated in Key2$. Once that process is finished Key0$ is the base key and Key2$ is the encryption key. To encrypt it uses a FOR/NEXT loop to go through each character in the string you want to encrypt, it looks at the position of the character in the base key and adds whatever character is in the same position in the encryption key. To decrypt it does the opposite (looks at the encryption key for the character and adds whatever character is in the base key).
So if your keys are like this:
Key0$="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Key2$="KSVDBJLORQXZNCWTMPFUIEHAGY"
The word "HELLO" will always encrypt to "OBZZW" and vice-versa.
Using the random seed just makes it easier by letting the computer create the encryption key rather than doing it by hand like I did for the example above.
Here's the whole thing (just create a text file called "! Text.txt"):
` Set the current seed
CSeed=4764
` Delete encrypted file if it exists
if file exist("! Encrypted.txt") then delete file "! Encrypted.txt"
` Open the text file to encrypt and the file to write the encryption
open to read 1,"! Text.txt"
open to write 2,"! Encrypted.txt"
repeat
` Read a line in the file to encrypt
read string 1,Tex$
` Encrypt the string
Encrypt$=Encrypt(CSeed,Tex$)
` Write the string to the encrypted file
write string 2,Encrypt$
` Leave the REPEAT/UNTIL when the text file reading is complete
until file end(1)
` Close files
close file 1
close file 2
` To make sure it works decrypt the last encryption
` Delete the decrypted file if it exists
if file exist("! Decrypted.txt") then delete file "! Decrypted.txt"
` Open the encrypted file and the file to write the decryption
open to read 1,"! Encrypted.txt"
open to write 2,"! Decrypted.txt"
repeat
` Read a line in the encrypted file
read string 1,Tex$
` Decrypt the string
Decrypt$=Decrypt(CSeed,Tex$)
` Write the string to the decrypted file
write string 2,Decrypt$
` Leave the REPEAT/UNTIL when the encrypted file reading is complete
until file end(1)
` Close the files
close file 1
close file 2
end
` Encrypt a string
function Encrypt(Seed,Tex$)
` Pick the random seed
randomize Seed
` Create the base key
for t=32 to 126:Key0$=Key0$+chr$(t):next t
` Copy the base key to Key1$
Key1$=Key0$
` Create the encryption key
for t=1 to 95
` Pick a random character from Key1$
a=rnd(len(Key1$)-1)+1
` Add the random charater to Key2$
Key2$=Key2$+mid$(Key1$,a)
` Remove the random character from Key1$
Key1$=remove$(Key1$,mid$(Key1$,a))
next t
` Encrypt the string
for t=1 to len(Tex$)
Enc$=Enc$+mid$(Key2$,find char(Key0$,mid$(Tex$,t)))
next t
` Return the encrypted text
endfunction Enc$
` Decrypt a string
function Decrypt(Seed,Tex$)
` Pick the random seed
randomize Seed
` Create the base key
for t=32 to 126:Key0$=Key0$+chr$(t):next t
` Copy the base key to Key1$
Key1$=Key0$
` Create the encryption key
for t=1 to 95
` Pick a random character from Key1$
a=rnd(len(Key1$)-1)+1
` Add the random charater to Key2$
Key2$=Key2$+mid$(Key1$,a)
` Remove the random character from Key1$
Key1$=remove$(Key1$,mid$(Key1$,a))
next t
` Decrypt the string
for t=1 to len(Tex$)
Decr$=Decr$+mid$(Key0$,find char(Key2$,mid$(Tex$,t)))
next t
` Return the decrypted text
endfunction Decr$