Jump to content

how to Write INI to String


Recommended Posts

good morning sirs.

please i have a request from you.

i have an variable to Read a data from a file

this data is Encrypted

and when i read it i Decrypte it.

for that i need a function to Write a ini data to string.

;#Function# =====================================================================================================================
; Name............: _IniReadFromString
; Description.....: Returns the value of a key in a specific section of an ini-formatted string
; Syntax..........: _IniReadFromString($szInput, $szSection, $szKey, $Default)
; Parameters......:
;   $szInput - The string that contains data in ini format
;   $szSection   - The sectionname (just as in IniRead)
;   $szKey   - The keyname (just as in IniRead)
;   $Default - The default value if the key does not exist or reading failed (just as in IniRead)
; Return values ..:
;   Success  - Returns the read value
;   Failure  - Returns $Default
; Author .........: FichteFoll
; Remarks ........: Works for Unicode as well as for ANSI
; Related ........: IniRead, _IniReadSectionFromString
; Link ...........; See on top
; Example ........; $var = _IniReadFromString(StringFormat("[Sect]\r\nMyKey1=value1\r\nMyKey2=value2"), "Sect", "MyKey2", "no_value")
; ===============================================================================================================================
Func _IniReadFromString($szInput, $szSection, $szKey, $Default)
$szInput = StringStripCR($szInput)
;~  Local $aRegMl = StringRegExp($szInput, "\[" & __StringEscapeRegExp($szSection) & "\]\n+(?:[^\[].*?=.*\n)*" & __StringEscapeRegExp($szKey) & "=(.*)\n?(",3)

Local $aRegMl = StringRegExp($szInput, "\[" & __StringEscapeRegExp($szSection) & "\]\n+(?:[^\[].*?=.*\n)*" & __StringEscapeRegExp($szKey) & "=(.*)\n?", 3)
If @error Then Return SetError(1, 0, $Default) ; key not found    
Return $aRegMl[0]

EndFunc;==>_IniReadFromString


; ###############################################################################################################################

; ===============================================
; = Internal Use Only
; ===============================================
Func __StringEscapeRegExp($szExp)
Return StringRegExpReplace($szExp, "([\(\)\[\]\{\}\\\/\?\.\\|\+])", "\\$1") ; ()[]{}\/?.|+
EndFunc;==>__StringEscapeRegExp

like this  function Read the ini from string.

 

 

please ihelp me

thanks in advance

Link to comment
Share on other sites

Where is your  problem exactly?

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

Of course, you could write a function for that. A quick&dirty workaround would be

a) create a temp file

b) use IniWriteSection to that file

c) Use IniReadSection from that file

d) delete the tempfile

 

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

30 minutes ago, Xenobiologist said:

Of course, you could write a function for that. A quick&dirty workaround would be

a) create a temp file

b) use IniWriteSection to that file

c) Use IniReadSection from that file

d) delete the tempfile

 

yes sir i can do that but the file i Contains important information such as contact information for FTP data, I'm afraid it can be caught using continuous temporary file copies.

for that I prefer reading the file internally

i hope that i find any other idea

Link to comment
Share on other sites

there is this UDF can read the ini from string 

i used it to read my protected string

but i need the same to write to string

for example i have

$s_ini = StringFormat("[test]\r\nkey=value\r\nkey2=value2\r\n")

and i want to change the value of key

i know i can use the replace

but what about if the ini has more than one section?

for that i hope to find for example

_iniWriteToString($s_string, $s_Section, $s_key, $s_value)

here is the INI Read UDF

iniex.au3

Link to comment
Share on other sites

Are you sure that an INI file can even hold encrypted strings? You could always just encrypt the whole INI file after writing the strings to it in the usual way.

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

here is an example about the protected string

i couldn't use the normal iniWrite

#include <Crypt.au3>
$string = StringEncrypt(true, StringFormat("[test]\r\nkey=value\r\nkey2=value2\r\n"), "1234")
msgBox(64, "result", "Encrypted value" & @crlf & $string & @crlf & "string value" & @crlf & StringEncrypt(false, $string, "1234"))
exit
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

i hope you can help me

Link to comment
Share on other sites

Then, why are you using an INI file? Use something better, like a DB or other file type.

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

Just now, BrewManNH said:

Then, why are you using an INI file? Use something better, like a DB or other file type.

the ini is easy and The result is guaranteed.

Unlike other methods, it's hard to handle and you need quite a lot of experience.

Link to comment
Share on other sites

25 minutes ago, nacerbaaziz said:

the ini is easy and The result is guaranteed.

This whole thread proves that statement wrong. You're using the wrong tool from the toolbox. You're using a sledgehammer and a crowbar to force fit something into something it's not designed to hold.

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

