Sign in to follow this  
Followers 0
2Radon

Crypting an ini and getting only one section, even if I add different ones.

22 posts in this topic

#1 ·  Posted (edited)

I can't come to why it only writes ONE section in the ini file EVER.

Sorry I can't put up the whole program, but it is quite extensive and may have sensitive information also. Hope you can help anyhow. :(

#include <Crypt.au3>
#include <MsgBoxConstants.au3>
#include <ColorConstants.au3>
            $rUSR = GUICtrlRead($txtUSR) ; Read entered USR and PW
            $rPW = GUICtrlRead($txtPW)
            If Not($rUSR="") And Not($rPW="") Then ; If not empty
                _Crypt_Startup()
                $rUSR = _StringEncrypt(1, $rUSR, "cryptpw") ; 2 ARPL
                $rPW = _StringEncrypt(1, $rPW, "cryptpw") ; Encrypt the string
                If FileExists("Creds") Then ; If a file containing credentials doesn't already exist
                    _Crypt_DecryptFile("Creds", "Creds.ini", "cryptpw2, $CALG_AES_256) ; Turn it back into an ini file
                    FileSetAttrib("Creds.ini", "+H")
                EndIf
                IniWrite("Creds.ini", $rCredsButton, "USR", $rUSR) ; Add the encrypted USR and PW values under the chosen section
                IniWrite("Creds.ini", $rCredsButton, "PW", $rPW)
                _Crypt_EncryptFile("Creds.ini", "Creds", "cryptpw2, $CALG_AES_256) ; Turn the ini back into an encrypted file
                _Crypt_Shutdown()
                FileSetAttrib("Creds", "+H")
                FileDelete("Creds.ini") ; Delete the ini so that only a double-encrypted credentials file remains
                IniWrite($fINI, "Creds", $rCredsButton, 1)
                GUICtrlSetData($txtUSR, "")
                GUICtrlSetData($txtPW, "")
            Else
                MsgBox($MB_OK, "Wrong Input", "Please enter Username and Password for " & $rCredsButton & "!") ; USR and PW not entered
            EndIf



Func _StringEncrypt($fEncrypt, $sData, $sPassword)
    _Crypt_Startup() ; Start the Crypt library.
    Local $sReturn = ''
    If $fEncrypt Then ; If the flag is set to True then encrypt, otherwise decrypt.
        $sReturn = _Crypt_EncryptData($sData, $sPassword, $CALG_AES_256)
    Else
        $sReturn = BinaryToString(_Crypt_DecryptData($sData, $sPassword, $CALG_AES_256))
    EndIf
    _Crypt_Shutdown() ; Shutdown the Crypt library.
    Return $sReturn
EndFunc   ;==>_StringEncrypt
Edited by 2Radon

Share this post


Link to post
Share on other sites



Why should it write to any section other than "Creds"?

As written in IniWrite in that code above, it must write in Creds.ini in section $rCredsButon (which can be, for example, "Google", "Bank", "Facebook"...) in the keys USR and PW under that section. So if there is already a section [Google] with USR and PW under it, next time it writes in case of [bank] it should create a new section but with keys USR and PW also under it. All I'm getting is one section in Creds.ini getting overwritten every time with the new values in section and keys.

Share this post


Link to post
Share on other sites

Seems I have found out through testing that when decrypting it somehow brings back only one Section of the ini file. What am I fusing here?!

Share this post


Link to post
Share on other sites

I think you have filename and section confused.

EDIT:

Or maybe section and key

 

Nay, as stated in the help file for IniWrite(), first comes file, then section, then key, then value. Just like I have.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Yes, and in your code the only section you attempt to write to is "Creds".

IniWrite($fINI, "Creds", $rCredsButton, 1)

Sorry, should have cropped that too. That is a different ini file.

This is the most important part:

_Crypt_Startup()
                $rUSR = _StringEncrypt(1, $rUSR, "cryptpw") ; 2 ARPL
                $rPW = _StringEncrypt(1, $rPW, "cryptpw") ; Encrypt the string
                If FileExists("Creds") Then ; If a file containing credentials doesn't already exist
                    _Crypt_DecryptFile("Creds", "Creds.ini", "cryptpw2, $CALG_AES_256) ; Turn it back into an ini file
                    FileSetAttrib("Creds.ini", "+H")
                EndIf
                IniWrite("Creds.ini", $rCredsButton, "USR", $rUSR) ; Add the encrypted USR and PW values under the chosen section
                IniWrite("Creds.ini", $rCredsButton, "PW", $rPW)
                _Crypt_EncryptFile("Creds.ini", "Creds", "cryptpw2, $CALG_AES_256) ; Turn the ini back into an encrypted file
                _Crypt_Shutdown()

It currently saves 2 out of 4 different sections for some friggin flippin dog knows what reason and drives me mad and keeps me tied.

Edited by 2Radon

Share this post


Link to post
Share on other sites

Is that your actual code, because you have a missing quote in the line below? It belongs after cryptw2.

_Crypt_DecryptFile("Creds", "Creds.ini", "cryptpw2, $CALG_AES_256)

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

Share this post


Link to post
Share on other sites

 

Is that your actual code, because you have a missing quote in the line below? It belongs after cryptw2.

_Crypt_DecryptFile("Creds", "Creds.ini", "cryptpw2, $CALG_AES_256)

 

Nop, I simply replaced sensitive information as I was writing this post. :( In my real code it has everything and works but as I described weirdly behaves I believe...

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Instead of writing the data to disk without encryption and then encrypt the data on the disk, for security reasons it is better for you to write the encrypted data directly to disk using >IniVirtual

What you can do is:
1) read the encrypted data (ini data) from the disk in to a variable
2) decrypt the variable
3) do what you want to do with IniVirtual - so after i did what you want, the decrypted variable (the ini data) was updated.
4) Encrypt again the updated decrypted variable so you will have encrypted variable
5) Write the encrypted variable to the file
 
 
Edited by gil900

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I want to give the program for you guys to see and find the problem, but it's got sensitive information for a company, some names, which could be really messy to cover.

I am still constantly testing it all day and it is just senseless.

The code I wrote in the first post is, I think, responsible for writing only ONE section + USR and PW key in the file that is double encrypted.

Could double encryption mess it hard? It encrypts the ini value, which is written in the ini, and this ini file is encrypted. To retrieve the PW and USR it is done vice versa. But It won't write more than one section, no matter that it is different. Also, a bit earlier it worked by saving 2 sections and not saving 2 others. Now it's back at one. No idea.

I read the code through several times, it is supposed to do what I want, everything matches from A to Z, but it doesn't. I don't see it.

Can I maybe log whatever the program does very closely and see where it writes in the file and what happens then, and what happens in encryption steps?

 

 

Shouldn't this successfully Decrypt the file, delete the said lines and then encrypt this modified file back to the previous file?

If FileExists($fCreds) Then             
_Crypt_Startup()
                _Crypt_DecryptFile($fCreds, $fCredsINI, "AteaReady4PonyLimbo", $CALG_AES_256) ; Turn it back into an ini file
                FileSetAttrib($fCredsINI, "+H")
                IniDelete("Creds.ini", $rCredsButton);, "USR")
                IniDelete("Creds.ini", $rCredsButton);, "PW")
                ;IniWrite($fCredsINI, $rCredsButton, "USR", "0") ; Doesn't work as well
                ;IniWrite($fCredsINI, $rCredsButton, "PW", "0")
                _Crypt_EncryptFile($fCredsINI, $fCreds, "AteaReady4PonyLimbo", $CALG_AES_256) ; Turn the ini back into an encrypted file
                _Crypt_Shutdown()
                FileSetAttrib($fCreds, "+H")
                FileDelete($fCredsINI) ; Delete the ini so that only a double-encrypted credentials file remains
