encrypt and decrypt pointer

Hello ,

How to encrypt and decrypt pointer [ 99DE27 ] And save the result in ini file
Last edited on
The question doesn't make much sense as it stands.

A pointer will point to a block of memory, and you probably want to encrypt the block that is pointed to, not the pointer itself. You'll also need to know the size of the block.

Once you have the data to be encrypted, then you can apply one of the many encryption algorithms.
Thanks for replay
i only need encrypt pointer not block
A pointer is just an address, just a number. Use reinterpret_cast to convert it to an integer type and encrypt that.
Nice please can give me example by c++ ?
Yup, this totally isn't an XY problem. No siree!

encrypt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Example program
#include <iostream>
#include <fstream>

int main()
{
	if constexpr (sizeof(int) == sizeof(int*))
	{
		// (branch not tested)
		int* ptr = reinterpret_cast<int*>(0x99DE27);
		int im_a_ptr_trust_me = reinterpret_cast<int>(ptr);
		im_a_ptr_trust_me ^= 0xCF7237;

		std::ofstream fout("innie.ini");		
		fout << im_a_ptr_trust_me;	
	}
	else
	{
		static_assert(sizeof(long long) == sizeof(long long*));
		
		long long* ptr = reinterpret_cast<long long*>(0x99DE27);
		long long im_a_ptr_trust_me = reinterpret_cast<long long>(ptr);
		im_a_ptr_trust_me ^= 0xCF7237; // randomly generated by random.org

		std::ofstream fout("innie.ini");		
		fout << im_a_ptr_trust_me;
	}
}


decrypt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Example program
#include <iostream>
#include <fstream>

int main()
{
	if constexpr (sizeof(int) == sizeof(int*))
	{
		// (branch not tested)
		std::ifstream fin("innie.ini");		
		
		int im_a_ptr_trust_me;
		if (!(fin >> im_a_ptr_trust_me))
		{
			std::cout << "Error reading file\n";
			return 1;
		}

		im_a_ptr_trust_me ^= 0xCF7237;

		int* ptr = reinterpret_cast<int*>(im_a_ptr_trust_me);
		
		std::cout << ptr << '\n';
	}
	else
	{
		static_assert(sizeof(long long) == sizeof(long long*));
		
		std::ifstream fin("innie.ini");		
		
		long long im_a_ptr_trust_me;
		if (!(fin >> im_a_ptr_trust_me))
		{
			std::cout << "Error reading file\n";
			return 1;
		}

		im_a_ptr_trust_me ^= 0xCF7237;

		long long* ptr = reinterpret_cast<long long*>(im_a_ptr_trust_me);
		
		std::cout << ptr << '\n';
	}
}
Last edited on
thanks so much Ganado

i got this error :|19|error: static assertion failed|
Uh what compiler are you using? What does this print for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main()
{
    cout << "int: " << sizeof(int) << '\n';
    cout << "long: " << sizeof(long) << '\n';
    cout << "long long: " << sizeof(long long) << '\n';
    
    cout << "int*: " << sizeof(int*) << '\n';
    cout << "long*: " << sizeof(long*) << '\n';
    cout << "long long*: " << sizeof(long long*) << '\n';
    cout << "char*: " << sizeof(char*) << '\n';
}
We should be teaching you how to fish, not fishing for you.
the problem is easy enough. you can do the same with <random> and a little more code to set that up.

1
2
3
4
5
6
7
8
9
int password = 1234; //get any string from user, convert to int somehow if you want to get fancy.
int x;
int * ip = &x;
srand(password);
uint64_t enc = (uint64_t)(ip)^rand();
file << enc;
decrypt:
srand(password);
ip = (int*)(enc^rand());

but pointers don't survive across executions. if you save a number to a file, exit the program, restart the program, load the pointer, and try to use it, it will NOT be a valid pointer most of the time. It could work on a fully deterministic embedded system with a very simple OS and a very regimented program execution cycle such that your program always loaded to the same memory each time, but generally speaking, this is nonsense.
Last edited on
Thanks for reply Ganado

Uh what compiler are you using?
i use this code
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <typeinfo>

int main()
{
    if (__cplusplus == 201703L) std::cout << "C++17\n";
    else if (__cplusplus == 201402L) std::cout << "C++14\n";
    else if (__cplusplus == 201103L) std::cout << "C++11\n";
    else if (__cplusplus == 199711L) std::cout << "C++98\n";
    else std::cout << "pre-standard C++\n";

}


