' QBasic CipherSaber implementation ' Copyright (C) 2000 ÉRDI Gergõ ' ' This program is free software; you can redistribute it and/or modify ' it under the terms of the GNU General Public License as published by ' the Free Software Foundation; either version 2 of the License, or ' (at your option) any later version. ' ' This program is distributed in the hope that it will be useful, ' but WITHOUT ANY WARRANTY; without even the implied warranty of ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ' GNU General Public License for more details. ' ' You should have received a copy of the GNU General Public License ' along with this program; if not, write to the Free Software ' Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ' ' See http://ciphersaber.gurus.com/ for the official CipherSaber home page. DECLARE SUB createiv (outfile!) DECLARE FUNCTION askmode! () DECLARE SUB encrypt (infilename$, outfilename$) DECLARE SUB decrypt (infilename$, outfilename$) DECLARE SUB readiv (file!) DECLARE FUNCTION askinput$ () DECLARE FUNCTION askoutput$ () DECLARE SUB askkey () DECLARE SUB init () DECLARE FUNCTION cipher! (byte!) DIM SHARED state(255) AS INTEGER DIM SHARED keydata(255) AS INTEGER DIM SHARED keylength AS INTEGER infilename$ = askinput outfilename$ = askoutput IF askmode = 0 THEN decrypt infilename$, outfilename$ ELSE encrypt infilename$, outfilename$ END IF FUNCTION askinput$ askinput: INPUT "Enter input filename: ", tmp$ IF tmp$ = "" THEN GOTO askinput END IF askinput$ = tmp$ END FUNCTION SUB askkey enterkey: INPUT "Enter key: ", keystring$ keylength = LEN(keystring$) IF keylength = 0 OR keylength > 246 THEN GOTO enterkey END IF FOR i = 0 TO (keylength - 1) keydata(i) = ASC(MID$(keystring$, (i + 1), 1)) NEXT i END SUB FUNCTION askmode answer = -1 PRINT "Encrypt/Decrypt [e/d]?" askmode: char$ = INKEY$ IF char$ = "d" OR char$ = "D" THEN answer = 0 END IF IF char$ = "e" OR char$ = "E" THEN answer = 1 END IF IF answer = -1 THEN GOTO askmode askmode = answer END FUNCTION FUNCTION askoutput$ askoutput: INPUT "Enter output file name: ", tmp$ IF (tmp$ = "") THEN GOTO askoutput END IF askoutput$ = tmp$ END FUNCTION FUNCTION cipher (byte) STATIC i, j i = (i + 1) MOD 256 j = (j + state(i)) MOD 256 SWAP state(i), state(j) n = (state(i) + state(j)) MOD 256 ret = byte XOR state(n) cipher = ret END FUNCTION SUB createiv (outfile) RANDOMIZE TIMER FOR i = 0 TO 9 byte = RND * 256 keydata(keylength + i) = byte PRINT #outfile, CHR$(byte); NEXT i keylength = keylength + 10 END SUB SUB decrypt (infilename$, outfilename$) infile = FREEFILE OPEN infilename$ FOR BINARY AS #infile outfile = FREEFILE OPEN outfilename$ FOR OUTPUT AS #outfile askkey readiv infile init DO char = ASC(INPUT$(1, #infile)) cchar = cipher(char) PRINT #outfile, CHR$(cchar); LOOP UNTIL LOC(infile) = LOF(infile) CLOSE infile CLOSE outfile END SUB SUB encrypt (infilename$, outfilename$) infile = FREEFILE OPEN infilename$ FOR BINARY AS #infile outfile = FREEFILE OPEN outfilename$ FOR OUTPUT AS #outfile askkey createiv outfile init DO char = ASC(INPUT$(1, #infile)) cchar = cipher(char) PRINT #outfile, CHR$(cchar); LOOP UNTIL LOC(infile) = LOF(infile) CLOSE infile CLOSE outfile END SUB SUB init FOR i = 0 TO 255 state(i) = i NEXT i j = 0 FOR i = 0 TO 255 n = i MOD keylength j = (j + state(i) + keydata(n)) MOD 256 SWAP state(i), state(j) NEXT i END SUB SUB readiv (file) FOR i = 0 TO 9 byte = ASC(INPUT$(1, #file)) keydata(keylength + i) = byte NEXT i keylength = keylength + 10 END SUB