C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Encryption »

Encrypting Selected node in a XML file


Posted Date: 30 Apr 2008    Resource Type: Code Snippets    Category: Encryption
Author: DharmarajMember Level: Diamond    
Rating: 1 out of 5Points: 30



Imports System
Imports System.XML
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Public Class Encrypt

Public Function startEncrypt(ByVal filename As String) As Boolean
Dim xmldoc As New XmlDocument()
Try
xmldoc.Load(filename)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try

' Create a new TripleDES object, TripleDes will be the algorithm used to encrypt the XML data
Dim sharedkey As New TripleDESCryptoServiceProvider()

''Save this key to disk to enable the recipient to decrypt
Dim writer2 As IO.StreamWriter = New IO.StreamWriter("SharedTDESKey.txt")
Dim str As String = Convert.ToBase64String(sharedkey.Key)
writer2.WriteLine(str)
writer2.Close()

'Create a new EncryptedXML object
Dim exml As EncryptedXml = New EncryptedXml(xmldoc)

'Select the Billing element to be encrypted
Dim conifigElem As XmlElement = CType(xmldoc.SelectSingleNode("/configuration/appSettings"), XmlElement)

'Encrypt the billing element data using the TripleDES alogrithm, save the results into a byte array

Dim encryptedBilling As Byte() = exml.EncryptData(conifigElem, sharedkey, False)

' Create an EncryptedData object and populate it.
Dim ed As New EncryptedData()

' Specify the namespace URI for XML encryption elements.
ed.Type = EncryptedXml.XmlEncElementUrl
' Specify the namespace URI for the TrippleDES algorithm.
ed.EncryptionMethod = New EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl)


' Create a CipherData element.
ed.CipherData = New CipherData()

' Set the CipherData element to the value of the encrypted XML element.
ed.CipherData.CipherValue = encryptedBilling


' Replace the plaintext XML elemnt with an EncryptedData element.
EncryptedXml.ReplaceElement(conifigElem, ed, False)

'Write the encrypted data to disk
Try
xmldoc.Save(filename)

Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return True
End Function
End Class




Responses

Author: Kapil Dhawan    17 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil








Author: Dharmaraj    18 Jun 2008Member Level: Diamond   Points : 1
Hi,
Nice to hear from you. I will give my best code here.



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add 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.
Next Resource: How to encript and decript a password
Previous Resource: Generating MD5 for a string
Return to Discussion Resource Index
Post New Resource
Category: Encryption


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use