Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[Dev] Xor

Joined
Apr 12, 2013
Messages
897
Reaction score
480
hey, i created in some freetime a little script in C# for Xoring

Code:
public static void Xor(string text, string key)        {
            string xored = "";
            int[] rofl = new int[text.Length + key.Length];
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            byte[] btext = enc.GetBytes(text);
            int i = 0;
            while (i < btext.Length)
            {
                if (i == key.Length)
                {
                    i = 0;
                }
                rofl[i] = btext[i] ^ key[i];
                //MsgBox.ShowSt(i.ToString());
                xored += rofl[i].ToString("X");
                i++;
            }
            MsgBox.ShowSt(xored);
        }

it's working but only if the key is longer than the text, do you have some suggestions etc?

AND btw: This is not a QQ i wanna Help Thread, it's for discussing etc
 
Skilled Illusionist
Joined
Jul 10, 2008
Messages
371
Reaction score
94
may be it's can help you:

Code:
QString XOR(QString value,QString key)
{
    QString retval(value);

    short unsigned int klen=key.length();
    short unsigned int vlen=value.length();
    short unsigned int k=0;
    short unsigned int v=0;

    for(;v<vlen;v++)
    {
        retval[v]=value[v].toLatin1()^key[k].toLatin1();
        k=(++k<klen?k:0);
    }

    return retval;
}

it's my c++ QT function for XOR encoding/decoding

