Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Line breaks problem in text encryption

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

636 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