The dialect of BASIC used for this program is QBASIC. Thus it runs on the QBASIC interpreter and can be compiled by QuickBASIC 4.5, PDS 7.1 or Visual BASIC for DOS 1.0. The Ciphersaber strong encryption project is described on the Ciphersaber website which should be read for more information.
The program itself can be used to encipher and decipher files. It uses the Ciphersaber-1 version of the RC4 algorithm which has now been superceded by the Ciphersaber-2 version.
Firstly it is necessary to declare all the variables and initialise them. Here the standard postfix characters have been used to specify the variable types. The "$" means that the variable is for strings, the "%" means that the variable is for 16-bit signed integers and the "&" means that the variable is for 32-bit signed integers. String variables are automatically initialised to the empty string when they are declared and integers are initialised to zero. The RANDOMIZE TIMER statement ensures that a unique pseudorandom sequence is generated when that is required later in the program.
<<DeclarationAndInitialisation>>= DIM fin$, f1%, fout$, f2%, ky$, q$, i%, j%, j&, s%(255) RANDOMIZE TIMER
We need to get user input to tell the program whether encoding is intended or decoding, which file we are interested in, and what the key phrase is supposed to be. We also need to open the two files which we are going to work on: one for input; one for output. As this program was originally written to be as short as possible a minimal method of indicating whether we wish to encode a file or decode it is used: in short the input filename is prefixed with "+" when the intention is to encrypt and with "-" when the intention is to decrypt.
<<InputOutput>>= INPUT "Input File (+Encode, -Decode), Output File, Key: ", fin$, fout$,ky$ LET f1% = FREEFILE OPEN MID$(fin$, 2) FOR BINARY AS #f1% LET f2% = FREEFILE OPEN fout$ FOR BINARY AS #f2%
A ciphersaber key consists of a user chosen section and a 10 character pseudorandom sequence. The sequence ensures that the key is at least 10 characters long. The pseudorandom sequence is stored as the first part of a Ciphersaber encoded file so that it can be retrieved when the decoding is done later on.
<<KeyCreation>>= LET q$ = CHR$(RND * 256) + CHR$(RND * 256) + CHR$(RND * 256) + CHR$(RND * 256) + CHR$(RND * 256) + CHR$(RND * 256) + CHR$(RND * 256) + CHR$(RND * 256) + CHR$(RND * 256) + CHR$(RND * 256) IF LEFT$(fin$, 1) = "-" THEN GET #f1%, , q$ ELSE PUT #f2%, , q$ LET ky$ = ky$ + q$
Once we have a long enough key the next step is to use it to create a coding array which will be used in the (de)crypting phase.
<<CodingArrayInitialisation>>= FOR j% = 0 TO 255 LET s%(j%) = j% NEXT FOR i% = 0 TO 255 LET j% = (j% + s%(i%) + ASC(MID$(ky$, i% MOD LEN(ky$) + 1, 1))) MOD 256 SWAP s%(j%), s%(i%) NEXT
Now we're ready to do the (de)crypting. The RC4 coding method which we are using is symmetrical. In other words we encrypt and decrypt a file in the same way, so we can use the same program to do either job. The following section shows how. Basically we read the input file one character at a time, use it to update the coding array, (de)crypt it and print it out in the new file.
<<Transformation>>= LET ky$ = " " FOR j& = 1 TO LOF(f1%) + (LEFT$(fin$, 1) = "-") * 10 LET i% = (i% + s%(j& MOD 256)) MOD 256 SWAP s%(i%), s%(j& MOD 256) GET #f1%, , ky$ LET ky$ = CHR$(ASC(ky$) XOR s%((s%(j& MOD 256) + s%(i%)) MOD 256)) PUT #f2%, , ky$ NEXT
Finally it's time to go home. We close down the open files and return to the operating system.
<<FinishUp>>= CLOSE #f1%, f2% SYSTEM
So we put it all together, et voila!