By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,136 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,136 IT Pros & Developers. It's quick & easy.

saveing a txt file with a unique name?

P: 9
How can i save a .txt file where the name of the file would be the result of the selection made from a Dom tree view's parent node tag in visual basic?
So for example i have an editable tree view form i add an entry to the tree view when i save it another process is started "part of which is an encryption scheme" the important part i need help with is saving the sharedkey as the value of the elements parent tag applied to a new addition to the tree and retrieving the key from a selected elements tag value. so for example i make a new account in my vb the new account code looks like this
Expand|Select|Wrap|Line Numbers
  1.  Private Sub saveNewAccount()       
  2.  
  3.         Dim objAccount As MSXML2.IXMLDOMElement
  4.         Dim objNewChild As MSXML2.IXMLDOMElement
  5.  
  6.         On Error GoTo ErrorHandler
  7.         If txtUsername.Text = "" Then
  8.         ElseIf txtPassword.Text = "" Then
  9.         ElseIf txtServer.Text = "" Then
  10.             Err.Number = 424
  11.             GoTo ErrorHandler
  12.             Exit Sub
  13.         Else
  14.  
  15.         End If
  16.  
  17.         objAccount = m_objDOMAccounts.createElement("Account")
  18.         objAccount.setAttribute("AccountID", txtUsername.Text)
  19.         m_objDOMAccounts.documentElement.appendChild(objAccount)
  20.  
  21.         objNewChild = m_objDOMAccounts.createElement("Username")
  22.         objNewChild.text = txtUsername.Text
  23.         objAccount.appendChild(objNewChild)
  24.  
  25.  
  26.         objNewChild = m_objDOMEQ2Accounts.createElement("EncryptedData")
  27.         objNewChild.text = txtPassword.Text
  28.         objAccount.appendChild(objNewChild)
  29.  
  30.  
  31.         objNewChild = m_objDOMEQ2Accounts.createElement("Server")
  32.         objNewChild.text = txtServer.Text
  33.         objAccount.appendChild(objNewChild)
  34.  
  35.         objNewChild = m_objDOMEQ2Accounts.createElement("Character")
  36.         objNewChild.text = txtCharacter.Text
  37.         objAccount.appendChild(objNewChild)
  38.  
  39.         populateTreeWithChildren(objAccount)
  40.         MsgBox("The Account was created!")
  41.  
  42.         'Error handling
  43. ErrorHandler:
  44.         If Err.Number = 424 Then
  45.             MsgBox("An Account Username, Password and Server are required to create a new account!")
  46.             If Err.Number = 1 Then
  47.                 MsgBox("That Account all ready exists! Please choose a different Account Username!")
  48.                 If Err.Number = 2 Then
  49.                     MsgBox("A different error!")
  50.                     If Err.Number = 3 Then
  51.                         MsgBox("Another error!")
  52.                     Else : MsgBox("Account was created but with errors! You should probably reset your data!")
  53.                     End If
  54.                 End If
  55.             End If
  56.         End If
  57.  
  58.         m_objDOMAccounts.save(m_strXmlPath)
  59.         Encrypt()
  60.         tvwAccounts.Refresh()
  61.         webTarget.Refresh()
  62.         'clear the object data from memory
  63.         objAccount = Nothing
  64.         objNewChild = Nothing
  65.  
  66.     End Sub
  67.  
The resulting out put xml is added to an existing xml doc looks like below.
the xml looks like this before encryption see the previous account is all ready encrypted after code finishes the second will be encrypted with its own unique key.

