Encryption program error

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Heliostrapolis

Encryption program error

Post by Heliostrapolis »

am working on an encryption/decryption program and when i decrypt the file i lose the last line. I know the problem is in this sub but i am not sure where. Here is the code, can someone help me? (i left out the part that actually decypts the file)


SUB Decrypt (infile$, outfile$, txt$(), tmptxt$(), tmpchnge(), encryption(), final())

DIM crypt(1, 1)
DIM line$(1, 1)
LET b = FREEFILE
OPEN infile$ FOR INPUT AS b
IF outfile$ = infile$ THEN
outfile$ = "c:\temp.txt"
END IF
LET c = FREEFILE
OPEN outfile$ FOR APPEND AS c

DO WHILE NOT EOF(b)
LINE INPUT #b, linetochange$
linetochange = VAL(linetochange$)
IF z = 0 THEN
tmpchange(0, 0) = linetochange
ELSEIF z = 1 THEN
tmpchange(0, 1) = linetochange
ELSEIF z = 2 THEN
tmpchange(1, 0) = linetochange
ELSEIF z = 3 THEN
tmpchange(1, 1) = linetochange
FOR x = 0 TO 1
FOR y = 0 TO 1
IF final(x, y) > 1.5 AND final(x, y) < 2.5 THEN
final(x, y) = 0
END IF
IF final(x, y) > .9 AND final(x, y) < 1.9 THEN
PRINT #c, line$
PRINT line$
line$ = ""
ELSE
line$ = line$ + CHR$(final(x, y))
END IF
NEXT
NEXT
z = -1
END IF
z = z + 1
LOOP
END SUB
Antoni Gual

Post by Antoni Gual »

Here is the problem:
IF final(x, y) > .9 AND final(x, y) < 1.9 THEN
PRINT #c, line$
PRINT line$
line$ = ""
ELSE
line$ = line$ + CHR$(final(x, y))
END IF
if the if condition is false you don't print output, so you may reach the end of the input file with a line$ waiting to be printed.
You should add after the WEND this line:

Code: Select all

IF LEN(line$)>0 THEN PRINT #c,line$
Heliostrapolis

Ummmm, no

Post by Heliostrapolis »

That isn't the problem because when i am encoding the line of text i add
chr$(1) to the very end of the line

DO WHILE NOT EOF(b)
LINE INPUT #b, linetochange$
FOR x = LBOUND(txt$) TO UBOUND(txt$)
tmptxt$(x) = txt$(x)
NEXT
lin = lin + 1
REDIM txt$(lin)
linetochange$ = linetochange$ + CHR$(1)
txt$(lin) = linetochange$
FOR x = LBOUND(tmptxt$) TO UBOUND(tmptxt$)
txt$(x) = tmptxt$(x)
NEXT
REDIM tmptxt$(lin)
LOOP
User avatar
Levi
Veteran
Posts: 79
Joined: Tue Jul 27, 2004 11:44 pm
Location: Alone and forgotten
Contact:

okay...

Post by Levi »

I'm sorry I know you don't have the whole program loaded and are sure it's found in here but I'm having a hard time following the purpose of most of it so any help I can offer will be limited because of my minds needs to know everything... But I'm having troubles with the code right here

IF final(x, y) > 1.5 AND final(x, y) < 2.5 THEN
final(x, y) = 0
END IF
IF final(x, y) > .9 AND final(x, y) < 1.9 THEN


Now as you can see the QB would continue right down the line after each if statment. So even if the number was less than 1.9 it would never gain access to this if statment and thus the < 1.9 is pointless. Because of the stament before it that says if it is greater than 1.5. Upon being 1.5, 1.6, 1.7 1.8 all below 1.9 the number becomes zero. And so you point of < 1.9 is pointless. Unless you change 1.9 to 1.5 or place this one before the other and say if not > 1.5 but > 1.9. This clearly seperates the two for that area.

Now you posted some extra code in reply to anothers post....this describes how you encode the text though not in complete detail. You simply added onto the txt$ array one last digit and placed the line to change + chr$(1) into it. I can only guess however what txt$ holds, I doubt it could hold an entire file unless the files you plan on encrypting are small ones. And even with that the code you've presented us makes no sense, we cannot truly comprehend the data being given, how the data is recieved originally, what processes you go through to give the text exactly what it needs.

your Print #c, line$ will print to your output file but only if the final(x,y) is not <1.51 or > 2.5. And so if it's not then it just keeps right on going. Adding a chr$(0) to the end of the line as it goes.

And line$ I can only assuem it's a global variable cause you don't mention it anywhere in your current code. How it relates to z or final. I in truth can't help you. I would need more information. Not how you encode or decode but what each variable is for, why you handled z in that manner. z = -1 has no if handle. It'll simply say z = -1, z = -1 skip all ifs, z = z +1 z = 0 so use z = 0 if. And it repeatedly does this until all lines are taken from the file. Now here's more confusion. Are your files encoded into giant files of lines with only one character in them? This would explain the txt$(), and the use of many of these things. Chr$(1) becomes either an identifier or somethign that says okay nothign to look at here. becaue when you say (val(linetochange$)) unless it's one character that is a number, or heck even a large number it won't work. So there is alot of unexplained stuff that would make our helping in decoding alot easier. DOn't give us the how to do it, give us the why. Why things are handled in a certain way, because they're numbers you got through an algorithm. Why you choose to send info to the file while others rot. What is the deal? Come on you can tell me.

Sorry i could't help. I guess it's been too long since I've programmed to be of any real help.
Later days,
Matthew

May those who love us love us
And those who don't
May the good Lord turn their hearts
And if he doesn't
May he turn their ankles
So we'll know them by their limping
-Irish prayer
Heliostrapolis

Figured it out (-:

Post by Heliostrapolis »

Yeah, good point about >1.5 < 1.9 thing
Never even realized
I finished the program, uploaded it, it ain't perfect and i've already made some revisions, but it works. It encrypts and decrypts text files.
Post Reply