C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Encrypt & Decrypt a string - Data Encryption Standard (DES)


Posted Date: 24 Feb 2007    Resource Type: Code Snippets    Category: Security

Posted By: ANITA MARY JOSEPH       Member Level: Gold
Rating:     Points: 10



DES is a symmetric encryption where a secret key is used to encrypt data and the very same key is used to decrypt it too.

The below snippet can be tested as follows:
TextBox1.Text = "dotnetspider"
TextBox2.Text = EncryptToBase64String(TextBox1.Text, "91817161")
TextBox3.Text = DecryptFromBase64String(TextBox2.Text, "91817161")



Public Function EncryptToBase64String(ByVal stringToEncrypt As String, ByVal SEncryptionKey As String) As String
Try
'Convert the Encryption Key to an array of Bytes
Dim key As Byte() = {}
key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey)

'Convert the original string to an array of Bytes
Dim inputByteArray As Byte() = Encoding.UTF8.GetBytes(stringToEncrypt)

'set the initialization vector
Dim IV As Byte() = {12, 34, 56, 78, 90, 92, 94, 96}

'create a memory stream.
Dim ms As MemoryStream = New MemoryStream

'create a crypto service provider object
Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider

'Create a CryptoStream using the memory stream,Key and the initialization vector
Dim cs As CryptoStream = New CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()

'Return the encrypted byte array that represents the memory stream.
Return Convert.ToBase64String(ms.ToArray)
Catch e As Exception
Return e.Message
End Try
End Function

Public Function DecryptFromBase64String(ByVal stringToDecrypt As String, ByVal sDecryptionKey As String) As String
Try

'Convert the Decryption Key to an array of Bytes
Dim key As Byte() = {}
key = System.Text.Encoding.UTF8.GetBytes(sDecryptionKey)

'Convert the original string to an array of Bytes
Dim inputByteArray((stringToDecrypt.Length)) As Byte
inputByteArray = Convert.FromBase64String(stringToDecrypt)

'set the initialization vector
Dim IV As Byte() = {12, 34, 56, 78, 90, 92, 94, 96}

'create a memory stream.
Dim ms As MemoryStream = New MemoryStream

'create a crypto service provider object
Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider

'Create a CryptoStream using the memory stream,Key and the initialization vector
Dim cs As CryptoStream = New CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write)
cs.Write(inputByteArray, 0, inputByteArray.Length)
cs.FlushFinalBlock()

'Return the decrypted byte array that represents the memory stream.
Dim encoding As System.Text.Encoding = System.Text.Encoding.UTF8
Return encoding.GetString(ms.ToArray)

Catch e As Exception
Return e.Message
End Try
End Function




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Previous Resource: Programmatically deny the Read permission on a file
Return to Discussion Resource Index
Post New Resource
Category: Security


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use