Encriptation

I'm trying to encrypt a RichTextBox and I'm using this to save:

    charsinfile% = Len(Rich.TextRTF)
    CommonDialog1.ShowSave
    Open CommonDialog1.FileName For Output As 1
    For i% = 1 To charsinfile
        letter$ = Mid(Rich.TextRTF, i%, 1)
        Print #1, Asc(letter$) Xor 50;
    Next i%
    Close #1


This works good no matter how big the text box is.
and I'm using this to open:

    CommonDialog1.ShowOpen
    Open CommonDialog1.FileName For Input As 1
    Do Until EOF(1)
        Input #1, Number&        
        decrypted$ = decrypted$ & Chr(Number& Xor 50)
    Loop
    Rich.SelRTF = decrypted$
    Close #1

This works good if the string is short. Otherwise I get abort!
I must decrypt the file char by char so that I don't deppend on the string but I can't. I've tried this but didn't work:


    CommonDialog1.ShowOpen
    Open CommonDialog1.FileName For Input As 1
    Do Until EOF(1)
        Input #1, Number&
        decrypted$ = Chr(Number& Xor 50)
        Rich.SelRTF = decrypted$
    Loop
    Close #1

Someone help me please!
LVL 1
ogogAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hj2k3Commented:
i think the problem is when you are saving; the line

 Print #1, Asc(letter$) Xor 50;

seems to be saving a number, in string format
what does the file look like once you have saved it?
0
hj2k3Commented:
i think your code should be

Print #1, Chr(Asc(letter$) Xor 50);

to save as a character, then to open you use a temporary character variable, declare it at the start

Dim tStr as String * 1

then to open,

Input #1, tStr     'takes one character
decrypted$ = Chr(Asc(tStr) Xor 50)

hope this helps
0
ogogAuthor Commented:
Yes I save it as a number and then I'm supposed to take each number and decrypt it. Your code does not work because too many chars look the same with a Xor 50! so when decrypting chars have nothing to do with the original.
I've also tried adding 1 to every asCii code but once again I'm using a string and I can't because I'll have very big text files. Here's what I used to encrypt:

    encrypt$ = ""
    CharsInFile% = Len(rtfRefs.TextRTF)
    For i% = 1 To CharsInFile
        letter$ = Mid(rtfRefs.TextRTF, i%, 1)
        encrypt$ = encrypt$ & Chr(Asc(letter$) + 1)
    Next i%
    Open CommonDialog.FileName For Output As 1#
    Print #1, encrypt$
    Close #1

This works with a word or two, but not with 200000 chars! :p
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

hj2k3Commented:
why shouldn't string work? i think it depends on the version of visual basic you are using. i think up to a certain version (3/4/5???) strings can only be 64KB (65536 bytes) long, but in version 6 or over they can be up to 2GB!
0
ogogAuthor Commented:
Well it can't. I have version 6 and If file is too long it overflows.
0
hj2k3Commented:
i think when you write the file, all the numbers are after each other without a delimiter, so for 'hello' you end up with 104101108108111, which VB will interpret as one number. what you want is 104,101,108,108,111.
for this you should replace
Print #1, Asc(letter$) Xor 50;
with
Write #1, Asc(letter$) Xor 50;
when you write the file.
could you paste the actual contents of the encrypted file to see if this is the case?
0
ogogAuthor Commented:
7 38 25 etc... they get separated by a space. It's very wierd I still don't know why it does not work. it works fine with a text box but not with a rich one.
0
hj2k3Commented:
which line excactly do you get overflow error on?
0
ogogAuthor Commented:
I can't say, I've changed the code again trying to get this right. But if I try to save a big string or just load a large amount of chars to a string I get overflow. Is there anyway of me getting the decoded chars one by one into the Rich text box?
0
hj2k3Commented:
you could try this:

CommonDialog1.ShowOpen
   Open CommonDialog1.FileName For Input As 1
   Rich.TextRTF=""
   Do Until EOF(1)
       Input #1, Number&
       Rich.TextRTF = Rich.TextRTF & Chr(Number& Xor 50)
   Loop
   Close #1

?
0
ogogAuthor Commented:
No, it doesn't work either. what about adding one to every ascii char but when loading not using a big string?
0
hj2k3Commented:
CommonDialog1.ShowOpen
  Open CommonDialog1.FileName For Input As 1
  Rich.TextRTF=""
  Dim tStr as String * 1
  Do Until EOF(1)
      Input #1, Number&
      tStr = Chr(Number& Xor 50)
      Rich.TextRTF = Rich.TextRTF & tStr
  Loop
  Close #1

?
which line is overflow?
0
igfpCommented:
no, now it doesn't even shows anything on the rtf box. it remains empty. Let's do like this, I'll paste the code with my Add one char and the open as well and let's see what we can do from there, what do you say? I really apreciate all the trouble you're going through.
0
hj2k3Commented:
ok, paste the code, i'll put it into VB and try and debug
0
ogogAuthor Commented:
   encrypt$ = ""
    CharsInFile% = Len(rtfRefs.TextRTF)
    For i% = 1 To CharsInFile
        letter$ = Mid(rtfRefs.TextRTF, i%, 1)
        encrypt$ = encrypt$ & Chr(Asc(letter$) + 1)
    Next i%
    Open CommonDialog.FileName For Output As 1#
    Print #1, encrypt$
    Close #1

-----------------------------

    Decrypt$ = ""
    CharsInFile = Len(Alltext$)
    For i% = 1 To CharsInFile
        letter$ = Mid(Alltext$, i%, 1)
        Decrypt$ = Decrypt$ & Chr(Asc(letter) - 1)
    Next i%
    rtfRefs.TextRTF = Decrypt$
    Close #1

