Line breaks problem in text encryption

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.


jorgemgonzalezAsked:
Who is Participating?
 
omegaomegaDeveloperCommented:
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
 
omegaomegaDeveloperCommented:
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
 
jorgemgonzalezAuthor Commented:
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
 
jorgemgonzalezAuthor Commented:
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
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.

All Courses

From novice to tech pro — start learning today.