I think you must place your if block after the i++; statement not at while block start (you will never meet requierment to reset you i variable.
 
Skilled Illusionist
Joined
Jul 10, 2008
Messages
371
Reaction score
94
Oo sorry bad reading of my part, you use the same counter to iterate your string and your key

so when you arrive to key longer you reset all your index...

see un my exemple I have two index (v and k)

k is used like index for the key and reset when k == myKeyLenght, v iterate from start to end of string.

sorry for my first post with my fast reading.
 
Newbie Spellweaver
Joined
May 23, 2012
Messages
73
Reaction score
42
My cod XOR on C++
//---------------------------------------------------------------------------

#pragma hdrstop

#include "XORCrypt.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
AnsiString XOR_Crypt(AnsiString Input, AnsiString Key)
{
AnsiString Out;
for (int i=1,k=1; i<=Input.Length(); ++i,++k)
{
if( k>Key.Length() ) k=1;

int dig = Input ^ Key[k];
Out += IntToHex(dig,2);
}
return Out;
}
//---------------------------------------------------------------------------
AnsiString XOR_Encrypt(AnsiString Input, AnsiString Key)
{
AnsiString Out;
for (int i=1,k=1; i<=Input.Length(); i+=2,++k)
{
if( k>Key.Length() ) k=1;

int dig = StrToInt ( "0x"+Input.SubString(i,2));
Out += char(dig ^ Key[k]);
}
return Out;
}
//---------------------------------------------------------------------------
 
Experienced Elementalist
Joined
Oct 9, 2012
Messages
226
Reaction score
76
Easy to find and dont need some 'QStrings' -.-

again i triple facepalmed and release that (again)

i guess 80% ragezoners does not know "from where the hell i have that" ...
here is reply for these questions: you dont know what you have, you should stand up turn around and go away

[header]
Code:
#define	_XOR_LIB_H_

#define SIZE_MAX_XOR_DATA_TO_STRING		500			
#define SIZE_MAX_XOR_KEY_STRING			1024		

class XOR
{
public:
	XOR();
	~XOR();


	static BOOL XOREncode(BYTE *o_pbyEncoded, BYTE *i_pbySource, int i_nSourceSize, char *i_szKeyString);
	static BOOL XOREncode(BYTE *o_pbyEncoded, char *i_szSource, char *i_szKeyString);
	static BOOL XORBinary2String(TCHAR *o_szEncodedString, BYTE *i_byBinaryData, int i_nDataSize);
	static BOOL XORString2Binary(BYTE *o_byBinaryData, char *o_szEncodedString);
	static BOOL XORDecrypt(char *o_pszOutputString, char *i_pszInputString, char *i_pKeyString);

private:

};

[Source]
Code:
#include "stdafx.h"
#include "XOR_Lib.h"



XOR::XOR()
{
}

XOR::~XOR()
{
}

BOOL XOR::XOREncode(BYTE *o_pbyEncoded, BYTE *i_pbySource, int i_nSourceSize, char *i_szKeyString)
{
	if(0 >= i_nSourceSize)
	{
		return FALSE;
	}

	int nKeyLen = strlen(i_szKeyString);
	if(SIZE_MAX_XOR_KEY_STRING < nKeyLen)
	{
		return FALSE;
	}

	int nROffset	= 0;
	int nWOffset	= 0;
	int nKeyOffset	= 0;
	if(4 > nKeyLen)
	{
		for(int i=0; i < i_nSourceSize; i++)
		{
			o_pbyEncoded[nWOffset] = i_pbySource[nROffset] ^ i_szKeyString[nKeyOffset];

			nWOffset++;
			nROffset++;
			nKeyOffset = (nKeyOffset+1)%nKeyLen;
		}
	}
	else
	{
		int nEndOffset;
		nEndOffset	= i_nSourceSize - i_nSourceSize%4;	
		nKeyLen		= nKeyLen - nKeyLen%4;				
		while(nROffset < nEndOffset)
		{
			*(DWORD*)(o_pbyEncoded+nWOffset) = *(DWORD*)(i_pbySource+nROffset) ^ *(DWORD*)(i_szKeyString+nKeyOffset);

			nWOffset	+= 4;
			nROffset	+= 4;
			nKeyOffset	= (nKeyOffset + 4) % nKeyLen;
		}

		for(int i=0; i < i_nSourceSize - nEndOffset; i++)
		{
			o_pbyEncoded[nWOffset] = i_pbySource[nROffset] ^ i_szKeyString[nKeyOffset];

			nWOffset++;
			nROffset++;
			nKeyOffset = (nKeyOffset+1) % nKeyLen;
		}		
	}
	
	return TRUE;
}

BOOL XOR::XOREncode(BYTE *o_pbyEncoded, char *i_szSource, char *i_szKeyString)
{
	return XOR::XOREncode(o_pbyEncoded, (BYTE*)i_szSource, strlen(i_szSource), i_szKeyString);
}

BOOL XOR::XORBinary2String(TCHAR *o_szEncodedString, BYTE *i_byBinaryData, int i_nDataSize)
{
	if(SIZE_MAX_XOR_DATA_TO_STRING < i_nDataSize)
	{
		return FALSE;
	}
	
	memset(o_szEncodedString, 0x00, 2*i_nDataSize); // 2007-10-24 by cmkwon, Output ąöĆŰ ĂʱâČ­
	for(int i=0; i < i_nDataSize; i++)
	{
		TCHAR szTemp[512];
		wsprintf(szTemp, L"%02X", i_byBinaryData[i]);
		_tcsncpy_s(o_szEncodedString, 1024, szTemp, 1024);  
	}
	return TRUE;
}

BOOL XOR::XORString2Binary(BYTE *o_byBinaryData, char *o_szEncodedString)
{
	int nStringLen = strlen(o_szEncodedString);
	if(SIZE_MAX_XOR_DATA_TO_STRING < nStringLen)
	{
		return FALSE;
	}

	memset(o_byBinaryData, 0x00, nStringLen/2);	
	for(int i=0; i < nStringLen/2; i++)
	{
		char szTemp[512];
		memset(szTemp, 0x00, 512);
		memcpy(szTemp, &o_szEncodedString[i*2], 2);
		o_byBinaryData[i] = (BYTE)strtol(szTemp, NULL, 16);
	}	
	return TRUE;
}


BOOL XOR::XORDecrypt(char *o_pszOutputString, char *i_pszInputString, char *i_pKeyString)
{
	BYTE byEncodedBinary[1024];	
	memset(byEncodedBinary, 0x00, 1024);
	if(FALSE == XOR::XORString2Binary(byEncodedBinary, i_pszInputString))
	{
		return FALSE;
	}

	return XOR::XOREncode((BYTE*)o_pszOutputString, byEncodedBinary, strlen(i_pszInputString)/2, i_pKeyString);
}
 
Skilled Illusionist
Joined
Jul 10, 2008
Messages
371
Reaction score
94
I'm a Qt dev So I use this for made my tools.

for the rest I hope some people know were you find the above pasted code ^^'
 
Newbie Spellweaver
Joined
Dec 16, 2011
Messages
54
Reaction score
26
you can change string to bytearray. its c# and works for me

Code:
//no hex values!!!
        public static string Xor(string data, string key)
        {
            var output = "";
            var count = 0;
            for (int i = 0; i < data.Length; i++)
            {
                output += data[i] ^ key[count];
				
				if(4>key.Length)
					count = (count+1)%key.Length;
				else{
					if (i%65536 == 0)
						count = 0;

					if (key.Length - (key.Length % 4 +1) == count)
						count = 0;
					else
						count++;
				}
            }
            return output;
        }

its works for decrypt aswell for encrypt (i hope, cant remember even its my own function >.>)
 
Back
Top