6.17.2011

Textual Cue For TextBox Control

In this article, every thing is based on this : EM_SETCUEBANNER

this is a Windows Message that we can send to an edit control such as a textbox, so we can have something like this :
 
this tip "Enter your full name here" on the textbox is usally useful to the user and avoid some confusion.
All we need to do, is to send this message to the handle of the control, via SendMessage Api.

Declare Unicode Function SendMessageW Lib "User32.dll" (ByVal Hwnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Boolean, ByVal lParam As String) As Boolean
    Const EM_SETCUEBANNER = 5377
    Function SetCueText(ByRef Textbox1 As TextBox, ByVal CueText As String, Optional ByVal ShowOnFocus As Boolean = False) As Boolean
        Return SendMessageW(Textbox1.Handle, EM_SETCUEBANNER, ShowOnFocus, CueText)
    End Function

Usage :
Call SetCueText wih these parameters: :
Textbox1 : the textbox control that you want to add a cue text to it. (Textbox)
CueText : The Tip that will show on the textbox (String)
ShowOnFocus : Set to true if you want the cue text visible even if the textbox has the focus (boolean)
Return value : returns true if it succeeds, otherwise it returns false.
Even Better, we can extend the textbox control, and add a property Called "CueText", just like this :

Imports System.Windows.Forms

Public Class mTextBox
    Inherits TextBox
    Declare Unicode Function SendMessageW Lib "User32.dll" (ByVal Hwnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Boolean, ByVal lParam As String) As Boolean
    Const EM_SETCUEBANNER = 5377
    Private _CueText As String
    Private _CueFocus As Boolean
    Public Property CueText() As String
        Get
            Return _CueText
        End Get
        Set(ByVal value As String)
            _CueText = value
            If Me.Handle <> IntPtr.Zero Then SendMessageW(Me.Handle, EM_SETCUEBANNER, New IntPtr(CInt(_CueFocus)), _CueText)
        End Set
    End Property
    Property CueTextFocus() As Boolean
        Get
            Return _CueFocus
        End Get
        Set(ByVal value As Boolean)
            _CueFocus = value
            CueText = CueText
        End Set
    End Property
End Class

What we did, we created a control that inherits from the usual textbox, and added two properties that allow us to get and set the cue text, and also whether to show on focus or not.

OpenIconDialog

If you noticed in your tool box, you've got your OpenFileDialog, SaveFileDialog, ColorDialog, ect...
But There Is No OpenIconDialog.
What we are going to do, is to use the PickIconDlg Api to show a dialog to choose an embedded icon in a file,it will return the index of the selected icon in the file, and then we use the ExtractIconEx Api to load the icon of that index to memory, get its handle and return a variable of type System.Drawing.Icon.
So basically, we made an OpenIconDialog.

