Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Outputting to file
#1
For some reason, my program prints just one line, then quits. I'm thinking it may be because my encryption program is sticking an EOF character in on accident, but not sure...

Code:
Dim plaintxt As String
Dim filename As String
Dim pass As Integer
Dim xorchar As Integer
Dim encrypted As String
Dim ciphered As String
Dim outfile As String
Dim file1 As Integer
Dim file2 As Integer

Input "File to encrypt: ", filename
Input "Password (numeric): ", pass
Input "File to output to: ", outfile
file1 = FreeFile
Open filename For Input As #file1
file2 = FreeFile
Open outfile For Output As #file2

Randomize pass

While Not EOF(file1)
    Line Input #file1, plaintxt
    
    For i = 1 To Len(plaintxt)
        xorchar = Int(Rnd(1) * 255 + 1)
        encrypted += chr$(asc(mid$(plaintxt,i,1)) xor xorchar)
    Next i
    Print #file2, encrypted
Wend
Close #1
Close #2
Print "Done!"
Print
Print

'Try unencrypting

Input "File to cipher: ", filename
Input "Password (numeric): ", pass
Input "Output File: ", outfile

file1 = FreeFile
Open filename For Input As #file1
file2 = FreeFile
Open outfile For Output As #file2
Randomize pass

While Not Eof(file1)
    Line Input #file1, encrypted
    For i = 1 To Len(encrypted)
        xorchar = Int(Rnd(1) * 255 + 1)
        ciphered += chr$(asc(mid$(encrypted, i, 1)) xor xorchar)
    Next i
    Print #file2, ciphered
Wend
Close #1
Close #2

Print "Done!"
sleep

Any ideas how to fix it?
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#2
I wrote a similar cypher program once, except there's no password feature and it could load different cyphers from files.

Anyway, one thing I noticed was the Close statements had numbers instead of the FREEFILE variables you declared. Other than that, I'm afraid I don't have the advanced programming skills to help you further. But at least I tried.
url=http://www.freewebs.com/boxtopstuff/]Planet Boxtop[/url] (Look out for the redesign!)
The only member of QBNF with severe "tomorrow syndrome."
Reply
#3
Thanks... I'm stilling having a problem guys... Sad Any ideas at all?
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#4
Hey, I tried your code and me encrypted file was 3 times the size and my ciphered file was tottally messed up, the first line was repeated along with some wierd characters.
Reply
#5
OK, it took me some time, but i think i got it.

I don't think Line Input will work with the ciphered data, because its not normal text.

I would suggest a method like this

I haven't done any error checking in the code, but you can use the same function for crypt/decrypt.

The key is a value 0-255 (although some values may be pointless IE 32 just flips the case of alpha text)

Hope this gives you some ideas, you could add your random stuff or use a string as the key.

It also will work on any file, not just text.

Code:
Option Explicit

Sub XORCrypt_Simple(inFile As String, outfile As String, key As uByte)
  Dim temp As uByte
  Dim hFile1 As Integer
  Dim hFile2 As Integer
  hFile1 = FreeFile
  Open inFile For Binary As #hFile1
  hFile2 = FreeFile
  Open outfile For Binary As #hFile2
    While NOT EOF(hFile1)
      Get #hFile1, , temp
      temp = temp XOR key
      Put #hFile2, , temp
    Wend
  Close
End Sub

XORCrypt_Simple("in.txt", "out.txt", 33)

XORCrypt_Simple("out.txt", "decry.txt", 33)
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#6
Thank you yetifoot. Now it works... on my computer. My only problem is that I'm not sure that the random number generator is the same on each computer. Would someone mind testing it for me? Here is the code:

Code:
Declare Sub XORCrypt_Simple(inFile As String, outFile As String)

Dim plaintxt As String
Dim filename As String
Dim pass As Integer
Dim xorchar As Integer
Dim encrypted As String
Dim ciphered As String
Dim outfile As String
Dim file1 As Integer
Dim file2 As Integer

Input "File to encrypt: ", filename
Input "Password (numeric): ", pass
Input "File to output to: ", outfile
Randomize pass

XORCrypt_Simple(filename, outfile)

Print "Done!"
Print
Print

'Try unencrypting

Input "File to cipher: ", filename
Input "Password (numeric): ", pass
Input "Output File: ", outfile

Randomize pass
XORCrypt_Simple(filename, outfile)
Print "Done!"
sleep

Sub XORCrypt_Simple(inFile As String, outFile As String)
  Dim temp As uByte
  Dim hFile1 As Integer
  Dim hFile2 As Integer
  hFile1 = FreeFile
  Open inFile For Binary As #hFile1
  hFile2 = FreeFile
  Open outFile For Binary As #hFile2
    While NOT EOF(hFile1)
      Get #hFile1, , temp
      xorchar = Int(Rnd(1) * 255 + 1)
      temp = temp XOR xorchar
      Put #hFile2, , temp
    Wend
  Close
End Sub

Here is the encrypted message:

Code:
Á܏:™Œlº¿`Jsˆ±„™Ö¥utfùýœ½è9;rhzÈPØÿŠ¸¼1“®-r±L&͍>²ùØA‹¤x®˜ ³¸åPgÙ!iòKɆ £¹Z—x?Wjëi¦Z£;^U252Jh!\“ê¼yfõ=°®¿~¬1–& ¦ðT©¬Âê©ò¾"KëË
Žñ9…ßÕw²êãO'^øjŸ9ìt…rÒ…    Â¬aij+»Ò’¦~+!Kîè*ÆÛäÇRˆæM/ýGMÙ

The password is 123. Let me know if it works.
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#7
I can tell you without even running it that the same seed value will work on any computer.
Reply
#8
To be honest, it's probably pointless for me to try that example, as the forum probably lost some of the non-printable characters.

Are you sure Chaos, don't Linux use the Berkley rand(), whereas windows use MSVCRT rand() ?

I would probably hunt for a very simple random generator to use in the program, just to be sure.
EVEN MEN OF STEEL RUST.
[Image: chav.gif]
Reply
#9
Yeah, FB's random number generator is dependant on the libc rand(), which isn't guaranteed to be the same across platforms.
Reply
#10
Hmm... well if you guys know of a random number generator library for FB, let me know.
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)