Edited by 2Radon

Share this post


Link to post
Share on other sites

Attached a new code I made.

This one works better though, but still, when I mess around with deleting and writing and other things I can do with this it ends up in about the same glitch.

Play around with this and see that it doesn't work as intended:

Creds and Creds.ini are hidden files.

Use WriteCreds.au3 to write to Creds.ini and then encrypt to Creds.

Use ReadCredsTest.au3 to see what it can read out of Creds (not Creds.ini).

Using WriteCreds GUI, delete some, write some again, overwrite some, check inside Creds.ini and what ReadCredsTest.au3 says.

Gosh!

CredsSolving.zip

CredsSolving.zip

Share this post


Link to post
Share on other sites

FYI, there's an >UDF to encrypt an ini.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

FYI, there's an >UDF to encrypt an ini.

 

Dangit, things I didn't really expect appear in UDFs!

But regarding my own pile of code - why does it do so? Can I make it work without digging into this UDF? :(

 

Does double-encryption like I am doing help any actually?

I want to hide the fact that it is an ini file also, like I am doing.

Edited by 2Radon

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Sure, but this UDF looks neat (except some mistakes I corrected post #2).

Edited by FireFox

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Sure, but this UDF looks neat (except some mistakes I corrected post #2).

So can you come up with why exactly my code makes flawed results like it does? Is it AutoIT related, not really a fault in my code per se?

Edited by 2Radon

Share this post


Link to post
Share on other sites

I haven't looked your code. Your fault I presume.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
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
Sign in to follow this  
Followers 0