18 hours ago, nacerbaaziz said:

yes sir i can do that but the file i Contains important information such as contact information for FTP data, I'm afraid it can be caught using continuous temporary file copies.

for that I prefer reading the file internally

i hope that i find any other idea

You could also encrypt the data with Autoit before writing it to the temp file.

Then read it again and decrypt it in your script.

🙂

Like BrewManNH said maybe try somethig else, I don't understand your concept of using strings of ini files.

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

3 hours ago, Xenobiologist said:

You could also encrypt the data with Autoit before writing it to the temp file.

Then read it again and decrypt it in your script.

🙂

Like BrewManNH said maybe try somethig else, I don't understand your concept of using strings of ini files.

that what i want a way to add a value to ini into string

i want to read the ini file using the FileRead function

and encript it internaly, after that i have a function to read the ini sections from a variable not from file

and i posted the udf on top

now i need the same way to add an ini section or value to variable not to ini file

after that i want to cript the data again and save it to file

 

Link to comment
Share on other sites

Again, it's an instance of the XY problem!

Hardly hard:

#include <SQLite.au3>

Local Const $SQLITE_DLL = "C:\SQLite\bin\System.Data.SQLite.dll" ;<-- Change to the location of your System.Data.SQLite dll

_SQLite_Startup($SQLITE_DLL, False, 1)
If @error Then Exit MsgBox($MB_ICONERROR, "SQLite Error", "Unable to start SQLite. Check existence of DLL")
Local $hDB = _SQLite_Open("inidata.sq3")

Local $sPass = InputBox("", "Enter inidata password", "", "*")
_SQLite_Exec($hDB, "pragma key='" & $sPass & "'")
$sPass = 0

Local $s
#cs
$s = "CREATE TABLE if not exists INI (" & _
        "Section CHAR NOT NULL COLLATE NOCASE, " & _
        "Key CHAR NOT NULL COLLATE NOCASE, " & _
        "Value CHAR DEFAULT '', " & _
        "PRIMARY KEY (Section COLLATE NOCASE, Key COLLATE NOCASE)) WITHOUT ROWID;"
_SQLite_Exec($hDB, $s)
#ce

Local $aRows, $iRows, $iCols
_SQLite_GetTable2d(-1, "select * from ini where section like 'mysection'", $aRows, $iRows, $iCols)
_SQLite_Display2DResult($aRows)
ConsoleWrite(@LF)

$s = "insert or ignore into ini values ('myothersection', 'newkey2', 'newvalue2')"
_SQLite_Exec($hDB, $s)

_SQLite_GetTable2d(-1, "select * from ini where section like 'myothersection'", $aRows, $iRows, $iCols)
_SQLite_Display2DResult($aRows)
ConsoleWrite(@LF)

_SQLite_GetTable2d(-1, "select * from ini", $aRows, $iRows, $iCols)
_SQLite_Display2DResult($aRows)
ConsoleWrite(@LF)

_SQLite_Close($hDB)
_SQLite_Shutdown()

Passphrase of the attached file is your pseudo.

inidata.sq3

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

you can just encrypt section&key&values then use iniwrite to write the encrypted string and for read use iniread to get the encrypted section&key&values then decrypt it to get the orignal data  ,example :-

#include <Crypt.au3>

_Crypt_Startup()

INI_Encrypt_Write("test.ini","1234","Testsection","TestKey","TestValue")
MsgBox(0,"", INI_Decrypt_Read("test.ini","1234","Testsection","TestKey",0))

func INI_Encrypt_Write( $filename,$password , $section,$key,$value)
    $section=_Crypt_EncryptData ( StringToBinary($section), $password, $CALG_AES_256)
    $key=_Crypt_EncryptData ( StringToBinary($key), $password, $CALG_AES_256)
    $value=_Crypt_EncryptData ( StringToBinary($value), $password, $CALG_AES_256)
    IniWrite($filename,$section,$key,$value)
endfunc

func INI_Decrypt_Read( $filename,$password , $section,$key,$default)
    $section=_Crypt_EncryptData ( StringToBinary($section), $password, $CALG_AES_256)
    $key=_Crypt_EncryptData ( StringToBinary($key), $password, $CALG_AES_256)
    $encryptedString=IniRead($filename,$section,$key,$default)
$decrypt=BinaryToString(_Crypt_DecryptData($encryptedString, $password, $CALG_AES_256))
return $decrypt
endfunc


_Crypt_Shutdown()

here is the test.ini
 

[0x9C8DC54F9391114C9168053C313A58AB]
0x2C30CEBB1003D32EE8B2B45BEE3AA723=0x9C0E57198A990F1D35B6A54136E0405E

BTW :- i agree with every one that ini is not  recommended .

Edited by Network_Guy
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...