Click here to Skip to main content
15,886,574 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have this:

C++
freopen("CONOUT$", "w", stdout);

	std::string Filename = "test.dat";
	std::ifstream input(Filename, std::ios::binary | ios::in);  // Open the file
	
	std::string line;                                           // Temp variable
	std::vector<std::string> lines;                             // Vector for holding all lines in the file
	while (std::getline(input, line))                           // Read lines as long as the file is
	{
	
	lines.push_back(encryptDecrypt(line));
	
	}

	CIniReader iniReader("test.dat");
	char *myip = iniReader.ReadString("test", "1", "127.0.1.9");
	
	for (auto s : lines)                                         // For each line in vector
	{
		
		cout << s;                                              // Print it
		cout << endl;                                           // new line
	}


test.dat is XOR-ed, and i can decript it ok, being able to read line by line, but what i want is to access certain sections of that data, stored ini style of course:

C++
[test]
1 =
2 = 
3 = 
[test2]
1 = 
2 =
3 =


I am a begginer, and i have no idea on how to do this... help please?
Posted

First regarding the Windows API (GetPrivateProfile-s and WritePrivateProfile-s functions), I personally wouldn't recommend anyone to use them. These are old, deprecated and buggy functions that exist today purely because of compatibility with 16-bit Windows-based applications (read these functions "Note" ...).

Now regarding your requirements, there are quite a few libraries capable of processing INI file's content, you just need to search for them.
For example have you tried the following:
https://github.com/brofield/simpleini[^]
But I'm not sure if there is any INI library that provides encryption or decryption, so after you choose the library you would need to make a sort of a wrapper functions that would decrypt the whole content before passing it to the chosen INI library and encrypt the whole content provided from the chosen INI library.

Actually I have an INI library that can encrypt/decrypt and manipulate with INI files:
https://github.com/MarioZ/MadMilkman.Ini[^]
But it is a .NET library so I'm afraid it want suite your needs, unless you are using C++/CLI.
 
Share this answer
 
Comments
[no name] 3-Jun-15 6:16am    
I an using CIniReader from github, as for encryption i was using my custom XOR, but i will see yours now and test! And i use c++. THANKS!
[no name] 3-Jun-15 6:18am    
amazing! it`s like you have done all the work for me :)) anyway i need to test the encryption...
[no name] 3-Jun-15 6:52am    
Mario Z - This can be cracked with the less effort... :(
Mario Z 3-Jun-15 7:44am    
What do you mean, with less effort?
It uses a Rijndael encryption algorithm.

It is true that I use a hard-coded "Salt" which is not recommended, but I've done this on purpose in order to simplify the API for other programmers, but you can easily adjust this library in order to provide both "Password" and "Salt", that will be used for encrypting, in the same manner.

This would require adjusting only this code. So instead of using the private static readonly byte[] Salt you would need to provide your own just like the string passwordPhrase is provided.
I would rather use Windows API (see, for instance, "GetPrivateProfileInt function"[^] as opposed to hand craft parsing for retrieving entries of a INI file.
 
Share this answer
 
Comments
[no name] 3-Jun-15 4:21am    
@CPallini - can you understand what i need?
CPallini 3-Jun-15 4:54am    
No, of course I cannot.
I insist you'd better use Windows API, (you may save the 'decrypted' content on a temporary file, for the purpose).
The other way, namely parsing yourself the INI content, is, of course, feasible, but would require far more work.
[no name] 3-Jun-15 4:56am    
Well that`s why i am here, i am a noob begginer, whereas here stand pro-coders...
I must use this encryption to protect the content of my file, and it must be .ini style...
CPallini 3-Jun-15 5:04am    
So what's the problem? As I already suggested, you might decrypt the file, write the plain content to a temporary one and use Windows API for reading entries.
If you don't want to do what suggested then you need to hand craft a (simple) parser.
[no name] 3-Jun-15 5:06am    
Problem is, that i have no idea on how to do that? it was in my mind, but i found no examples of so on the google... i am here to get some examples/solutions...
Take a look at this Project: A Small Class to Read INI File[^]

There is an example of how to do exactly what you need:
C++
#include "iostream"
#include "IniWriter.h"
#include "IniReader.h"
int main(int argc, char * argv[])
{
 CIniWriter iniWriter(".\\Logger.ini");
 iniWriter.WriteString("Setting", "Name", "jianxx");   
 iniWriter.WriteInteger("Setting", "Age", 27); 
 iniWriter.WriteFloat("Setting", "Height", 1.82f); 
 iniWriter.WriteBoolean("Setting", "Marriage", false);  
 CIniReader iniReader(".\\Logger.ini");
 char *szName = iniReader.ReadString("Setting", "Name", "");   
 int iAge = iniReader.ReadInteger("Setting", "Age", 25); 
 float fltHieght = iniReader.ReadFloat("Setting", "Height", 1.80f); 
 bool bMarriage = iniReader.ReadBoolean("Setting", "Marriage", true); 
 
 std::cout<<"Name:"<<szName<<std::endl
   <<"Age:"<<iAge<<std::endl 
   <<"Height:"<<fltHieght<<std::endl 
   <<"Marriage:"<<bMarriage<<std::endl; 
 delete szName;  
 return 1;   
}


In your case you will do something like:
C++
CIniReader iniReader("test.dat");
char *szName = iniReader.ReadString("test", "1", "");   
 
Share this answer
 
v3
Comments
[no name] 3-Jun-15 4:30am    
Very good, thank you, but still, if my test.dat that contains the .INI values is encrypted, where do i call the encryptDecrypt function? because if i call test.dat to read it, it will show only wierds chars(of course)... function encryptDecrypt is string.
Leo Chapiro 3-Jun-15 4:37am    
You need to call encryptDecrypt AFTER iniReader.ReadString like this e.g.: char *szName = encryptDecrypt(iniReader.ReadString("test", "1", ""));
[no name] 3-Jun-15 4:46am    
Ok, i had to change char * to string because was giving me error, but now it compiles, but no ouput, like it`s not reading anything... :(
Leo Chapiro 3-Jun-15 4:58am    
But you have nothing in your example, haven't you: "1 =" ! Write something like: "1 = test1"
[no name] 3-Jun-15 4:59am    
well in the real file i have content: 1 = 127.0.0.1
Try using registry class to store your encrypted data in a binary key.
This link[^] give also same examples.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900