Solved

Line breaks problem in text encryption

Posted on 2006-06-22
4
236 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 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now