Declare Unicode Function PickIconDlg Lib "shell32.dll" (ByVal hwnd As IntPtr, ByVal pszIconPath As StringBuilder, ByVal cchIconPath As Integer, ByRef piIconIndex As Integer) As Integer
    <DllImport("shell32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function ExtractIconEx(ByVal stExeFileName As String, ByVal nIconIndex As Integer, ByVal phiconLarge As IntPtr(), ByVal phiconSmall As IntPtr(), ByVal nIcons As Integer) As Integer
    End Function
    Function OpenIconDialog(Optional ByVal IcoPath As String = "Shell32.dll") As Icon
        Dim buff As New StringBuilder(IcoPath, 500)
        Dim ind As Integer
        If PickIconDlg(Nothing, buff, buff.Capacity, ind) = 1 Then
            Dim bitm As New Bitmap(1, 1)
            Dim hIcon() As IntPtr = New IntPtr(0) {}
            If (ExtractIconEx(buff.ToString, ind, hIcon, Nothing, 1) = 1) AndAlso hIcon(0) <> IntPtr.Zero Then
                Return Icon.FromHandle(hIcon(0))
            End If
        End If
        Return Nothing
    End Function
It would look something like this :

Msdn recommends that we call DestroyIcon api after finishing of using the icon, but we don't need to add that api declaration to our code since the type System.Drawing.Icon has a method called Dispose, that will eventually cleanup and call the DestroyIcon api.

6.16.2011

Custom Encryption : Polymorphic Stairs

An Encryption is a process to make a plaintext unreadable to anyone unless they have the algorithm and the key to transform the data to it's orginal state. It is usally -in general- used to PROTECT data from being read/understood/stolen.
check out more on wiki : http://en.wikipedia.org/wiki/Encryption

A stronger encryption method is the one with less chance to reverse data to original plaintext without knowing the password, which is called cracking.

today we are going to make our own "Custom Encryption", even if dotnet provides us with a fine list of encryption routines, check them out on msdn : http://msdn.microsoft.com/en-us/library/system.security.cryptography.aspx

What is Polymorphic?
Polymorphic stands for "multiple forms". in our case, the output of the encryption is diffrent everytime we use it, which gives some confusion to the one who tries to "crack" it without the password.

here is an exemple, which i called "Polymorphic Stairs"

Imports System.Text
Public Class PolyMorphicStairs
    Overloads Shared Function PolyCrypt(ByVal Data As String, ByVal Key As String, Optional ByVal ExtraRounds As UInteger = 0) As String
        Dim buff() As Byte = PolyCrypt(Encoding.Default.GetBytes(Data), Encoding.Default.GetBytes(Key), ExtraRounds)
        PolyCrypt = Encoding.Default.GetString(buff)
        Erase buff
    End Function
    Overloads Shared Function PolyDeCrypt(ByVal Data As String, ByVal Key As String, Optional ByVal ExtraRounds As UInteger = 0) As String
        Dim buff() As Byte = PolyDeCrypt(Encoding.Default.GetBytes(Data), Encoding.Default.GetBytes(Key), ExtraRounds)
        PolyDeCrypt = Encoding.Default.GetString(buff)
        Erase buff
    End Function
    Overloads Shared Function PolyCrypt(ByRef Data() As Byte, ByVal Key() As Byte, Optional ByVal ExtraRounds As UInteger = 0) As Byte()
        Array.Resize(Data, Data.Length + 1)
        Data(Data.Length - 1) = Convert.ToByte(New Random().Next(1, 255))
        For i = (Data.Length - 1) * (ExtraRounds + 1) To 0 Step -1
            Data(i Mod Data.Length) = CByte(CInt((Data(i Mod Data.Length)) + CInt(Data((i + 1) Mod Data.Length))) Mod 256) Xor Key(i Mod Key.Length)
        Next
        Return Data
    End Function
    Overloads Shared Function PolyDeCrypt(ByRef Data() As Byte, ByVal Key() As Byte, Optional ByVal ExtraRounds As UInteger = 0) As Byte()
        For i = 0 To (Data.Length - 1) * (ExtraRounds + 1)
            Data(i Mod Data.Length) = CByte((CInt(Data(i Mod Data.Length) Xor Key(i Mod Key.Length)) - CInt(Data((i + 1) Mod Data.Length)) + 256) Mod 256)
        Next
        Array.Resize(Data, Data.Length - 1)
        Return Data
    End Function
End Class

Lets take a look at this Class :
First, we have two functions, one to encrypt data and the other to decrypt data.
there are two parameters, one is the data, the other is the key, and the return value is the data after the transformation. they are both overloaded to accept strings or byte arrays as parameters. there is also an optionnal parameter called ExtraRounds, which represents how many addtional times you want to encrypt your data. for exemple, you want to encrypt it twise, set ExtraRounds to 1 when calling the function.

Here is how it works :
We first generate a random byte, and add it the end of the bytearray of the plaintext. after that, we add the value of that byte to the previous one, and using the bitwise operator XOR with a byte from the key. we just loop and do the same with every byte in the array.

The Output :
With the random byte sticked to the end of the array, its value is gonna be added to the previous byte, which also be added to the one before it... in other words the random byte will affect the hole array : the output will be completely randomized each time.
Here is an exemple where the plain-text and key are pretty basic :
As you can see, outputs are random and scrambled each time.
But the more complex the key is, the better.

Decryption :
In the decryption routine, we do the exact same thing, but backwards. First, we Xor the byte, and then remove the value next to it. after the loop finishes, we just remove the random value in the end of the buffer and we get our plaintext back to normal.

Weakness :
Xor operator is a weak Cipher by it self, the password can be guessed by analysing the output. the addition of a value to another and the use of a random value covers xor weakness, but there is alot of cracking routines, such as bruteforcing for exemple.

Why the random value was added to the end of the array?
Good Quesion. Why? this would work also if we add it at the start of the array. it is simply added to the end for a faster performance, because i don't really want copy the full array to a new one, or push all values to give space for the random one. as simple as that.