.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Encryption & Decryption >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

To Wite in XML File in Encrypted Format & To Read from XML file in Decrypted Format

Posted By:Prasanti Prusty       Posted Date: December 16, 2009    Points: 10    Category: Encryption & Decryption    URL: http://www.dotnetspark.com  
 

Let the Table Having Records like Following
select * from tbcountrymst

1 Afghanistan
2 Albania
3 Algeria
4 Andorra
5 Angola
6 Antigua and Barbuda
7 Argentina
8 Armenia
9 Australia
10 Austria
11 Azerbaijan


Code :

Public Class ReadWriteXML

#Region "Variables"
Dim Con As New SqlClient.SqlConnection
Dim com As New SqlClient.SqlCommand
Dim ds, dsRW As New DataSet
#End Region

Private Sub ReadWriteXML_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Con.ConnectionString = "Password=YY;Persist Security Info=True;User ID=YY;Initial Catalog=YY;Data Source=YY"
Con.Open()
End Sub

#Region "EnCrypt or DeCrypt String "
Public Function fnEnCryptDeCrypt(ByVal Text As String) As String
Dim strTempChar As String = "", i As Integer
For i = 1 To Len(Text)
If Asc(Mid$(Text, i, 1)) < 128 Then
strTempChar = CType(Asc(Mid$(Text, i, 1)) + 128, String)
ElseIf Asc(Mid$(Text, i, 1)) > 128 Then
strTempChar = CType(Asc(Mid$(Text, i, 1)) - 128, String)
End If
Mid$(Text, i, 1) = Chr(CType(strTempChar, Integer))
Next i
Return Text
End Function
#End Region

'To Write To XML File in Encrypted Format
Private Sub btnWriteToXML_Encrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWriteToXML_Encrypt.Click
ds = New DataSet
dsRW = New DataSet
Dim adp As New SqlClient.SqlDataAdapter("Select Code,Description From tbcountrymst", Con)
adp.Fill(ds)

If ds.Tables.Count > 0 Then
If ds.Tables(0).Rows.Count > 0 Then

'Needs to Crate Dataset Which Columns DataType should be String.
'So It will allow any Characters as the data (Encrypted Data)
dsRW.Tables.Add()
dsRW.Tables(0).TableName = ds.Tables(0).TableName
For j As Integer = 0 To ds.Tables(0).Columns.Count - 1
Dim dc As New DataColumn
dc.ColumnName = ds.Tables(0).Columns(j).ColumnName
dc.DataType = "String".GetType()
dsRW.Tables(0).Columns.Add(dc)
Next

'Now Encrypt all the Original Value and put it in New dataset which keeps all the encrypted data.
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
dsRW.Tables(0).Rows.Add()
For j As Integer = 0 To ds.Tables(0).Columns.Count - 1
dsRW.Tables(0).Rows(i).Item(j) = fnEnCryptDeCrypt(ds.Tables(0).Rows(i).Item(j))
Next
Next
End If
End If

'Write the Encrypted Dataset in XML
dsRW.WriteXml("F:\Projects\Random\Random\ReadWriteXMLFile.xml")

'To Check the Output (Encrypted Value) at the RunTime
Process.Start("F:\Projects\Random\Random\ReadWriteXMLFile.xml")

End Sub

'To Read From XML File in Decrypted Format
Private Sub btnReadfromXML_Decrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadfromXML_Decrypt.Click
ds = New DataSet

'Read the Encrypted Data From XML file
ds.ReadXml("F:\Projects\Random\Random\ReadWriteXMLFile.xml")

If ds.Tables.Count > 0 Then
If ds.Tables(0).Rows.Count > 0 Then
For i As Integer = 0 To ds.Tables(0).Rows.Count - 1
For j As Integer = 0 To ds.Tables(0).Columns.Count - 1

'Decrypt all the Values in the DataSet
ds.Tables(0).Rows(i).Item(j) = fnEnCryptDeCrypt(ds.Tables(0).Rows(i).Item(j))

Next
Next
End If
End If

'To Check the Output (Decrypted Value) at the RunTime
ds.WriteXml("F:\Projects\Random\Random\ReadWriteXMLFile.xml")
Process.Start("F:\Projects\Random\Random\ReadWriteXMLFile.xml")
End Sub

End Class

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend