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