-
Using this XOR encryption in C++
Hello,
I am currently using a byte encryption in C# which gets decrypted in my C++ client.
It is XOR, but I can't get it to work. Is there anything that Im doing wrong?
I already wrote the C++ XOR in C#, which look pretty much the same.
C#
Code:
public byte[] XOR(byte[] strng, byte[] key)
{
int string_len = strng.Length;
int key_length = key.Length;
int i, position;
for (i = 0; i < string_len; i++)
{
position = i % key_length;
strng[i] = Convert.ToByte(strng[i] ^ key[position]);
}
return strng;
}
C++
Code:
char *encrypt(char *string, char *key)
{
int string_len = strlen(string);
int key_length = strlen(key);
int i,position;
for(i = 0; i < string_len; i++)
{
position = i % key_length;
string[i] = (char)((int)string[i] ^ (int)key[position]);
}
return string;
}
In C# the bytes get encrypted like:
Code:
byte[] bytestocrypt = //here my source of the bytes
byte[] b = new byte[] { 4, 5, 3, 0 };
bytestocrypt = XOR(bytestocrypt, b);
in C++
Code:
LPVOID pBuffer = //here source of decrypted bytes
char b[4] = {4, 5, 3, 0};
char *decrypted = encrypt((char *)pBuffer, b);
pBuffer = decrypted;
Thanks in advance.
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Hello,
I am currently using a byte encryption in C# which gets decrypted in my C++ client.
It is XOR, but I can't get it to work. Is there anything that Im doing wrong?
I already wrote the C++ XOR in C#, which look pretty much the same.
Pointers are not arrays. The code you wrote is not the same as the C# code. The C# code actually uses arrays. Pointers are just that in C++ -- pointers. They are not arrays (although you use pointers to point to the beginning of arrays). You can pass arrays to C# and they remain arrays, not so in C++, so the real solution is to use constructs that maintain their "arrayness" when passed around.
Second, post the main() program. Your attempt of using pointers is highly dependent on how you called that function. For example, this tells us nothing:
Code:
LPVOID pBuffer = //here source of decrypted bytes
Everything, and I mean everything depends on how you initialized that pBuffer.
Third, you posted another question concerning this, and my response to you is the same -- ditch the pointers and use classes. Your C++ code not only is not the same, it is dangerous as heck. I can easily break it like this:
Code:
char *encrypt(char *string, char *key);
//..
encrypt("abc123", "123456"); // writing to a string literal as the first argument -- possible crash
encrypt(NULL, NULL); // calling strlen() on a NULL pointer -- undefined behaviour
Both of those lines compile. Now what happens when you run it? The first one has a string-literal as the first argument. The second has a NULL as the first argument. Providing those values as arguments makes your encrypt function go haywire.
I could go on and on about how that function is crash-prone. You're using C++, not 'C' -- there is absolutely no excuse to make crash-prone functions like this. To make it more bullet-proof, use what I mentioned in the other thread, and that is container classes.
Code:
#include <vector>
typedef std::vector<char> ByteArray;
//...
ByteArray& XOR(ByteArray& strng, const ByteArray& key)
{
size_t string_len = strng.size();
size_t key_length = key.size();
int position;
for (size_t i = 0; i < string_len; i++)
{
position = i % key_length;
strng[i] = (ByteArray::value_type)(strng[i] ^ key[position]);
}
return strng;
}
This code is much more safer to use. Also, here is a radically different way in C++ to do the same code.
Code:
#include <algorithm>
#include <vector>
typedef std::vector<char> ByteArray;
struct XORTransformer
{
int curPos, m_size;
const ByteArray& m_key;
XORTransformer(const ByteArray& key) : m_key(key), curPos(-1), m_size(key.size()) { }
ByteArray::value_type operator()(ByteArray::value_type val)
{
++curPos;
return (ByteArray::value_type)(val ^ m_key[curPos % m_size]);
}
};
template <typename Transformer>
ByteArray& Transform(ByteArray& strng, const ByteArray& key)
{
std::transform(strng.begin(), strng.end(), strng.begin(), Transformer(key));
return strng;
}
int main()
{
char bytes[] = "xabcde54";
char keys[] = "12345678";
ByteArray key(keys, keys + sizeof(keys) / sizeof(keys[0]));
ByteArray barr(bytes, bytes + sizeof(bytes)/sizeof(bytes[0]));
Transform<XORTransformer>(barr, key);
}
The utility of doing it this way may not be apparant, but basically, I created a function where you can plug-in the transformation you want to use. If you come up with another way to transform the source string, just call:
Code:
Transform<AnotherWay>(barr, key);
Where AnotherWay is a class that handles the key and transforms the source string in whatever way it sees fit.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Thank you for your reply.
That is indeed right, I could not get the other version working. So this time, instead of translating C# to C++, I chose a snippet which was posted online.
I translated this into C# because that is the language that I normally use to code, and understand.
Your solution seems to be right, but I would like to use an algorithm instead of these libraries to XOR.
This XOR snippet was posted online and seemed to work for the people.
And thanks for pointing out the problems.
---
Btw I used your above snippet and there still seems to be an error on the first line, passing the first argument.
And the source of the Buffer is just a bytearray from RC DATA embedded resources. (FindResource(0, "0", "RT_RCDATA");
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Your solution seems to be right, but I would like to use an algorithm instead of these libraries to XOR.
Where did I not use the algorithm you posted? It's in both the examples I posted -- you just don't recognize it.
Quote:
Btw I used your above snippet and there still seems to be an error on the first line, passing the first argument.
The full example I posted works correctly. I don't know what you're doing or what data you're using to have such an error.
Quote:
And the source of the Buffer is just a bytearray from RC DATA embedded resources. (FindResource(0, "0", "RT_RCDATA");
OK, so what's the problem? You can take any byte array and place it in the vector.
That's why I mentioned to you in the other thread to learn the interface of the vector class. If you did that, you wouldn't see that there is any problem.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
Paul McKenzie
Where did I not use the algorithm you posted? It's in both the examples I posted -- you just don't recognize it.
Sorry I did recognize it of course, but I just wanted to say that the second example that you coded was not an option for me.
Well if it is working for you, then I will be making a mistake?
This is the full code that I am talking about.
Code:
#include <windows.h>
#include "extra.h"
#include <vector>
typedef std::vector<char> ByteArray;
HINSTANCE instance;
WPARAM wParam ;
ByteArray& XOR(ByteArray& strng, const ByteArray& key)
{
size_t string_len = strng.size();
size_t key_length = key.size();
int position;
for (size_t i = 0; i < string_len; i++)
{
position = i % key_length;
strng[i] = (ByteArray::value_type)(strng[i] ^ key[position]);
}
return strng;
}
LPVOID noel()
{
LPVOID pBuffer = NULL;
DWORD dwSize;
LPBYTE lpData;
HRSRC hRsrc = FindResource(0, "0", "RT_RCDATA");
HGLOBAL hGlobal = LoadResource(0, hRsrc);
dwSize = SizeofResource(0, hRsrc);
lpData = (LPBYTE)LockResource(hGlobal);
pBuffer = lpData;
char b[4] = {4, 5, 3, 0};
char decrypted[] = XOR(pBuffer, b);
pBuffer = decrypted;
return pBuffer;
}
Maybe you can see the error?
|45|error: invalid initialization of reference of type 'ByteArray& {aka std::vector<char>&}' from expression of type 'char*'|
|13|error: in passing argument 1 of 'ByteArray& XOR(ByteArray&, const ByteArray&)'|
Could be beginners mistake , excuse me for that.
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Sorry I did recognize it of course, but I just wanted to say that the second example that you coded was not an option for me.
Well, it is the same as the first example, except it's wrtten in a much more flexible way.
Quote:
Well if it is working for you, then I will be making a mistake?
This is the full code that I am talking about.
Even if you didn't use ByteArray, there are problems with your code.
First, All of those Windows API calls are off-topic in this forum. Assume I know nothing about them or what they do.
Code:
LPVOID pBuffer = NULL;
//...
pBuffer = lpData;
So is pBuffer NULL terminated array of char? If not, then your original XOR function using char* would not have worked properly, since calling strlen() on data you aren't sure is NULL terminated leads to strlen() going off the deep-end trying to find the NULL.
If it is NULL terminated, then you need to create ByteArray's from this data. You can't just send it to the function, as raw char data is not a ByteArray. Remember that ByteArray is a vector -- a full-blown class that wraps an array, and isn't just an array. You have to get the raw data into the ByteArray object somehow. That's why I mentioned to you to study the interface of vector, so that you know how to convert from a raw type to a vector:
Code:
pBuffer = lpData;
ByteArray b1(pBuffer, pBuffer + strlen(pBuffer));
ByteArray b2(b, b + sizeof(b));
XOR(b1, b2);
Then you use b1 and b2 from there, as pBuffer is no longer needed at that point.
There is a constructor for vector that takes two arguments, where both arguments are iterators to starting and beginning of the sequence of values. In the example, you want to create a vector that consists of characters from pBuffer and from b.
The b1 vector is created by supplying the start of pBuffer and the end of pBuffer. The same with b2 -- the start of b and the end of b are provided. Then presto, you have the vector of characters.
Again, I'm assuming you know that pBuffer is NULL terminated. If it isn't, then you need to know the number of bytes so as to set up the b1 vector correctly (you can't use strlen()).
Code:
ByteArray b1(pBuffer, pBuffer + whatever_number_of_bytes);
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
Paul McKenzie
...
Nice design, but your XORFunction object carries a variable state, which makes it implicitly dangerous, especially when mixed with the STL (transform does not necessarily guarantee the range will be transformed in-order if you provide it with a random access iterator, for example).
IMO, the Encoder should take an entire input range directly anyways, as some encoders will need the entire input to function correctly anyways.
About this line:
Code:
template <typename Transformer>
ByteArray& Transform(ByteArray& strng, const ByteArray& key)
Did you purposefully omit to pass a Transformer object? I suppose it was to prevent the re-use of the XORFunction (which would be problematic, given the whole state thing)?
Function-like parameters parameters are usually also passed as an argument, in case they can be parametrized (as far as I have seen anyways). Plus, it avoids having to specify the parameterization of the function when calling it.
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
monarch_dodra
Nice design, but your XORFunction object carries a variable state, which makes it implicitly dangerous, especially when mixed with the STL (transform does not necessarily guarantee the range will be transformed in-order if you provide it with a random access iterator, for example).
True. I was assuming the iterator was random access and did an in-order traversal (from begin() to end() in that order).
Code:
template <typename Transformer>
ByteArray& Transform(ByteArray& strng, const ByteArray& key)
Quote:
Did you purposefully omit to pass a Transformer object? I suppose it was to prevent the re-use of the XORFunction (which would be problematic, given the whole state thing)?
Not on purpose, no. I could have rewritten it to take a Transformer as a parameter also. I was thinking more of the lines of a policy-based design.
All you're saying is true however.
Edit:
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Hmm alright thanks. Ill read into that vectors and such.
But the thing that I dont get is why even a Char encryption fails for me. I must be doing something wrong.
I did a simple test with a Char array by Xoring it twice (which would result in the input)
Code:
char *XOR(char *str, char *cipher)
{
for (size_t i = 0; i < strlen(str); i++)
{
str[i] ^= cipher[i % strlen(cipher)];
}
return str;
}
Code:
char *CPA = XOR((char*)"CreateProcess", (char*)"Sddd");
CPA = XOR(CPA, (char*)"Sddd");
This failed too, the CPA output was not the original that I entered. Pretty much tried with alot of different XOR encryptions and variations.
Failed in all for some reason.
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
But the thing that I dont get is why even a Char encryption fails for me. I must be doing something wrong.
I already mentioned what you're doing wrong:
Code:
char *XOR(char *str, char *cipher)
{
for (size_t i = 0; i < strlen(str); i++)
{
str[i] ^= cipher[i % strlen(cipher)];
}
return str;
}
Code:
char *CPA = XOR((char*)"CreateProcess", (char*)"Sddd");
CPA = XOR(CPA, (char*)"Sddd");
Didn't I already mention that you cannot write on a string-literal? You are passing a string-literal, and writing to a string-literal is undefined behaviour. All of that casting doesn't turn a string-literal into something else.
You must pass a writable buffer, i.e. a true array of chars, not a quoted string-literal.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
Paul McKenzie
You must pass a writable buffer, i.e. a true array of chars, not a quoted string-literal.
Regards,
Paul McKenzie
Oh I thought the string woulc become an array if I added (char*).
Like
arraybit[0] = C
arraybit[1] = r
Then how do you suggest to encrypt that string with a XORencryption?
Regards,
-
Re: Using this XOR encryption in C++
If you want quick proof of how dangerous this is:
Code:
int main()
{
char *p = "abc123";
p[0] = 'x';
}
What do you think will happen when you run this program? Most of the time, the program will crash as soon as you change p[0]. Maybe it won't crash, and you have the false sense that the program is OK when it really isn't. This is what your first call to XOR was doing.
This is what I mean by writing to a string-literal as invoking undefined behaviour. Unlike languages such as C#, where if you do something illegal, you always get the runtime to let you know, C++ doesn't work that way. You make a mistake like this, and you don't know what will happen. Maybe it will work, maybe it will crash, maybe it works on your machine but crash on another, etc.
The following is the correction:
Code:
int main()
{
char p[] = "abc123";
p[0] = 'x';
}
This program now is perfectly fine. See the difference?
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Then how do you suggest to encrypt that string with a XORencryption?
You must provide a writable buffer, an actual char array for example. You were not doing that in your attempt.
That's the danger of doing this, and why I mentioned back in my second post on this thread the many ways I can break your code. You still didn't address the NULL first argument -- that hole is still there.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Uhm yes I see the difference, but I have no idea why it would be dangerous.
Are you saying that the difference between
char p[] and char *p is that char p[] is really a char array while the first one is a string-literal and not writable?
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Uhm yes I see the difference, but I have no idea why it would be dangerous.
Are you saying that the difference between
char p[] and char *p is that char p[] is really a char array while the first one is a string-literal and not writable?
Yes. Why not try the small program and see what happens? Depending on the compiler you're using, the code that uses the string-literal will immediately crash on the assignment to p[0].
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Oh finallly got the difference now then, thanks. Indeed also on stackoverflow a good example. The first one is in the read-only memory without copying the string to the newly allocated memory.
So next step would be
Code:
char *XOR(char str[], char cipher[])
{
for (size_t i = 0; i < strlen(str); i++)
{
str[i] ^= cipher[i % strlen(cipher)];
}
return str;
}
Not sure though if the call stays the same, as that is still unwritable and does not need to be writable. so just kept the (char *) there.
Code:
char *CPA = XOR((char *)"CreateProcessA", (char *)"Sddd");
Ill Google now to see what you mean by the NULL first argument.
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
So next step would be
Code:
char *XOR(char str[], char cipher[])
{
for (size_t i = 0; i < strlen(str); i++)
{
str[i] ^= cipher[i % strlen(cipher)];
}
return str;
}
No. Still wrong.
I also mentioned this to you on the other thread. This
Code:
char *XOR(char str[], char cipher[])
is exactly the same as this:
Code:
char *XOR(char *str, char *cipher)
So you haven't changed a thing.
Quote:
Ill Google now to see what you mean by the NULL first argument.
You are calling strlen(NULL) in your function if NULL is the first argument. There is no guarantee how strlen() behaves if a NULL is passed, as strlen() doesn't check for NULL pointers. All strlen() does is start at the address you give it, and search for a '\0' character.
A NULL address is not the same thing as a '\0' character.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
The bottom line is this:
In your function, you're assigning a value to str, which is the first parameter. You are passing an unassignable string-literal as the first parameter. Stop passing string literals and instead pass the address of the first element of a true char array.
C++ is not C#, where you get all kinds of warning bells and flags going off when you make a mistake. You are basically on your own, and C++ allows you to shoot yourself in the foot if you want to.
I don't think you're aware of these things -- in C++, just because something compiles doesn't mean it is a well-formed program, and the only issue is that the logic may be off. Nothing could be further from the truth -- you can create ill-formed programs that compile that break all sorts of rules (writing to string literals, accessing array elements out of bounds, etc.), thus the behaviour of such programs is -- you don't know.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Quote:
You are calling strlen(NULL) in your function if NULL is the first argument. There is no guarantee how strlen() behaves if a NULL is passed, as strlen() doesn't check for NULL pointers. All strlen() does is start at the address you give it, and search for a '\0' character.
A NULL address is not the same thing as a '\0' character.
Regards,
Paul McKenzie
But my first argument will not be NULL, so I dont see the problem in this.
-
Re: Using this XOR encryption in C++
Yes, I get that point now. C++ differs alot from C# as I see now.
Quote:
I also mentioned this to you on the other thread. This
Code:
char *XOR(char str[], char cipher[])
is exactly the same as this:
Code:
char *XOR(char *str, char *cipher)
"Stop passing string literals and instead pass the address of the first element of a true char array."
Then I really don't know what Im doing wrong.. and how I should do it. Could you give an example?
Im getting confused I think.
Thanks
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
But my first argument will not be NULL, so I dont see the problem in this.
And if I use your function and pass a NULL, it breaks.
What if in another (larger) program, the string you're passing it is generated or computed at runtime (in other words, you have no way to know in advance what the string will be), and that generation produces a NULL. You then blindly pass it to your XOR function. What then?
The point being that a function should be written so that simple things like this won't break it. When you write a function, the underlying reason why you write it is that it can be reused in other programs and other situations, not just the situation you find yourself in now.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Then I really don't know what Im doing wrong.. and how I should do it. Could you give an example?
Again, don't pass a string literal as the first argument. I don't know how I can make it any simpler than that:
Code:
char p[10] = "abc123"; // a real array of char
XOR(p, "whatever");
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
You are really a 'man of the rules'. I appreciate that you are that strict.
I get your point. Although, I was just using this in a single small program in which im sure the first argument is not a Null.
I could easily add an if statement so it leaves the void or returns any other array and doesn't go in the loop.
Edit: removed and reading the post above.
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
Paul McKenzie
Again, don't pass a string literal as the first argument. I don't know how I can make it any simpler than that:
Code:
char p[10] = "abc123"; // a real array of char
XOR(p, "whatever");
Regards,
Paul McKenzie
Yes, but that gives a problem too..
Like a small test:
Quote:
char CPA[15] = "CreateProcessA"; // a real array of char
CPA = XOR(CPA, "whatever");
CPA = XOR(CPA, "whatever");
This is not possible because XOR returns char *
The compiler also gives two erros
"incompatible types in assignment of 'char*' to 'char [15]'|"
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Yes, but that gives a problem too..
Like a small test:
This is not possible because XOR returns char *
The compiler also gives two erros
"incompatible types in assignment of 'char*' to 'char [15]'|"
So assign it to a char*.
Code:
char *returnValue = XOR(CPA, "whatever");
Which comes back full circle in why using classes that wrap arrays and pointers removes all of these headaches.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
It indeed really gives a headache.
But then again it fails.. because the input is again a returnvalue which is not declared like CPA[15]. if the return is not a real char array.
But I think it is, and the code below should have worked.
Quote:
char CPA[15] = "CreateProcessA"; // a real array of char
char *returnValue = XOR(CPA, (char *)"whatever");
returnValue = XOR(returnValue, (char *)"whatever");
This didn't fix it either.
Quote:
char CPA[15] = "CreateProcessA"; // a real array of char
char *returnValue = XOR(CPA, (char *)"whatever");
char CPA2[15] = returnValue;
returnValue = XOR(CPA2, (char *)"whatever");
-
Re: Using this XOR encryption in C++
But then again it fails.. because the input is again a returnvalue which is not declared like CPA[15]. if the return is not a real char array.
But I think it is, and the code below should have worked.
It should work, because you pass CPA and then return it, so returnValue points to CPA[0]. You did not really need returnValue to begin with as you could just reuse CPA.
That said, stop casting away const-ness. You should have written:
Code:
XOR(CPA, "whatever");
Then the function declaration should be:
Code:
char* XOR(char str[], const char cipher[]);
-
Re: Using this XOR encryption in C++
Laserlight I think you are wrong in that as I tried it earlier too on the previous page.
Using using CPA instead of the returnValue would give the error
error: incompatible types in assignment of 'char*' to 'char [15]'|
Changed it to const char now. Hope someone knows a solution for this.
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
...
The entire problem so far, when explained by Paul about "Literal" is one of "constness".
Unfortunately, for compatibility reasons, string literals in C++ are not declared const, but you are not allowed to modify them. That means there is nothing that will prevent you from trying to do it, and shooting yourself in the foot.
If you seriously want to get into C++, then you should really try to avoid such low level technical stuff, and start getting comfortable with the higher level stuff. The low level details will then flow naturally.
----
Here is a simple implementation, using basic return by value.
Code:
#include <iostream>
#include <vector>
std::string XOR(const std::string& sentence, const std::string& key)
{
std::string ret(sentence);
const size_t sentence_size = sentence.size();
const size_t key_size = key.size();
for (size_t i = 0; i<sentence_size; ++i)
{ret[i] ^= key[i % key_size];}
return ret;
}
int main()
{
std::string encoded = XOR("This is my secret sentence", "ANDthisISmyKEY");
std::cout << "Encoded sentence: " << encoded << std::endl;
std::cout << "Decoded sentence: " << XOR(encoded, "ANDthisISmyKEY") << std::endl;
}
The fact that the output is a different object than the input will mean a bit of overhead, but it also guarantees you don't hurt yourself...
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by qwerz
Using using CPA instead of the returnValue would give the error
error: incompatible types in assignment of 'char*' to 'char [15]'|
That is because you are trying to assign to an array. However, since you are changing the array using the pointer that is the first argument, you don't need to use the return value to begin with.
-
Re: Using this XOR encryption in C++
Thanks monarch_dotra, that did work.
But it uses iostream which adds about 400kb to my binsize. I prefer to not use that.
Am I right that a char array encryption is basically not possible without using a vector in C++?
I want to know the solution without vector if it is even possible.
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Thanks monarch_dotra, that did work.
But it uses iostream which adds about 400kb to my binsize. I prefer to not use that.
Where is iostream used in the function? The only thing in the XOR function is std::string, and that is not iostream.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Am I right that a char array encryption is basically not possible without using a vector in C++?
Eveyone has shown you what to do.
Forget about XOR for a moment -- what about any function that returns a char* and takes a char* and const char* as parameters? Do you think that no such function can ever exist? Of course not.
Code:
#include <cstring>
int main()
{
char p[100];
strcpy(p, "abc123");
char* returnValue = strcat(p, "This is concatenated");
}
So how is it that the strcat() seems to compile and work? It has the exact same signature as your XOR function.
http://www.cplusplus.com/reference/c...string/strcat/
So what makes it special? Absolutely nothing. The problem is that you seem to have trouble using such functions, and I don't know exactly what trouble you're having.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Quote:
Originally Posted by
qwerz
Thanks monarch_dotra, that did work.
But it uses iostream which adds about 400kb to my binsize. I prefer to not use that.
Code:
#include <string>
std::string XOR(const std::string& sentence, const std::string& key)
{
std::string ret(sentence);
const size_t sentence_size = sentence.size();
const size_t key_size = key.size();
for (size_t i = 0; i<sentence_size; ++i)
{ret[i] ^= key[i % key_size];}
return ret;
}
int main()
{
std::string encoded = XOR("This is my secret sentence", "ANDthisISmyKEY");
}
Compiled using Visual Studio 2010. The executable size for Release mode is 9,728 bytes, not 400k bytes.
Regards,
Paul McKenzie
-
Re: Using this XOR encryption in C++
Paul thanks for all your help and the others that responded here too. I finally got it now. It was lack of C++ knowledge I guess.
Btw I also found a way to encrypt bytearrays without the char vector that you used.
It is just by using an LPBYTE argument instead, and the XOR worked.
This thread could be closed if someone else does not need it.