Thread: somthing funky

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    somthing funky

    i have the following
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void Decrypt( char *, char * );
    void Get_Password( char *, char * );
    
    int main()
    {
        char Text[] = "TOMORROW NEVER DIES";
        char Pword[] = "hello";
        char Encrypted[20] = { '\0' };
        char *crypt = NULL;
    
        for ( int j = 0, i = 0; Text[i]; i++)
        {
            Encrypted[i] = Text[i] ^ Pword[j];
    
            if ( j == 4 ) j = 0;
            else ++j;
    
            ++i;
        }
        
        //Decrypt( Encrypted, Pword );
        crypt = Encrypted;
        Decrypt( crypt, Pword );
    
        Get_Password( crypt, Text );
    
        return 0;
    }
    
    void Decrypt( char *Encrypted, char *Pword )
    {
        for ( int j = 0, i = 0; i < 20 ; i++ )
        {
            printf("%c", Encrypted[i] ^ Pword[j]);
    
            if ( j == 4 ) j = 0;
            else ++j;
        }
        putchar('\n');
    }
    
    void Get_Password( char *Encrypted, char *Text )
    {
        for ( int j = 0, i = 0; i < 20 ; i++, j++ )
        {
            printf("%c", Encrypted[i] ^ Text[j]);
        }
        putchar('\n');
    }
    Both functions only get passed the first element of the encrypt array.
    Since i was told there is no need to tell the function to expect an array ie char [] and to use char * as the name of the array was effectively a pointer anyway. That is what i have been doing and has worked the last week or so. It even works for the password but not the encrypt string.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,071
    And it is pretty obvious why it doesn't work...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,598
    In your first for loop you are incrementing i twice.
    And you shouldn't hard-code the lengths.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    void Encrypt( char *, int len, const char * );
     
    int main()
    {
        const char *Original = "TOMORROW NEVER DIES";
        char Text[256];
        const char *Pword = "hello";
     
        strcpy( Text, Original );
        int text_len = strlen( Text );
     
        printf( "Original:  %s\n", Original );
     
        Encrypt( Text, text_len, Pword );
        // This may not print properly due to possible embedded '\0's
        printf( "Encrypted: %s\n", Text );
     
        printf( "Password:  " );
        for ( int i = 0; i < text_len ; i++ )
            printf("%c", Original[i] ^ Text[i]);
        putchar('\n');
         
        // length is needed due to possible embedded '\0's
        // (so you can't just use strlen in Encrypt)
        Encrypt( Text, text_len, Pword );
        printf( "Decrypted: %s\n", Text );
     
        return 0;
    }
     
    void Encrypt( char *Encrypted, int len, const char *Pword )
    {
        int pword_len = strlen( Pword );
        for ( int j = 0, i = 0; i < len; i++, j = (j + 1) % pword_len )
            Encrypted[i] ^= Pword[j];
    }
    Ordinary language is totally unsuited for expressing what physics really asserts.
    Only mathematics can say as little as the physicist means to say. - Bertrand Russell

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    bugger and double bugger.....that is what i get for cutting and pasting code and changing from a while to a for loop spent ages banging my head against the screen guess i couldnt see the wood for the trees.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i have changed the password to "hel" and trying to brute force it

    Code:
    int main()
    {
        char Text[] = "TOMORROW NEVER DIES";
        char Pword[] = "hel";
        char Encrypted[20] = { '\0' };
    
    
        for ( int j = 0, i = 0; Text[i]; i++)
        {
            Encrypted[i] = Text[i] ^ Pword[j];
    
            if ( j == 2 ) j = 0;
            else ++j;
        }
    
        for ( int i = 0; Encrypted[i]; i++)
            printf("%c", Encrypted[i]);
        putchar('\n');
    
        for ( int Letter = 0, StartIndex = 0, i = 97; i <= 122; i++ )
        {
            if  ( ( ( Encrypted[StartIndex] ^ i )  >= 65  &&  ( Encrypted[StartIndex] ^ i )  <= 90  ) || ( Encrypted[StartIndex] ^ i )  == 32 )
            {
                int j = StartIndex;
                Letter = 0;
                while ( j <= 18 )
                {
                    if ( ( ( Encrypted[j] ^ i )  >= 65  &&  ( Encrypted[j] ^ i )  <= 90  ) || ( Encrypted[j] ^ i )  == 32 )
                    {
                        j += 3;
                    }
                    else
                    {
                        j = StartIndex;
                        Letter = 1;
                        break;
                    }
                }
                if ( !Letter )
                {
                    printf("%c ", i);
                    StartIndex++;
                    i = 96;
                    //putchar('\n');
                }
            }
        }
    
        return 0;
    }
    the output i get is odd
    <*!'7>'2L& :-7L,,);
    h a l a a l a a l a a l a a l a a a a

    first pass it gets the first and third letter correct then the next 4 passes it only gets the third letter correct then the final lot it gets none of them correct

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,598
    Your code doesn't make a lot of sense to me.
    You seem to be trying to do something like the following.
    It assumes that the plaintext is only uppercase letters and spaces and that the password is only lowercase letters, and that we know the password's length.
    So it can only find possible password letters, not the exact letters since multiple letters could lead to uppercase letters or spaces. A longer message should give a better result.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
     
    int main()
    {
        char Text[] = "TOMORROW NEVER DIES";
    //    char Text[] = "A MUCH LONGER MESSAGE FOR HOPEFULLY A BETTER RESULT";
        char Pword[] = "hel";
        char Encrypted[sizeof Text];
     
        // Don't hardcode string lengths.
        int len = strlen( Text );
        int plen = strlen( Pword );
     
        printf("%s\n", Text);
     
        for ( int j = 0, i = 0; Text[i]; i++, j = (j + 1) % plen )
            Encrypted[i] = Text[i] ^ Pword[j];
        Encrypted[len] = '\0';
     
        // Encrypted characters are not necessarily printable.
        for ( int i = 0; i < len; i++)
            printf(isprint(Encrypted[i]) ? "%c" : "{0x%02X}", Encrypted[i]);
        putchar('\n');
     
        // Don't use numbers for letters, e.g., say 'A' instead of 65.
        for ( int StartIndex = 0; StartIndex < plen; StartIndex++ )
        {
            printf( "%2d: ", StartIndex );
            for ( int i = 'a'; i <= 'z'; i++ )
            {
                int Letter = 1;
                for ( int j = StartIndex; j < len; j += plen ) {
                    int x = Encrypted[j] ^ i;
                    if ( ! ( ( x >= 'A' && x <= 'Z' ) || x == ' ' ) )
                    {
                        Letter = 0;
                        break;
                    }
                }
                if ( Letter )
                    printf( "%c ", i );
            }
            putchar('\n');
        }
     
        return 0;
    }
    Ordinary language is totally unsuited for expressing what physics really asserts.
    Only mathematics can say as little as the physicist means to say. - Bertrand Russell

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    its another Euler challenge. you have to read in an encrypted text file and decipher it All you are told is the password is three lower case letters.. So my approach was to start off with a known string in uppercase then once that works a known string in lower case then a mixture and finally put in some punctuation

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    1,071
    If you start to think in terms of functions AND pointers this could be proven easier:
    Code:
    #include <stddef.h>
    #include <stdint.h>
    
    // Both dest and src points to buffers with the same size.
    void xorblk( void *dest, const void *src, size_t blksize,
                 const void *key, size_t keysize )
    {
      uint8_t *d;
      const uint8_t *s, *de, *k, *ke;
    
      d = dest;
      s = src;
      de = dest + blksize;
      k = key;
      ke = key + keysize;
    
      while ( d < de )
      {
        *d++ = *s++ ^ *k++;
        if ( k == ke ) k = key;
      }  
    }
    
    // test.
    #include <stdio.h>
    #include <string.h>
    
    static void display( const char *, const void *, size_t );
    
    int main( void )
    {
      char dest[32];    // with enough space to hold "encoded" data.
      char src[] = "TOMORROW NEVER DIES";
      const char key[] = "hel";
      size_t size, ksize;
    
      size = strlen( src );   // or (sizeof src - 1)
      ksize = strlen( key );  // or (sizeof key - 1)
    
      xorblk( dest, src, size, key, ksize );
      display( "original :", src, size );
      display( "encoded :", dest, size );
    
      xorblk( src, dest, size, key, ksize );
      display( "decoded :", src, size );
    }
    
    void display( const char *prompt, const void *p, size_t size )
    {
      const uint8_t *q, *qe;
      ptrdiff_t offset;
    
      fputs( prompt, stdout );
    
      q = p;
      qe = p + size;
    
      while ( q < qe )
      {
        offset = q - (const uint8_t *)p;
        if ( offset % 16 == 0 )
          printf( "\n%08tx:", offset );
    
        printf( " %02x", *q );
        q++;
      }
      putchar( '\n' );    
    }
    Code:
    $ ./xorenc
    original:
    00000000: 54 4f 4d 4f 52 52 4f 57 20 4e 45 56 45 52 20 44
    00000010: 49 45 53
    encoded :
    00000000: 3c 2a 21 27 37 3e 27 32 4c 26 20 3a 2d 37 4c 2c
    00000010: 2c 29 3b
    decoded :
    00000000: 54 4f 4d 4f 52 52 4f 57 20 4e 45 56 45 52 20 44
    00000010: 49 45 53
    Last edited by flp1969; 06-06-2023 at 05:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Funky output
    By zeldac in forum C Programming
    Replies: 15
    Last Post: 09-17-2014, 11:53 AM
  2. Somthing weired with C !!
    By wasem21 in forum C Programming
    Replies: 9
    Last Post: 12-18-2010, 12:02 PM
  3. XP allowing me to do somthing 2K won't! Wow
    By Queatrix in forum Windows Programming
    Replies: 0
    Last Post: 07-25-2007, 04:52 PM
  4. Funky enum
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2006, 10:33 AM
  5. funky new avatar
    By iain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-14-2002, 11:44 AM

Tags for this Thread