Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Since I am fairly new to this forum I could not post this to the Code Library.

I posted it on Extreme VB talk and thought someone here may find it of use also.

 

This is a simple class to encode and decode lines of text. I find it very usefull to keep prying eyes from data (as long as true security is not an issue) such as file locations and specific program data that I save to data files. While the Encoding is simple and can be broken there are steps that make it harder. Such as using a different key for each encoded line. This helps hide the method used and adds complexity to the process of trying to break it.

 

However, I want to make it plain that this does not provide secure encryption.

This is more of a method to obscure data for privacy.

Public NotInheritable Class CypherText
   ' Prevent the compiler from creating a default public constructor.
   Private Sub New()

   End Sub
   Public Shared Function EnCode(ByVal ToEnCode As String, ByVal KeyCode As String) As String
       Dim MyReturn As String = ""
       'Convert string to Character array
       Dim MyArray() As Char = ToEnCode.ToCharArray
       Dim Result As String
       'Loop through Array and alter the character
       For MyLoop As Integer = 0 To MyArray.Length - 1
           Result = Chr(Asc(MyArray(MyLoop)) Xor Asc(KeyCode))
           'Do not alter if it results in a Carrage Return or Line Feed
           If Result = Chr(13) Or Result = Chr(10) Then
               MyReturn = MyReturn & Chr(Asc(MyArray(MyLoop)))
           Else
               MyReturn = MyReturn & Result
           End If
       Next
       Return KeyCode & MyReturn
   End Function
   Public Shared Function DeCode(ByVal ToDeCode As String) As String
       Dim MyReturn As String = ""
       Dim Result As String
       'First Character is the Encoding Key
       Dim KeyCode As String = ToDeCode.Substring(0, 1)
       'Convert string to Character Array
       Dim MyArray() As Char = ToDeCode.ToCharArray
       'Loop through all but first character and convert back to original character
       For MyLoop As Integer = 1 To MyArray.Length - 1
           Result = Chr(Asc(MyArray(MyLoop)) Xor Asc(KeyCode))
           'If result is a Carrage Return oR Line Feed it was not encrypted
           If Result = Chr(13) Or Result = Chr(10) Then
               MyReturn = MyReturn & Chr(Asc(MyArray(MyLoop)))
           Else
               MyReturn = MyReturn & Result
           End If
       Next
       Return MyReturn
   End Function
End Class

Now to call the methods I use the following

Const KeyVal As String = "abcdefghijklmnopqrstuvwxyz0123456789[]?/><!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Public RndKey As New Random


Dim EncodedTextVariable As String
EncodedTextVariable = CypherText.EnCode("Text to encode Here", KeyVal.Substring(RndKey.Next(0, 77), 1)


Dim OriginalText As String
OriginalText = CypherText.DeCode(EncodedTextVar)

 

Hope this will be of use :D

Posted
Let me start by saying I am not trying to be rude, but why create something like this when secure encryption is readily accessible in the runtime and can be implemented with as little or less code?
Life is a comedy to those who think; a tragedy to those who feel.
Posted
Let me start by saying I am not trying to be rude, but why create something like this when secure encryption is readily accessible in the runtime and can be implemented with as little or less code?
Well I'll tell you why I did this. First, this is something I developed when I was using VB6 which did not have the secure encryption available without using API's and getting fairly involved.

 

Second, This is an alternative that can be included within the code that will not be dependant on any external library. While this does not have any real impact when dealing with the .NET framework. it is an alternative if you are wanting some type of stand alone program.

 

Third, I always like to have alternatives. I have had issues when trying to use System.Security.Cryptography (But most of that I am sure is my inexperience with .NET)

 

Fourth, Sometimes it's just looking for a different way of doing things.

 

My reason was I was looking for a method that I needed to function easily on several platforms with several languages (The code is easy to develop in any language) and in some cases on systems where .NET was not going to be an alternative. This was used in programs using Foxpro, VB6, C++ (Unmanaged), and VB 2005.

Posted
The title of the post says that it is only intended to obscure data -- The .NET Cryptography Library may be slight overkill in this situation.

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Posted
Thats fair, like I said I was not trying to be condescending or rude, just curious.
Life is a comedy to those who think; a tragedy to those who feel.
Posted

This is a similar method, however you use the same method for "encrypting" and "decrypting"

 

           public static String Crypt(String sInput)
           {
               // your integer key here, lower values will leave "readable" text, higher will be unprintable..generally speaking
               int iKey = 64;
               System.Text.StringBuilder sbInput = new StringBuilder(sInput, sInput.Length);
               System.Text.StringBuilder sResult = new StringBuilder(sInput.Length);
               for (int i = 0; i < sbInput.Length; i++)
               {
                   sResult.Append((Char)(sbInput[i] ^ iKey));
               }
               return sResult.ToString();
           }

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

  • 1 month later...
Posted

Nate, Your method is pretty much what mine is the reason for seperate encode and decode functions is I developed my to encode lines of text and as such I wanted to make it where if several lines of text had similar data the obscured text would not reflect it. By using a random character from the 77 character Keyval constant results in each line having different encodings.

 

There are a lot of ways to do something like this this was just an exercise for me in getting a function to work the same in .NET as I had working in VB6 so that files encoded in VB6 apps could be read in .NET. I also find doing things like this often help me learn new aspects of the language.

 

Prophet,

 

No offense taken (I wondered why I needed this when I first did it)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...