?
Solved

Encriptation

Posted on 2003-03-09
29
Medium Priority
?
152 Views
Last Modified: 2010-05-01
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!
0
Comment
Question by:ogog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 12
29 Comments
 
LVL 1

Expert Comment

by:hj2k3
ID: 8097725
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8097747
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
 
LVL 1

Author Comment

by:ogog
ID: 8097839
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:hj2k3
ID: 8097902
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
 
LVL 1

Author Comment

by:ogog
ID: 8097923
Well it can't. I have version 6 and If file is too long it overflows.
0
 
LVL 1

Expert Comment

by:hj2k3
ID: 8097973
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
 
LVL 1

Author Comment

by:ogog
ID: 8098070
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8098127
which line excactly do you get overflow error on?
0
 
LVL 1

Author Comment

by:ogog
ID: 8098171
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8098195
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
 
LVL 1

Author Comment

by:ogog
ID: 8098380
No, it doesn't work either. what about adding one to every ascii char but when loading not using a big string?
0
 
LVL 1

Expert Comment

by:hj2k3
ID: 8098531
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
 

Expert Comment

by:igfp
ID: 8098938
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8098946
ok, paste the code, i'll put it into VB and try and debug
0
 
LVL 1

Author Comment

by:ogog
ID: 8098958
   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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8099063
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8099065
oh, and i% to i&
0
 
LVL 1

Accepted Solution

by:
hj2k3 earned 100 total points
ID: 8099140
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
 
LVL 1

Author Comment

by:ogog
ID: 8099483
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8099507
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
 
LVL 1

Author Comment

by:ogog
ID: 8105948
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8105983
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
 
LVL 1

Author Comment

by:ogog
ID: 8106953
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
 
LVL 1

Author Comment

by:ogog
ID: 8109956
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8110901
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
 
LVL 1

Author Comment

by:ogog
ID: 8113525
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8116486
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
 
LVL 1

Expert Comment

by:hj2k3
ID: 8116498
sorry, one last thing, change CopyMemory to MoveMemory
0
 
LVL 1

Author Comment

by:ogog
ID: 8118543
yes it seems to works much faster, and works with 2G.B. files as well? I have no idea!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question