Friday, March 11, 2011

3DES Encryptor sample in VB.NET

Hi All,


Welcome to my blog again. I had a requirement in my project to encrypt some settings using a simple encryption. But I couldn't find a ready made solution, and from my college days, I always liked 3DES encryption. So, here is a "ready made" solution :

{Note: Of course, you'll have to change the IV (initialization vector) and the Key in the variable strCrypt }



Imports System.Security.Cryptography
Imports System.IO
Public Class clsDCrypto

    Private m_bKey(), m_bIV() As Byte

    Public Sub New()
        Dim strCrypt, strIV As String
        strCrypt = "q5AsCA64SeY+SpoJ24ERrg=="
        strIV = "VaOHJOI2J/c="
        m_bKey = Convert.FromBase64String(strCrypt)
        m_bIV = Convert.FromBase64String(strIV)
    End Sub

    Public Function DecryptString(ByVal strCipherText As String) As String
        Dim strTempDecrypt As String
        Dim memstream2 As New MemoryStream
        Dim iCryptoDecryptor As ICryptoTransform
        Dim tDESProviderd As New TripleDESCryptoServiceProvider
        Dim cryptdStream As CryptoStream
        Dim bCipher As Byte()

        bCipher = Convert.FromBase64String(strCipherText)
        tDESProviderd.KeySize = 128
        iCryptoDecryptor = tDESProviderd.CreateDecryptor(m_bKey, m_bIV)
        cryptdStream = New CryptoStream(memstream2, iCryptoDecryptor, CryptoStreamMode.Write)
        cryptdStream.Write(bCipher, 0, bCipher.Length)
        cryptdStream.FlushFinalBlock()
        memstream2.Position = 0

        Dim temp2(CType(memstream2.Length - 1, System.Int32)) As Byte

        memstream2.Read(temp2, 0, CType(temp2.Length, System.Int32))
        memstream2.Close()
        cryptdStream.Close()

        strTempDecrypt = System.Text.Encoding.UTF8.GetString(temp2)
        Return strTempDecrypt
    End Function

    Public Function EncryptString(ByVal strPlainText As String) As String

        Dim tDESProvider As New TripleDESCryptoServiceProvider
        'Dim tDes2 As New TripleDES
        Dim cryptStream As CryptoStream
        Dim iCryptoEncryptor As ICryptoTransform
        Dim memStream As New MemoryStream()
        Dim bPlainText As Byte()
        Dim strTempEncrypt As String

        tDESProvider.KeySize = 128

        iCryptoEncryptor = tDESProvider.CreateEncryptor(m_bKey, m_bIV)
        cryptStream = New CryptoStream(memStream, iCryptoEncryptor, CryptoStreamMode.Write)
        bPlainText = System.Text.Encoding.UTF8.GetBytes(strPlainText)
        cryptStream.Write(bPlainText, 0, bPlainText.Length)
        cryptStream.FlushFinalBlock()
        Dim tmp(CType(memStream.Length - 1, System.Int32)) As Byte
        memStream.Position = 0
        memStream.Read(tmp, 0, CType(memStream.Length, System.Int32))
        memStream.Close()
        cryptStream.Close()
        strTempEncrypt = Convert.ToBase64String(tmp)
        Return strTempEncrypt
    End Function
End Class

Simple XML Encoder.

Hi All,

I had frequently convert some XML / HTML text in to an encoded text [eg: If you have to post it in a blog], so I came up with this simple [very simple :)] .NET XML encoder. Enjoy!!


[Please let me know what you think in the comments section]


Public Class Form1

    Private Sub txtPlainText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPlainText.TextChanged
        Dim strPlainText, strXMLencoded As String
        strPlainText = txtPlainText.Text
        strXMLencoded = System.Web.HttpUtility.HtmlEncode(strPlainText)
        txtDestination.Text = strXMLencoded

    End Sub

    Private Sub cmdCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCopy.Click
        Clipboard.SetText(txtDestination.Text.Trim())
    End Sub

    Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvert.Click
        Dim strPlainText, strXMLencoded As String
        strPlainText = txtPlainText.Text
        strXMLencoded = System.Web.HttpUtility.HtmlEncode(strPlainText)
        txtDestination.Text = strXMLencoded
    End Sub
