Results 1 to 13 of 13

Thread: OverFlow Error, String Encryption

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Sydney, Australia
    Posts
    12

    Post OverFlow Error, String Encryption

    Hey guys,

    This is for my major Software Design and development task, i am creating a simple string encryption software. It basically works by allow the user to input text into a input text field and then it converts it using ROT encryption (i.e it converts each character to ascii then it adds a user selected number to that ascii and converts it back to character e.g user inputs "a" and uses 3 as the ROT number then then it will be encrypted into a "c") then prints the output into another text field. I've managed to finish the encryption part of the program but its very limited, for example with small amouts of text, up to 2 or 3 A4 pages it will work fine although it becomes non responsive untill it has finished, but with larger amouts of input e.g between 5-7 , if the user clicks on the form it appears the prgram has crashed, however if i wait, after a while it will finish and start working fine again ( Visual Basic also appears to crash, and depending on the amount of text it sometimes does ). That was one of its limitations, the bigger problem is that if i input 12 or more pages into the the input textbox and click encrypt then i recive a "overflow, error 6".

    Could anyone suggest any improvements i could make to fix my code,?


    thanks in advance



    This is the relevent coding from the Main encryption Form:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim strInputText As String ' the input strings
    4. Dim strOutputText As String 'output string
    5. Dim intNumLock As Integer ' number to be added to charcte
    6.  
    7. Public Sub Encrypt()
    8. Dim intCharcters As Integer 'number of charcters needed to be looped
    9.  
    10. For intCharcters = 0 To Len(txtInput.Text) - 1
    11.     txtInput.SelStart = intCharcters
    12.     txtOutput.SelStart = intCharcters
    13.     txtInput.SelLength = 1
    14.     txtOutput = txtOutput.Text + Cipher(txtInput.SelText)
    15. Next intCharcters
    16. End Sub

    This is the bit that does the actual encrypting/ converting of the characters

    VB Code:
    1. Public Function Cipher(strUserInput As String) As String
    2.  
    3. strUserInput = AscW(strUserInput) 'Converts the letter to a Ascii number
    4. strUserInput = strUserInput + 3 'Adds three to the Ascii number
    5. strUserInput = ChrW$(strUserInput) 'Converts the Ascii number back to a letter
    6.  
    7. Cipher = strUserInput 'Adds the Crypted letters to the textbox
    8. End Function

    of course if you can't fix the problem any other suggestions to improving my code would also be welcome

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: OverFlow Error, String Encryption

    Firs of all Change all the Integers to Long. Longs are more efficient in For Loops.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: OverFlow Error, String Encryption

    You'll get errors when the ASCII code reaches anything over 255 which is an invalid ASCII number.

    Also, here's an easier way to add to the ASCII code.
    VB Code:
    1. For i = 1 to Len(StringToBeEncrypted)
    2.    NewString = NewString & Chr(Asc(Mid(StringToBeEncrypted, i, 1)) + 3) & NewString
    3. Next

    Oh, Also your loop needs to start at 1, not 0. Zero is only a valid start point for arrays, not strings.

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: OverFlow Error, String Encryption

    Yes, the intCharcters variable should be a long, otherwise it is limited to 32767 characters.
    For the speed issue, I would advise you to not access control properties in a loop.
    Accessing control properties is slow, so it is much faster to put the text in a variable first, then encrypt, and after that put the encrypted text in the textbox.
    Your cipher function uses implicit conversion to and from string unnecessary.
    This takes time as well.
    Try this:

    VB Code:
    1. Public Sub Encrypt()
    2. Dim strInputText As String ' the input strings
    3. Dim strOutputText As String 'output string
    4. Dim lngCharacters As Long 'number of charcters needed to be looped
    5.     strInputText = txtInput.Text
    6.     For lngCharacters = 1 To Len(strInputText)
    7.         strOutputText = strOutputText & Cipher(Mid(strInputText, lngCharacters, 1))
    8.     Next
    9.     txtOutput.Text = strOutputText
    10. End Sub
    11.  
    12. Public Function Cipher(strUserInput As String) As String
    13.     Cipher = ChrW$(AscW(strUserInput) + 3)
    14. End Function
    Frans

  5. #5
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: OverFlow Error, String Encryption

    Yeah, that's true about the Long Variable and not Integer. It all depends on how long your strings may be. I only ever work with excrypting passwords for storage.

    A question though, does your project not require a stronger encryption method?

    Just curious.

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Sydney, Australia
    Posts
    12

    Re: OverFlow Error, String Encryption

    Thanks guys, (that was quick!)

    just another question before i go to fix up my code:

    I will need to deal with the unresponsiveness of the software from a user perspective as well, i mean, i will need to show them a progress dialog, i am wondering, if my software became unresponsive, is there someway i can make that dialog stay responsive?,e.g have a higher priority or run it as a seperate program, in order to keep the user busy, untill the code starts responding ( which it usally does when it is finished)?





    @ Spajeoly

    i don't really don't need a strong encryption as long as i can code effeciently/well, we are being examined not on how good our software is (we all got to create our own developer scanario e.g client, problem etc, ), rather how we work through the development process so documentation and theory is mostly what we get marks for, although we do get some marks on the logic, syntax and effeciency of our coding. Otherwise the class would stand no chance against someone who has been coding for a while.

    Although, yes i am working on something , i'll just give a small illusion of strong encryption by allow the user to choose a level of encryption (weak to strong), and each level, strongest being 5, would corrospond to the number of times that the whole input is encrypted, so instead of only adding the userinput to the charcter once, it would do it 5 times for the strongest encryption, which, will of couse take longer the stronger the encryption the user asks for.
    Last edited by Spider786; Apr 18th, 2006 at 06:27 AM.

  7. #7
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,794

    Re: OverFlow Error, String Encryption

    Take a look at this thread, it might help:
    http://www.vbforums.com/showthread.php?t=396715

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,692

    Re: OverFlow Error, String Encryption

    To make your program more user-responsive (and slower), add a DoEvents on the line just before the Next in the loop. (You can't make it more responsive without slowing it down.)

  9. #9
    Frenzied Member Spajeoly's Avatar
    Join Date
    Mar 2003
    Location
    Utah
    Posts
    1,068

    Re: OverFlow Error, String Encryption

    Quote Originally Posted by Spider786
    Thanks guys, (that was quick!)
    @ Spajeoly

    i don't really don't need a strong encryption as long as i can code effeciently/well, we are being examined not on how good our software is (we all got to create our own developer scanario e.g client, problem etc, ), rather how we work through the development process so documentation and theory is mostly what we get marks for, although we do get some marks on the logic, syntax and effeciency of our coding. Otherwise the class would stand no chance against someone who has been coding for a while.

    Although, yes i am working on something , i'll just give a small illusion of strong encryption by allow the user to choose a level of encryption (weak to strong), and each level, strongest being 5, would corrospond to the number of times that the whole input is encrypted, so instead of only adding the userinput to the charcter once, it would do it 5 times for the strongest encryption, which, will of couse take longer the stronger the encryption the user asks for.
    Understood. I hope everything works out well for ya.

    Ironically this thread came right as I had finished writing an encryption for saved passwords in a program at work.

  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Sydney, Australia
    Posts
    12

    Re: OverFlow Error, String Encryption

    Hi, i am still working on the responsiveness issue,

    Could you please explain "doEvents" to me, i am still learning vb, but im unsure of what doevents is, and how it works.


    thanks, evrything else so far has been coming along great

  11. #11
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: OverFlow Error, String Encryption

    DoEvents will tell the operating system that other tasks can be performed. Your code will resume as soon as the operating system decides it is your turn to get processor time again.

    This means that your code will take longer to run, but that other programs, and other events in your program, are not blocked by your code.

    If I were you, i would add a progress bar. This way the user would know your program doesn't hang, and can estimate how long it will take before it is finished.
    Frans

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Sydney, Australia
    Posts
    12

    Re: OverFlow Error, String Encryption

    thanks for your help so far, its been great,


    I have implemented all the modifications that have been suggested and they have helped me greatly, i just encrypted about 46 pages of my notes in under a minute With no error or issues,

    My teacher also suggested that i use a progress bar, but thanks for your suggestion anyway

    How ever, i have currently started working on the decryption part of my code, i was wondering if it would be possible to simply use my previous code but simply chnage the adding of a integer to the ASCII of the character to a Removing of the character(e.g change + 3 to -3), without changing any other vital code?. (it would be great if i could do this, as i could makr the code use the same fucntions, for the decryption and encryption).

    If you need to see my code, i will paste it here later, but i am in a rush.

    thanks for everything guys

  13. #13

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Location
    Sydney, Australia
    Posts
    12

    Help!

    Hey guys, Another issue with my software.

    When ever i encrypt the text, the spaces are also encrypted as part of the text, however when i decrypt, all the spaces are removed and are instead replaced by a small rectangle box. I am not sure what i am doing wrong, below is the code for my decrypt fucntion, it is the same as the encrypt fucntion only the variables have been changed and instead of adding a number to the ascii it deducts a number.

    VB Code:
    1. Public Function strDecipher(strUserInput As String) As String
    2. strInputText = frmMain.txtInput.Text 'defines the input source
    3.  
    4. strDecipher = ChrW$(AscW(strUserInput) - 3) ' converts each character to ascii, - the number originally added to encrypt then converts back to character
    5.  
    6. End Function
    7.  
    8. Public Function Decrypt()
    9.  
    10. Dim strInputText As String ' the input strings
    11. Dim strOutputText As String 'output string
    12. Dim lngCharacters As Long 'number of characters needed to be looped
    13.  
    14. strInputText = frmMain.txtInput.Text
    15.  
    16. frmMain.cmdDecrypt.Enabled = False 'disables the button, so i know its wokring
    17.  
    18.     For lngCharacters = 1 To Len(strInputText)
    19.         DoEvents ' makes the program more responsive by allow the System to tend to other tasks then return to the loop, downside means its also more slower
    20.         strOutputText = strOutputText & strDecipher(Mid$(strInputText, lngCharacters, 1)) 'loops each character through the decryption process above
    21.     Next
    22.  
    23.     frmMain.txtOutput.Text = strOutputText ' places the output into the textbox
    24.  
    25. frmMain.cmdDecrypt.Enabled = True 'enables the button, so i know the process has been completed
    26.  
    27. End Function

    Any help is better then no help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width