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

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:

[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.
   
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.
   
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...
CPallini 3-Jun-15 5:07am
   
To do... What? Parsing?
[no name] 3-Jun-15 5:09am
   
Parsing or whatever works so i can access my values AFTER is decrypted, user must not be able to see this at all ...
CPallini 3-Jun-15 5:20am
   
Then parse it. You may use the string.find method to match, for instance, the square brackets in section parsing.
If you are using a modern C++ compiler then you may take advantage of the regex facilities.
Frankie-C 3-Jun-15 5:26am
   
Carlo I think that ini files cannot store the binary data that will come out from XOR crypting.
In my opinion him cannot use ini files (that are text files) to do what he wants unless the crypted data is not ASCII coded (i.e. using their hex representation).
A much better option would be to use registry...
Leo Chapiro 3-Jun-15 5:30am
   
You mean, "A much better option would be to use Windows Registry", don't you ?
Frankie-C 3-Jun-15 5:32am
   
Yess of course a typo...
CPallini 3-Jun-15 5:40am
   
Actually (in my comments) I suggested to decrypt it, store the plain contetnt on a temporary file and then use Winsows API with it.
[no name] 3-Jun-15 5:28am
   
Yes you are right Frankie-C, the file will not read at all if it is not stored ASCII ...
still no idea how to code -_-
[no name] 3-Jun-15 5:32am
   
LOL how can i use registry when a average user can get to it -_-
Frankie-C 3-Jun-15 5:43am
   
If you choose to use registry you can get advantage of binary keys and store there you're crypted data. The registry access is generally even more discouraging for unexpert users (for hackers ax Xor crypting is not such complex protection...)
[no name] 3-Jun-15 5:43am
   
CPallini - instead of writing 15 lines of the same thing you could have shown me an example... i get it, store in a TEMP file, but theory is not the same with practice
Frankie-C 3-Jun-15 5:56am
   
This is not the right way to answer. Be polite please.
CPallini 3-Jun-15 6:05am
   
Then... do practise!
[no name] 3-Jun-15 5:45am
   
Frankie, my XOR is tested, i use 512 multichar keys, on wich i apply two times...
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:
#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:
CIniReader iniReader("test.dat");
char *szName = iniReader.ReadString("test", "1", "");   
   
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.
   

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