Thread: I do not do a lot of programing but ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Question I do not do a lot of programing but ...

    Would really love some help on getting this working.

    I was given the decrypt function and was trying to build the shell. I really don't remember my pointers and references, but think it is close...

    Please help! - Thanks!

    Code:
    #include <stdio.h>
    
    void decrypt();
    
    int main() {
    char in[255] = "NpfTvZKQKlM8Jh19/vI";
    
    decrypt(in);
    
    }
    
    int index(char *x) {
       char encIndex[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    
    // Not sure how to reference the array, but I believe I need to take in a char and return
    // an int.
    
    return 0;
    }
    void decrypt (char *in)
    {
        char first, second, third, fourth;
        int lf_count = 0,
            i        = 0;
        int byte_count = strlen(in);
        while (byte_count > 0)
        {
            first  = index(&in[i]);
            second = index(&in[i + 1]);
            third  = index(&in[i + 2]);
            fourth = index(&in[i + 3]);
    
            if (byte_count > 2)
                first = (first << 2) | (second >> 4);
            else
                first = (first << 2);
    
            printf("%c", first);
    
            if (byte_count > 2)
            {
                if (byte_count > 3)
                    second = (second << 4) | (third >> 2);
                else
                    second = (second << 4);
            }
    
            printf("%c", second);
    
            if (byte_count > 3)
            {
                third = (third << 6) | fourth;
                printf("%c", third);
            }
    
            i += 4;
            byte_count -= 4;
            lf_count++;
        }
    }
    Last edited by Dave_Sinkula; 10-06-2006 at 06:10 PM. Reason: Added [code][/code] tags -- learn to use them yourself.

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Welcome to the boards - use CODE tags when pasting code. Like:
    [code]-Your code-[/code]
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > int index(char *x)

    int index(char x)
    first = index(in[ i ]);
    Would seem a better interface.

    Then in your index() function, use a for loop to compare each element of encIndex with the parameter.
    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.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Your encIndex array has gone out of bounds at size 64. The encrypted input is "NpfTvZKQKlM8Jh19/vI". So, what would be the expected decrypted output for this string?

    A possible suggestion for the index function..
    Code:
    int index(char *x) 
    {
       char encIndex[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
       // or char encIndex[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
       return (int) (strchr(encIndex, *x)- encIndex);
     // Not sure how to reference the array, but I believe I need to take in a char and return
    // an int.
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. studying c++ programing ... concern
    By azsquall in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-03-2008, 04:30 PM
  2. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 04:48 PM
  3. using fopen getting a lot of warnings
    By netiad in forum C Programming
    Replies: 1
    Last Post: 04-28-2007, 08:44 PM
  4. programing
    By NiVaG in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 10-03-2004, 09:19 AM