Output: C++14

1
2
C:\MinGW\bin>gcc --version
gcc (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0



What does this print for you
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
int main()
{
    cout << "int: " << sizeof(int) << '\n';
    cout << "long: " << sizeof(long) << '\n';
    cout << "long long: " << sizeof(long long) << '\n';
    
    cout << "int*: " << sizeof(int*) << '\n';
    cout << "long*: " << sizeof(long*) << '\n';
    cout << "long long*: " << sizeof(long long*) << '\n';
    cout << "char*: " << sizeof(char*) << '\n';
}


1
2
3
4
5
6
7
int: 4
long: 4
long long: 8
int*: 4
long*: 4
long long*: 4
char*: 4
Thanks for reply kbw , ok i will learn to fish
Instead of long long try std::[u]intptr_t, systems with the ILP32 data model still have a 64-bit long long.
Last edited on
We should be teaching you how to fish, not fishing for you.
Encrypting a pointer makes no sense. For dumb stuff like this that clearly isn't useful, it's funner to just try out the wacky suggestion.

Instead of long long try std::[u]intptr_t, systems with ILP32 data model still have 64-bit long long.
Great suggestion, I was looking for something like that.

Hawlong wrote:
int: 4
long: 4
long long: 8
int*: 4

Weird, if int == int* size, then it shouldn't be hitting the static_assert to begin with. But moot point since mbozzi's suggestion makes more sense.
Last edited on
Thanks for reply jonnin
your example is working , my program auto get game pointer and get other info and save it in ini file ,This information is used by another program , I just don't want anyone to read what's inside the file ,That's why I wanted to encrypt the pointer
thanks for reply mbozzi

you mean i chnage this code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Example program
#include <iostream>
#include <fstream>

int main()
{
	if constexpr (sizeof(int) == sizeof(int*))
	{
		// (branch not tested)
		int* ptr = reinterpret_cast<int*>(0x99DE27);
		int im_a_ptr_trust_me = reinterpret_cast<int>(ptr);
		im_a_ptr_trust_me ^= 0xCF7237;

		std::ofstream fout("innie.ini");		
		fout << im_a_ptr_trust_me;	
	}
	else
	{
		static_assert(sizeof(long long) == sizeof(long long*));
		
		long long* ptr = reinterpret_cast<long long*>(0x99DE27);
		long long im_a_ptr_trust_me = reinterpret_cast<long long>(ptr);
		im_a_ptr_trust_me ^= 0xCF7237; // randomly generated by random.org

		std::ofstream fout("innie.ini");		
		fout << im_a_ptr_trust_me;
	}
}


to this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Example program
#include <iostream>
#include <fstream>

int main()
{

	if constexpr (sizeof(int) == sizeof(int*))
	{
		// (branch not tested)
		int* ptr = reinterpret_cast<int*>(0x99DE27);
		int im_a_ptr_trust_me = reinterpret_cast<int>(ptr);
		im_a_ptr_trust_me ^= 0xCF7237;

		std::ofstream fout("innie.ini");
		fout << im_a_ptr_trust_me;
	}
	else
	{

		static_assert(sizeof( std::intptr_t) == sizeof( std::intptr_t*));

		intptr_t* ptr = reinterpret_cast< std::intptr_t*>(0x99DE27);
		intptr_t im_a_ptr_trust_me = reinterpret_cast< std::intptr_t>(ptr);
		im_a_ptr_trust_me ^= 0xCF7237; // randomly generated by random.org

		std::ofstream fout("innie.ini");
		fout << im_a_ptr_trust_me;
	}
}
Glad it helps. Fair warning that xor encrypts are easy to hack if someone gets serious about breaking in, esp if its just off the same constant each time.
The fact that you're storing the pointer in an INI file suggests that you want to read it the next time you run the program. Or that you want to read it from another program. Neither one of those things will work.

When you run a program and it puts an object at address X, there's no guarantee that it will put the object at that same address the next time it runs. Chances are pretty slim in fact. If you modify the program and recompile it, chances drop from slim to preposterously low.

If you want to read that pointer into another program, you need to be aware that the other program can't access that address and get the same data, at least not on most computers. Programs run in different "address spaces." This is to protect them from each other.

As Gando said, this is an XY problem. You're asking about a proposed solution to your problem rather than describing the problem itself. Whatever you're trying to accomplish, there's probably a better way to do it.
Thank you all for your help Thank you Ganado
Topic archived. No new replies allowed.