Thread: Problems with Xor encryption

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,786
    Quote Originally Posted by lala123 View Post
    So.... Does anyone know why my code ain't working? The executable runs fine but the file remains unchanged.
    When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.
    So check the return values of th efunction called
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    vart

    Amazing! It worked! Thank you =)

    Here's the code in case someone wants it:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        FILE *fp;
        long fSize;
        size_t result;
        char key[] = "123", *buffer;
    
        fp = fopen ("xor.txt", "rb+");
        
        if (fp == NULL)
           {fputs("File error",stderr); exit(1);}
           
        fseek (fp, 0, SEEK_END);
        fSize = ftell (fp);
        rewind (fp);
        
        buffer = malloc (sizeof(char)*fSize);
        
        if (buffer == NULL)
           {fputs ("Memory error",stderr); exit (2);}
           
        result = fread (buffer, 1, fSize, fp);
        
        if (result != fSize)
           {fputs ("Reading error",stderr); exit (3);}
           
        XorEncrypt(buffer, key);
        
        rewind (fp);
        fwrite (buffer, 1, fSize, fp);
        
        fclose(fp);
        free(buffer);
        getch();
        return 0;
    }
    
    int XorEncrypt (char *string, char *key)
    {
        int i, j, mod;
        
        int StringLen = strlen(string); // 20
        int KeyLen = strlen(key); // 6
    
        for (i = 0; i < (StringLen / KeyLen); i++)
        {
            for (j = 0; j < KeyLen; j++)
            {
                string[i*KeyLen + j] ^= key[j];
            }
        }
        
        mod = StringLen &#37; KeyLen;
        
        if (mod != 0)
        {
             for (i = 0; i < mod; i++)
             {
                 string[StringLen - mod + i] ^= key[i];
             }
        }
        
        return 0;
    }

  3. #18
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Well actually not really.....

    When I use small strings in the file it works finely, but with larger texts and files...

    Example: (using key "test43")

    "This is a test!!!" -> " ZET@VRU" -> "This is a test!!!"

    But when I placed the code (the same I just posted) and then passed the output I got:

    Code:
    #includ SHGGZ\
    yoPZP]QZ
    My>>~ ^\]] ESPXEUATOSE/8Zy>HyoST2,?1Hy>TES[]E']I^~~TE NV+SQ@	O99TESTW[SQJ/8SIEW@V^RV^~~99TESTRCTXS[CS\K] LGVISVFQ_GZO99TEST99TEST]UTMIE=!x]hyTTESTOU2VFA]^SLZ MB]NyoSTTESy>TESGVS\RCXECX`1 8+q}0LHy>TESgZ SIU  ZO99TESTFVZO99TEST99TESTVFTX\E[]I
    \W[Z^R`]>~EST>~ESTZE[AU T	T+&8xyoSTTESRC T9 FJT [AVI  PVZOVT]^y>TESTyoST  XGTXSFVS\VFEISgZ _TRC]^~~TE~~TE  XGTDNTR`]99TESTTAGE[VfVSFXQALHTQKS\O~~TEST>~ESTk6WA
    \VF 
    ]>~EST>~ESTAP\]>~ESTU Q\RVISE6QT]>~EST>~ESTU	QZO99TESTRA [AU ]>~ESTTOhyTT AAECO99	hyy>ZS,[A1MC E[\REY@AXPTXZy>HyoSTT]T_TY\^~~TE~~TE@'ZT8 T	Q]\]]LHTTWCy>TESZGT.
    xVENTGG	XZO[EEy>>~ESTUS\]IECOZTYS\gGxVE\TV
    )TX_>~ESTHyoSTTESTR\E[TUHT^HE8MHT^_L~~TESThyTTESTTES@A/]? 
    8Q]TNSi*XSQJ/.O99TESTTEy>TES	99TEST99TESTY\ENTgGxVEVTV
    )>~EST>~ESTZE[[WTDNTyoSThyTTEST
    TZTXSDEOTY\^S]hyTTESThyTTESTTESTGGo` SSY^S_Z)E-IX(iyoSTTESTNyoST	hyTThyTT AAECO99
    =S There must be something wrong with the algo right?

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,674
    Code:
    #include
    test43test43
    What value do you get when you exclusive-or a letter with itself?
    What do you think strlen() is going to do when it gets to that value?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Salem View Post
    Code:
    #include
    test43test43
    What value do you get when you exclusive-or a letter with itself?
    What do you think strlen() is going to do when it gets to that value?
    If lala123 had been paying attention, he/she would have seen that I already pointed this out

  6. #21
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Oh, thanks for that. The code is working now.

    Code:
    int XorEncrypt (char *string, char *key, long sLen)
    {
        int i, j, mod;
        
        int KeyLen = strlen(key); // 6
    
        for (i = 0; i < (sLen / KeyLen); i++)
        {
            for (j = 0; j < KeyLen; j++)
            {
                string[i*KeyLen + j] ^= key[j];
            }
        }
        
        mod = sLen &#37; KeyLen;
        
        if (mod != 0)
        {
             for (i = 0; i < mod; i++)
             {
                 string[sLen - mod + i] ^= key[i];
             }
        }
        
        return 0;
    }
    Oh and BTW Im a man...

  7. #22
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by lala123 View Post
    Oh and BTW Im a man...
    Not like it matters, I just don't want to mistakenly refer to a woman as "he" or a man as "she" so I just use both

  8. #23
    the magic penguim
    Join Date
    Jul 2005
    Posts
    91
    Ok

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM