Solved

Line breaks problem in text encryption

Posted on 2006-06-22
4
282 Views
Last Modified: 2008-02-01
I'm working on a little plain text encryption app, the encryption and decryption work fine, except for the fact that the line breaks or return are ignored. IE:

This is my piece of text
and this is my other piece

After encryption this would read like this:

This is my piece of textand this is my other piece.

This is the method I have so far:

Private Const MIN_ASC As Short = 32 ' Space.
    Private Const MAX_ASC As Short = 126 ' ~.
    Private Const NUM_ASC As Integer = MAX_ASC - MIN_ASC + 1



Private Function Password(ByVal password As String) As Integer
        Dim value As Integer
        Dim ch As Integer
        Dim shift1 As Integer
        Dim shift2 As Integer
        Dim i As Short
        Dim str_len As Short

        shift1 = 3
        shift2 = 17
        value = 0

        str_len = Len(password)

        For i = 1 To str_len
            ' Add the next letter.

            ch = Asc(Mid(password, i, 1))
            value = value Xor (ch * 2 ^ shift1)
            value = value Xor (ch * 2 ^ shift2)

            ' Change the shift offsets.

            shift1 = (shift1 + 7) Mod 19
            shift2 = (shift2 + 13) Mod 23
        Next i

        Password = value
    End Function



Private Sub Encrypt(ByVal password As String, ByVal from_text As String, ByRef to_text As String)
        Dim str_len As Short
        Dim i As Short
        Dim ch As Short

        ' Initialize random number generator.

        Rnd(-1)
        Randomize(Password(password))

        ' Encrypt string.

        str_len = Len(from_text)

        For i = 1 To str_len
            ch = Asc(Mid(from_text, i, 1))

            ' Ignore non-printing characters.

            If (ch >= MIN_ASC And ch <= MAX_ASC) Then
                ch = ch + Int(NUM_ASC * Rnd())
                If ch > MAX_ASC Then
                    ch = ch - NUM_ASC
                End If
                to_text = to_text & Chr(ch)
            End If
        Next i

        My.Computer.Clipboard.Clear()
        My.Computer.Clipboard.SetText((to_text))
    End Sub



Private Sub Decrypt(ByVal password As String, ByVal from_text As String, ByRef to_text As String)
        Dim str_len As Short
        Dim i As Short
        Dim ch As Short

        ' Initialize random number generator.

        Rnd(-1)
        Randomize(Password(password))

        ' Decrypt string.

        str_len = Len(from_text)
        For i = 1 To str_len
            ch = Asc(Mid(from_text, i, 1))

            ' Ignore non-printing characters.

            If ch >= MIN_ASC And ch <= MAX_ASC Then
                ch = ch - Int(NUM_ASC * Rnd())
                If ch < MIN_ASC Then
                    ch = ch + NUM_ASC
                End If
                to_text = to_text & Chr(ch)
            End If
        Next i
    End Sub


How can I make the encrypt/decrypt method to recognize and keep line breaks? (return, enter, you name it)

Please help.


0
Comment
Question by:jorgemgonzalez
[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
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:omegaomega
ID: 16967183
Hello, jorgemgonzalez,

I'm not sure exactly what you are trying to do here, but the problem is probably that you are ignoring the line-feed carriage-return characters.  I can't see where you have defined MIN_ASC but if this value is > 13 then line-feed and carriage-return characters will be lost.  Why do you ignore some characters?

You could either choose to encrypt all characters (just remove the "If") or add those that you do not encrypt to the string in an "Else" block.

Cheers,
Randy
0
 

Author Comment

by:jorgemgonzalez
ID: 16967260
Thanks a lot for the fast reply Randy. I'm new to all this, (learning) maybe that explains why I must have missed some things. Could you please give me an example of implementing the options you just kindly stated above?

Thanks in advance.

Cheers,
Jorge
0
 
LVL 12

Accepted Solution

by:
omegaomega earned 350 total points
ID: 16967410
Hello, jorgemgonzalez,

For example, you could try changing the line,

    Private Const MIN_ASC As Short = 32 ' Space.
 
to

    Private Const MIN_ASC As Short = 0  ' Nul.

I haven't tried to analyze the affect this may have on the encryption algoritm that you're using.

Since you're "new to this" I can offer these suggestions:  

    Always use "Option Explicit"
    Always use "Option Strict"

These will highlight several other problems and possible problems in your code and force you to actually think about what you're doing and why you're doing it.  For example, what is the effect of using the same name for the Password function and its argument?  Why are you changing data types so frequently (long to/from integer to/from short)?   Have you thought about what will happen in the case of numeric overflows?

Good luck.

Cheers,
Randy
0
 

Author Comment

by:jorgemgonzalez
ID: 16967580
Thanks a lot Randy, it worked like a charm! I'll look into the possibility of numeric overflows.

Thank you for the other tips also, I'm taking notes of them. When I'm big I want to be like the experts here, but I'm only 48! =)

Cheers,
Jorge
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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