-----------------------------
this works fine but not with something like a Curriculum Vitae! If my work is very big (like hundreds of lines) it just blows!
0
hj2k3Commented:
change charsinfile% to charsinfile&
this changes it from integer to long. i can't believe i didn't spot this straight away! it did an overflow, but when i changed it to charsinfile& it works, but it's been doing a 190KB file for about a minute now and it still hasn't finished. (i have an athlon xp2200 512mb ram on win98)
0
hj2k3Commented:
oh, and i% to i&
0
hj2k3Commented:
ok, i've got the encryption working fast, using this:

Dim tStr As String * 1
Dim txt As String
txt = rtfRefs.TextRTF
  encrypt$ = ""
   CharsInFile& = Len(txt)
   Open "c:\tst.txt" For Binary As #1
   For i& = 1 To CharsInFile&
       letter$ = Mid(txt, i&, 1)
       tStr = Chr(Asc(letter$) + 1)
       Put #1, , tStr
   Next i&
   Close #1


but decryption is still painfully slow, due to the line
Decrypt$ = Decrypt$ & Chr(Asc(letter) - 1)
because it is copying itself completely over and over, getting bigger each time, which is extremely inefficient
working on a solution...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ogogAuthor Commented:
I'll try that! Still it will handle a very big file? How big? You know what's wierd, maybe there's a way of not reading the string over and over again to itself. I'll try the code and I'll let you know. It's pretty late in here so tomorrow I'll check that! Thank you!
0
hj2k3Commented:
it should handle file up to around 2.1GB, but i'm working on using the RtlMoveMemory API call to efficiently copy memory over...
0
ogogAuthor Commented:
the encriptation works perfectly and fast, still it takes hours to load. just for all the work you had you deserve more then these points! If you can find a way of speeding the opening i would apreciate that, I'm trying some myself but i didn't got anything working yet. Thanks
0
hj2k3Commented:
well, i had an idea of creating an array of characters, decoding the file one character at a time. this stage is quite fast, the array is filled nicely. but i looked into then using an RtlMoveMemory API call to do a nice efficient conversion of the character array into a proper string to be inserted into the rich text box, but i can't get it to work and keep crashing my computer. i might post one or two more times if i come up with anything. cheers for the points, anyway. 'preciate it!
0
ogogAuthor Commented:
i know, every time i try to move it char by char usilng rtfREFS.SelRTF it crashed VB. DOn't know what to do just yet!!
0
ogogAuthor Commented:
Hey hj2k3 what do you think about this? I was able to read a 1 MB file in about 50 Seconds (Athlon XP 2200)

We couldn't get char by char into the rtf box and I think because the rtf tags have lots of "{" etc. Even when i tried to filter these chars to print a line it crashed vb. So I build another box a simple text box (called it "rtfREFS2")and used the following:

CommonDialog1.ShowOpen
    NewLine$ = Chr$(13) + Chr$(10)
    Open CommonDialog1.FileName For Input As 1#
    Do Until EOF(1)
        Line Input #1, Lineoftext$
        Alltext$ = Alltext$ & Lineoftext
    Loop

   CharsInFile& = Len(Alltext$)
   pb.Max = CharsInFile&
   pb.Visible = True
   pb.Value = 0
   rtfREFS2.Text = ""
   For i& = 1 To CharsInFile&
       letter$ = Mid(Alltext$, i&, 1)
       letter$ = Chr(Asc(letter) - 1)
       rtfREFS2.SelText = letter$
   Next i&
   Close #1
   rtfREFS.TextRTF = rtfREFS2.Text

Worked fine with a big file and not as slow as reading the same string over and over again!
0
hj2k3Commented:
still, 50 seconds for 1 meg is still only about 20KB/sec, which is pretty dismal considering the memory bandwidth with an Athlon is more like 2GB/sec! i think loading and inserting the file line by line into the text box is a good idea, you will probably get at least a 10x performance boost over silly string copying. i might get back to you on this one, i want to make it work because there is a principle of efficiency at stake. if it works well enough, use it for now but keep subscribed to this thread...
0
ogogAuthor Commented:
well after taking away the progress bar it took only 10 seconds. I know it's not too fast but it works fine. If there was another effective and fast way it would be great. Thanks
0
hj2k3Commented:
i have found a way where it took <1 second with about 1MB.
put the following line at the top if all the code:

Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As Any, ByRef pSrc As Any, ByVal ByteLen As Long)

and here is the new code:
-----------------------------------------------------------
Dim AllText As String
Dim Letter As String * 1
Dim chars() As Byte
Dim Decrypt As String
Dim nChars As Long
Dim i As Long

Open "c:\tst.txt" For Input As #1
Input #1, AllText
Close #1

nChars = Len(AllText)
Decrypt = Space(nChars) 'need to pre-allocate space or it will crash
ReDim chars(nChars)
For i = 1 To nChars
    Letter = Mid(AllText, i, 1)
    chars(i) = Asc(Letter) - 1
Next i

CopyMemory Decrypt, chars(1), nChars

rt.TextRTF = Decrypt
-----------------------------------------------------------

p.s you should get in the habit of declaring all variables at the start of functions, it makes code easier to read and debug, and can help make things faster.

0
hj2k3Commented:
sorry, one last thing, change CopyMemory to MoveMemory
0
ogogAuthor Commented:
yes it seems to works much faster, and works with 2G.B. files as well? I have no idea!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.