Hi there, Im trying to learn how to handle reading and writing from txt files and strings. I decided to try it out by doing a simple encryption program. Heres the code:
Rem Project: encryption
Rem Created: 2004-06-08 20:54:49
Rem ***** Main Source File *****
rem setup
set display mode 640, 480, 16
sync off
REM ------ KEY ------
rem read the key
open to read 1, "key.txt"
read string 1, key$
rem make all lower case
key$ = lower$(key$)
rem check length
keylength = len(key$)
if keylength <> 29
print "Key is to short! Key should contain 29 signs. Aborting operation."
print "Length of key: ";keylength
suspend for key
end
endif
rem make the kay array
dim keyarray$(28)
rem write key into array
for x=1 to 29
keychar$ = mid$(key$, x)
keyarray$(x-1) = keychar$
next x
rem print key
cls
rem temp variable, captial A = 65
ascii = 64
rem print key
print "This is the key: "
for x=0 to 25
inc ascii
print chr$(ascii);" = ";keyarray$(x)
next x
print chr$(197);" = ";keyarray$(26)
print chr$(196);" = ";keyarray$(27)
print chr$(214);" = ";keyarray$(28)
rem print done message
print " "
print "Key contains ";keylength;" characters."
print " "
print "Key loaded... Press any key to continue"
suspend for key
REM ------ KEY END ------
REM ------ MESSAGE ------
cls
set cursor 150,50
print "Loading message: "
open to read 2, "input.txt"
read string 2, clear$
clear$ = lower$(clear$)
filelength = len(clear$)
rem print done mesage
set cursor 150,100
print clear$
set cursor 150,150
print "Length of message: ";filelength
set cursor 150,200
print "Message loaded... Press any key to continue"
suspend for key
cls
REM ------ MESSAGE ------
rem this is where the finished product will go
crypt$ = ""
rem prepare to translate
set cursor 100,100
print "Variables created, ready to translate... Press any key to continue."
suspend for key
cls
REM ------ ENCRYPTION ------
for x=0 to filelength
char$ = mid$(clear$, x)
set cursor 150,50
print "char$: ";char$
rem set cursor back to normal
set cursor 0,0
rem the ascii variable, 97 = lower case a... ascii is increased so 96 have no effect
ascii = 96
rem this is temp space
y$ = "6"
for translate=-1 to 31
inc ascii
if asc(char$) = ascii then y$ = keyarray$(translate+1)
rem åäö
rem 229=å 228=ä 246=ö
rem keyarray 26=å 27=ä 28=ö
if asc(char$) = 229 then y$ = keyarray$(26)
if asc(char$) = 228 then y$ = keyarray$(27)
if asc(char$) = 246 then y$ = keyarray$(28)
next translate
rem if nothing matched, dont encrypt character
if y$ = "6" then y$ = char$
set cursor 150,100
print "y$: ";y$
crypt$ = crypt$ + y$
set cursor 150,150
print "crypt$: ";crypt$
set cursor 150,200
print "Press space to encrypt next character or Escpape to encrypt entire message."
suspend for key
cls
next x
REM ------ ENCRYPTION END ------
open to write 3, "encrypted.txt"
write string 3, crypt$
rem print done message
cls
set cursor 150,50
print "Encryption done! Press any key to quit."
set cursor 150,100
print "Encrypted message: "
set cursor 150,150
print crypt$
sync
suspend for key
The problem is, if i make the input file to big then the program shuts down when its trying to read it. I assume im breaking some max limits on some variables or something. Because it works if the input file is quite small. What is the maximum string length? Or whatever it is im making to big
Thanks
Mizipzor
[Edit] Hehe, maybe i should post how to use the program to
. You need to txt files, input and key. The input file should hold whatever message you want to encrypt. The key file is a bit trickier.
The key files is how the message would be encrypted. Type the entire alphabet in a single line plus three characters. It doesnt matter which three the last characters is, theyre there because my language (swedish) has three extra characters at the end of the alphabet.
Example, if you want a to encrypted into Q and b into W then the key line should begin with QW... encrypted ab... and then follow with the rest of the alphabet. This is the line I use:
qwertyuiopasdfghjklzxcvbnmaaa
Why didnt I just post that line right away now... hmm... hehe
. Anyways the program should produce a file, encrypted.txt, which is the input.txt file encrypted
.
Thanks again
Dreaming of creating a highly advanced AI mind