Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Line breaks problem in text encryption

Posted on 2006-06-22
4
Medium Priority
?
307 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
  • 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 1400 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month20 days, 18 hours left to enroll

810 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