Jump to content

Writing encrypted text to XML file results in garbage


gritts
 Share

Go to solution Solved by BrewManNH,

Recommended Posts

I am creating a reporting app which accesses several databases for generating reports. I hope to store some of the information in an encrypted format within an XML file. I chose XML for its readability in this case should someone need to change other properties stored there. 
When I create my encrypted information and attempt to save it to an XML file, it is stored as unusable characters. If I take the same encrypted data and write it to a text file it comes out just fine. I've attempted to use the UTF-8 setting defined for the "_XMLCreateFile" within _XMLDOMWrapper and that did not make a difference. Below is my test code...
 
#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <_XMLDOMWrapper.au3>

$sFilePath = @ScriptDir & "\testfile.txt"
Local $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
If $hFileOpen = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
EndIf



$sEnc = Example()
FileWriteLine($hFileOpen, "The encrypted data is: " & $sEnc)
FileClose($hFileOpen)

If Not FileExists(@ScriptDir & "\testfile.xml") Then
    MsgBox(0, "Missing Config File", "testfile.xml file is missing, creating a new one...")
    _XMLCreateFile(@ScriptDir & "\testfile.xml", "EncryptedCollection")
    If @error Then Exit
    ;Exit
EndIf
_XMLFileOpen(@ScriptDir & "\testfile.xml")
_XMLCreateRootChild('EncryptedInfo')
_XMLCreateChildNode('//EncryptedInfo', 'EncryptedData')
_XMLUpdateField('//EncryptedInfo/EncryptedData', $sEnc)

Func Example()
    ; Encrypt text using a generic password.
    Local $sEncrypted = StringEncrypt(True, 'Encrypt this data.', 'securepassword')

    ; Display the encrypted text.
    MsgBox($MB_SYSTEMMODAL, '', $sEncrypted)

    ; Decrypt the encrypted text using the generic password.
    Local $sDecrypted = StringEncrypt(False, $sEncrypted, 'securepassword')

    ; Display the decrypted text.
    MsgBox($MB_SYSTEMMODAL, '', $sDecrypted)
    Return $sEncrypted
EndFunc   ;==>Example

Plain text is written to the XML fine. I've attached my samples

 

Missed this function that is part of my code above:

Func StringEncrypt($bEncrypt, $sData, $sPassword)
    _Crypt_Startup() ; Start the Crypt library.
    Local $sReturn = ''
    If $bEncrypt Then ; If the flag is set to True then encrypt, otherwise decrypt.
        $sReturn = _Crypt_EncryptData($sData, $sPassword, $CALG_RC4)
    Else
        $sReturn = BinaryToString(_Crypt_DecryptData($sData, $sPassword, $CALG_RC4))
    EndIf
    _Crypt_Shutdown() ; Shutdown the Crypt library.
    Return $sReturn
EndFunc   ;==>StringEncrypt

testfile.txt

testfile.xml

Edited by gritts
Link to comment
Share on other sites

Try it without using the StringEncrypt function and use the Crypt functions. I don't see any evidence of a StringEncrypt function in your code, and older versions of AutoIt used the name _StringEncrypt with the leading underscore. The _StringEncrypt function was removed from the Crypt.au3 UDFs back in 2013.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Solution

Are you looking to get the same text that's in the testfile.txt file, in the same format as that, into the XML?

If so try this change.

_XMLUpdateField('//EncryptedInfo/EncryptedData', String($sEnc))

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

@BrewManNH, that produced the results I was expecting. I can now decrypt what is being saved to the XML file.

For education sake, what is different about writing to XML that requires converting to a string as opposed to a text file. I'll be honest I had thought about trying that but since I was able to create entries in plain text I didn't think it would work. Does it perchance have to do with the fact the encrypted data is essentially a really large hex value (or can be seen as one)?

Thank you!

Link to comment
Share on other sites

From what I saw, it was reading it as a hex value and not a string, it was putting the characters associated with the hex value and not the number, into the xml file. Turning it into a string forced it to be put into the file as a string of numbers rather than as a number or characters.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...