Expand|Select|Wrap|Line Numbers
  1. <Accounts>
  2.   <Account AccountID="MYNAME">
  3.     <Username>MYNAME</Username>
  4.     <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
  5.       <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
  6.       <CipherData>
  7.         <CipherValue>His98SF14aBCaV6we/frjsj3Mkx6R7KkRx4PLFIEzJh24NP8XIMllJqUaVSmORIUFUD482VAx/k=</CipherValue>
  8.       </CipherData>
  9.     </EncryptedData>
  10.     <Server>server</Server>
  11.     <Character>username</Character>
  12.   </Account>
  13. <Account AccountID="MYNAME2">
  14.     <Username>MYNAME2</Username>
  15.     <EncryptedData>passwordnotyetencrypted</EncryptedData>
  16.     <Server>server</Server>
  17.     <Character>username</Character>
  18.   </Account>
  19. </Accounts>
  20.  
the encryption and decryption schemes work fine the problem is each time a new account is added, edited or deleted a new sharedkey is written for the selected account. yet each encrypted data element needs its own individual sharedkey the result is data that cant be decrypted because the key was overwritten by the new or edited element! The key is created and read as in the codes below...

Expand|Select|Wrap|Line Numbers
  1. Dim sharedkey As New TripleDESCryptoServiceProvider()
  2.  
  3.  
  4.         Dim writer2 As IO.StreamWriter = New IO.StreamWriter("SharedTDESKey.txt")
  5.         Dim str As String = Convert.ToBase64String(sharedkey.Key)
  6.         writer2.WriteLine(str)
  7.         writer2.Close()
  8.  
Expand|Select|Wrap|Line Numbers
  1. Dim sharedkey As New TripleDESCryptoServiceProvider()
  2.  
  3.         Dim reader As IO.StreamReader = New IO.StreamReader(Application.StartupPath & "\sharedTDESkey.txt")
  4.         Dim data() As Byte = Convert.FromBase64String(reader.ReadToEnd)
  5.         'Assign the retrieved shared key value to the TripleDESCryptoServiceProvider object
  6.         sharedkey.Key = data
  7.  
What i need is the sharedTDESkey.txt to be saved and read as something like
sharedTDESkey_MYNAME2.txt
or similar... something to make it unique to the account it is used for yet identifiable to the vb Dom tree view so it can later be called on to decrypt the data when need!
Its probably fairly simple by some standards but this is my first real project and ive overcome many of the more complicated obstacles yet get stuck on the simple ones... So any help is appreciated from an example to an explanation or just a suggestion! Peace All!
Aug 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: 9
I tried various methods and no matter what the SharedTDESKey keeps that name and overwrites! I thought that this would have worked but no luck...

Expand|Select|Wrap|Line Numbers
  1. EncryptionKeyName = lblElement.Text
  2.  
  3. Dim FileName As String = EncryptionKeyName  &  "_"  &  "SharedTDESKey"
  4.         Dim sharedkey As New TripleDESCryptoServiceProvider()
  5.         Dim str As String = Convert.ToBase64String(sharedkey.Key)
  6.         Dim StuffToWrite As String = (str)
  7.         Dim strPath As String = strSaveToPath & FileName & ".txt"
  8.         Dim SW As StreamWriter
  9.         Dim FS2 As FileStream
  10.         FS2 = New FileStream(strPath, FileMode.Create)
  11.         SW = New StreamWriter(FS2)
  12.         SW.Write(StuffToWrite)
  13.         SW.Close()
  14.         FS2.Close()
  15.  
lblelement.text contains the info i want included in the file name
I was hoping to get Myaccount_SharedTDESKey.txt
Aug 2 '07 #2

P: 9
im begining to think theres not anyone here that knows anything at all of all the posts ive made here only 1 was answered and all i got was a link to the ms website you guys are genious....
Aug 4 '07 #3

Expert 5K+
P: 8,434
im begining to think theres not anyone here that knows anything at all of all the posts ive made here only 1 was answered and all i got was a link to the ms website you guys are genious....
Sorry to hear you're not getting much response here, deaddog. We generally try to answer all questions. Unfortunately we seem to be a bit short on VB experts at the moment.

Since we are just volunteering our own time when we can spare it, it can be quite difficult to keep up.

You're quite welcome to join in and help improve the response.


P.S. I haven't tried to answer this one, as I don't know any XML.
Aug 4 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.