Monday, February 23, 2009

How to do secure encryption and decryption using DOTNET code

How to do secure encryption and decryption using DOTNET code

 

Following code will give you the way to do key based encryption and decryption, this code using Dotnet Cryptography class and vb.net. I have tested following code with Dotnet framwork 2.0 and window XP.

 

Namespace

 

Imports System.Security.Cryptography

 

Global Variable

 

'Blow will be the Key to do Encryption and Decryption

Private Shared TheKey() As Byte = {&H12, &H34, &H56, &H78, &H9A, &HBC, &HDE, &HFF}

Private Shared Vector() As Byte = {&H11, &H22, &H33, &H44, &H55, &H66, &H77, &H88}

 

Encryption

 

Public Shared Function GetEncryptedData(ByVal message As String) As String

 

Dim desServiceProvider As New DESCryptoServiceProvider

Dim memoryStream As New MemoryStream

Dim in_buf(), out_buf() As Byte

 

'Check for empty string

If message = String.Empty Then

    Return String.Empty

End If

 

' put the cleartext into the input buffer

in_buf = Encoding.ASCII.GetBytes(message)

 

Try

'create an DES Encryptor output stream

Dim crStream As New CryptoStream(memoryStream, desServiceProvider.CreateEncryptor(TheKey, Vector), CryptoStreamMode.Write)

crStream.Write(in_buf, 0, in_buf.Length)

crStream.FlushFinalBlock()

 

 ' read the ciphertext into the output array

 out_buf = memoryStream.ToArray

 

 memoryStream.Close()

 crStream.Close()

 Catch ex As System.Security.Cryptography.CryptographicException

     ' if encryption fails,return an empty string

     Return String.Empty

End Try

 

     Return Convert.ToBase64String(out_buf)

 

End Function

 

Decryption

 

Public Shared Function GetDecryptedData(ByVal message As String) As String

 

Dim desServiceProvider As New DESCryptoServiceProvider

Dim memoryStream As New MemoryStream

Dim in_buf(), out_buf() As Byte

 

'Check for empty string

If message = String.Empty Then

Return String.Empty

End If

 

' put the ciphertext into the input buffer

Try

   in_buf = Convert.FromBase64String(message)

   Catch ex As System.FormatException

   ' if the string isn't in the correct format,

 then return empty string

Return Return String.Empty

End Try

 

Try

  ' Create an DES Decryptor stream

Dim crStream As New CryptoStream(memoryStream, desServiceProvider.CreateDecryptor(TheKey, Vector), CryptoStreamMode.Write)

crStream.Write(in_buf, 0, in_buf.Length)

crStream.FlushFinalBlock()

 

' read the cleartext into the output array

out_buf = MemoryStream.ToArray

 

MemoryStream.Close()

crStream.Close()

Catch ex As System.Security.Cryptography.CryptographicException

  ' if decryption fails, just silently return an empty string

  Return String.Empty

 End Try

 

   Return Encoding.ASCII.GetString(out_buf)

 

End Function

 

Note:

 

If your project requirement is to do simple encryption decryptions then see below link

http://riteshk.blogspot.com/2004/07/how-to-do-encryption-and-decryption.html

 

If you are using Microsoft enterprise library and your project requirement is very specific to use any standard framework to do encryption and decryption then see following link

http://msdn.microsoft.com/en-us/library/cc309163.aspx

 

If you want to encrypt your app.config file the see following link

http://riteshk.blogspot.com/2008/05/programatically-configuration-files.html


Saturday, January 24, 2009

How to Generate Sequence Diagram online

How to Generate Sequence Diagram online

Don't wast your time to create sequence diagram using visio or some
other tools.
Here I found very useful site that generates sequence diagram as per
your requirement.

You have to just type very smple script that should descripe the call
from one class to
another and On click of "Draw" button you can get your secquence diagram
in the form of picture or pdf.
Don't worry about script learning and script notation, it's
very,following site will give you all help and example

http://www.websequencediagrams.com/

Tuesday, January 20, 2009

How to restore deleted table from Oracle Database

How to restore deleted table from Oracle Database

Its new feature available in Oracle 10g database, you can restore

your deleted table from database.

Try the following command to test this.

>Drop table <Table Name>;

>Table Dropped.

Oops!! Dropped the wrong table? No problem.

Just undrop it with Flashback Drop.

>FLASHBACK TABLE <Table Name> TO BEFORE DROP;

> Desc <Table Name>;

You will see dropped table description here.

How its work, to know more about this find out the

"New Features in Oracle Database 10g Release 2" section

on the following link

http://www.oracle.com/technology/deploy/availability/htdocs/Flashback_Overview.htm

Friday, December 19, 2008

How to disable close button from window form using .NET application

How to disable window close button ("X") in .NET


VB.NET

                       

To disable close button from the window form using vb.net needs some Dlls from system library. Call DisableCloseButton(me.Handle) function from your window form button click event or from where you want to disable "x" button.

Code-1

 

Private Const MF_BYPOSITION As Int32 = &H400

Private Const MF_REMOVE As Int32 = &H1000


Private Declare Auto Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As IntPtr


Private Declare Auto Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As IntPtr) As Int32


Private Declare Function DrawMenuBar Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean


Private Declare Auto Function RemoveMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32


'This method will be used to disable the Form "X" button

Public Shared Sub DisableCloseButton(ByVal hwnd As IntPtr)

Dim hMenu As IntPtr, n As Int32

hMenu = GetSystemMenu(hwnd, 0)

If Not hMenu.Equals(IntPtr.Zero) Then

              n = GetMenuItemCount(hMenu)

     If n > 0 Then

                   RemoveMenu(hMenu, n - 1, MF_BYPOSITION Or MF_REMOVE)

         RemoveMenu(hMenu, n - 2, MF_BYPOSITION Or MF_REMOVE)

                   DrawMenuBar(hwnd)

     End If

End If

End Sub

Code-2

Another way to disable close button from window form , override  "CreateParams" property. You have to Include following code in your in your window form.

     Private Const CP_NOCLOSE_BUTTON As Integer = &H200

     Protected Overloads Overrides ReadOnly Property CreateParams() As    CreateParams

         Get

             Dim myCp As CreateParams = MyBase.CreateParams

    myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON

              Return myCp

End Get

     End Property

C#.NET

In C#.net its simple just override "CreateParams" property, that's all.

Including following code in your in your window form.

private const int CP_NOCLOSE_BUTTON = 0x200;

protected override CreateParams CreateParams

{

   get

   {

      CreateParams myCp = base.CreateParams;

      myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;

      return myCp;

    }

 }

Monday, December 15, 2008

How to play sound with .NET

Play different sound with .NET

You can make some noise in .NET application using VB/C# .NET code. There are multiple ways to do this, I am giving one of them; hope it's fulfill your requirement.

VB.NET

'You can add some more sound as per your requirement

Public Enum MessageBeepType

[Default] = -1

OK = &H0

[Error] = &H10

Question = &H20

Warning = &H30

Information = &H40

End Enum

' This allows you to 'Beep' the sounds declared in the MessageBeepType

<Runtime.InteropServices.DllImport("USER32.DLL", setlasterror:=True)> _

Public Shared Function MessageBeep(ByVal type As MessageBeepType) As Boolean

End Function

' Allowing you to specify the frequency and duration of the sound.
<Runtime.InteropServices.DllImport("KERNEL32.DLL")> _
Public Shared Function Beep(ByVal frequency As Integer, ByVal duration As Integer) As Boolean
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MessageBeep(MessageBeepType.Default)

End Sub

C#.NET

Public Enum MessageBeepType

{

Default = -1,

OK = 0x0,

Error = 0x10,

Question = 0x20,

Warning = 0x30,

Information = 0x40

}

//This allows you to 'Beep' the sounds declared in the MessageBeepType

[Runtime.InteropServices.DllImport("USER32.DLL", setlasterror = true)]

public static bool MessageBeep(MessageBeepType type)

{

}

' Allowing you to specify the frequency and duration of the sound.

[Runtime.InteropServices.DllImport("KERNEL32.DLL")]
public static bool Beep(int frequency, int duration)

{

}

Button1_Click(object sender, System.EventArgs e)

{

MessageBeep(MessageBeepType.Default);

}

Tuesday, December 02, 2008

How to add Image in the DatagridView using VB.NET

How to add image or icon in the DatagridView column using vb.net or c#.net

VB.NET

Dim dgImage As New DataGridViewImageColumn
dgImage.Image = Drawing.Image.FromFile("C:\FixedOption.ico")
dgImage.ReadOnly = True
dgImage.ImageLayout = DataGridViewImageCellLayout.Normal
dgDataGridView.Columns.Add(dgImage)

'After This you can bind the data grid with any dataset,if you want
dgDataGridView.DataSource = DataSet

C#.NET

dgDataGridView.Rows.Clear();

DataGridViewImageColumn dgImage = new DataGridViewImageColumn();

dgImage.Image = Drawing.Image.FromFile("C:\\IconLocation\\FixedOption.ico");

dgImage.ReadOnly = true;

dgImage.ImageLayout = DataGridViewImageCellLayout.Normal;

dgDataGridView.Columns.Add(dgImage);

//After This you can bind the data grid with any dataset

dgDataGridView.DataSource = BindDataSet;

Thursday, November 20, 2008

Error: The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging' cannot be resolved. Please verify the spelling is correct or that the full type

Error: The type 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging' cannot be resolved. Please verify the spelling is correct or that the full type name is provided.

This error occurs when you work with Microsoft Exception and Logging block.

I got this error with VS2005 and Microsoft Exception and Logging 3.1.0.0,

I resolve this error while adding following reference into User Interface (where my app.config file exists)

Right click on the project and say Add Reference and select this reference from "EntLib3Src\App Blocks\bin" folder

Add Reference: "Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.dll"

Note: This folder create, when you install "Enterprise Library 3.1" in your system

http://www.microsoft.com/downloads/details.aspx?FamilyID=4c557c63-708f-4280-8f0c-637481c31718&displaylang=en