End Class

Sending a dummy message to a windows application

Hi All,

Here is my requirement: I have a virtual Windows server 2008 R2 environment, where I don't have much rights. I need to monitor some stuff in there, but since screen saver is enabled in the environment, the screen locks itself after few minutes. I did a bit of research and came up with this dummy program, which "simulates" some events, so that the environment thinks something is going on there, so it doesn't locks. But I've been thinking, there should be a better way. If anyone knows, please leave it in the comments section.


Here is the code I used :
[I will open up the windows notepad  and would type in the following in the window title text box: Untitiled - Notepad]


Imports System.Runtime.InteropServices
Imports System.Threading

Public Class frmSendString
    "user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    End Function
    "user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function

    Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
    (ByVal hWnd As IntPtr) As Boolean
    Dim myThread As New Thread(AddressOf ThreadProcessor_Dinesh)

    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
        Try
            ' Activate an application window.

            'If hwndNotepad <> 0 Then
            'SendMessage(dHref, New IntPtr(256), New IntPtr(96), &H1E0001)
            'SendMessage(dHref, New IntPtr(258), New IntPtr(96), &H1E0001)
            'SendMessage(dHref, New IntPtr(257), New IntPtr(96), &HC01E0001)
            StartSendingDummyEvents()
            'End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub StartSendingDummyEvents()
        myThread.Start()
    End Sub


    Private Sub ThreadProcessor_Dinesh()
        Dim hwndApp As IntPtr
        Dim dHref As HandleRef
        Dim irandVal As Integer
        Dim myRandi As New Random()

        Try
            While True
                hwndApp = FindWindow(vbNullString, txtWindowTitle.Text)
                dHref = New HandleRef(Nothing, hwndApp)
                'TextBox1.Text = dHref.Handle.ToString()
                SetForegroundWindow(hwndApp)
                'SendKeys.SendWait("111")
                Thread.Sleep(300)
                'SendKeys.SendWait("N1")
                Thread.Sleep(300)
                hwndApp = FindWindow(vbNullString, Me.Text)
                ' TextBox1.Text = dHref.Handle.ToString()
                SetForegroundWindow(hwndApp)
                irandVal = myRandi.Next()
                'Thread.Sleep(irandVal * 2)
                Thread.Sleep(500)
            End While
        Catch ex As Exception
            hwndApp = Nothing
        End Try
    End Sub

    Private Sub frmSendString_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        myThread.Abort()
        myThread = Nothing
    End Sub

    Private Sub frmSendString_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class



Wednesday, June 23, 2010

SSH / Telnet Keep connection alive in putty!


Folks! I have found the setting in the putty SSH client which enables the client to send a null packet to the server daemon process ever 'x' seconds. Hmmm... Big sigh of relief for me!! It was driving me crazy..
 
This is what you have to do to keep the SSH / Telnet connection alive for extended period of time.
 
Open putty settings, navigate to SSH, There will be an option to give the keep the connection alive. By default it will be '0', change it to something between 5 to 30.

Then save the settings to the default setting, so that this setting will be made permanent.

Tuesday, June 15, 2010

My Google voice number!

The below widget would enable you folks to communicate to me via telephone! Try that out!!! (Anyway no one talks to me.. It is so lonely here...)




Question: SSH inactivity closes the session...

Folks, I have been using SSH (secure shell) to open sessions to sun solaris servers in my work place, recently, I am running in to issues. If I keep the shell open for long, because of the IDLE_TIMEOUT setting my admin has put in, the connection terminates after 300 seconds. It is very annoying. To overcome this problem, I am running prstat to transfer some dummy data back and forth between the terminal and the server. If you have any other suggestions, please let me know in the comments section.. Much appreciated..