[Dev] Xor

Results 1 to 8 of 8
  1. #1
    [Dev] Xor St34lth4ng3l is offline
    True MemberRank
    Apr 2013 Join Date
    __FILE__Location
    816Posts

    config [Dev] Xor


    RaGEZONE Recommends

    RaGEZONE Recommends

    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


  2. #2
    Guss decarvk is offline
    True MemberRank
    Jul 2008 Join Date
    /home/bed/Location
    350Posts

    Re: [Dev] Xor

    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.

  3. #3
    [Dev] Xor St34lth4ng3l is offline
    True MemberRank
    Apr 2013 Join Date
    __FILE__Location
    816Posts

    Re: [Dev] Xor

    The i++ comes after that because if its before it will set the arrays wrong, ok it's possible but i prefer it in the last line

  4. #4
    Guss decarvk is offline
    True MemberRank
    Jul 2008 Join Date
    /home/bed/Location
    350Posts

    Re: [Dev] Xor

    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.

  5. #5
    Member vasyater2 is offline
    MemberRank
    May 2012 Join Date
    RussiaLocation
    73Posts

    Re: [Dev] Xor

    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[i] ^ 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;
    }
    //---------------------------------------------------------------------------

  6. #6
    Account Upgraded | Title Enabled! inetpub is offline
    True MemberRank
    Oct 2012 Join Date
    204Posts

    Re: [Dev] Xor

    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);
    }

  7. #7
    Guss decarvk is offline
    True MemberRank
    Jul 2008 Join Date
    /home/bed/Location
    350Posts

    Re: [Dev] Xor

    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 ^^'

  8. #8
    Member bergi9 is offline
    MemberRank
    Dec 2011 Join Date
    54Posts

    Re: [Dev] Xor

    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 >.>)




Advertisement