Need help fixing error to get correct output.

This is a discussion on Need help fixing error to get correct output. within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> char K[6]={'k','c','u','n','n','i'}; int i, j, T; void reset( int S[]) { for(i=0; i<=255; i++) S[i]=i; } void ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    19

    Need help fixing error to get correct output.

    Code:
    #include <stdio.h>
     char K[6]={'k','c','u','n','n','i'};
     int i, j, T;
    void reset( int S[])
    {   for(i=0; i<=255; i++)
         S[i]=i;
    }
    void init( int S[])
    {
       for(i=0; i<=255; i++)
          j=0;  
          j=(j + S[i] + K[i%6])%256;
          T=S[i];
          S[i]=S[j];
          S[j]=T;    
    }
    int main(void)
    {  int c, S[256], d, i=0, j=0, reset S[], init S[]; 
    reset(S); 
    init(S);
      while(1)
      {
        c = getchar(); if( c < 0) break;
        i=(i+1)%256;
        j=(j+S[i])%256;
        T=S[i];
        S[i]=S[j];
        S[j]=T;
        d=S[(S[i] + S[j])%256];
        printf( "%c", c^d);   
      }
      return 0;
    }
    Error is with reset/init in main. I also can't get my output to match, if i could get any help thanks.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Code:
    {  int c, S[256], d, i=0, j=0, reset S[], init S[];
    this wont even compile, what do you mean by reset S[] and init S[]? This isn't valid C.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Assignment is RC4 Decryption (General Idea)

    It says im suppose to call reset and init to initialize the array in main.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by kctrk4
    Assignment is RC4 Decryption (General Idea)

    It says im suppose to call reset and init to initialize the array in main.
    Give us a link to the description. But this call of S looks like a method call.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Use global variables for the key and keylength; the key will be your VU LDAP UserID, all lowercase. For example:
    char key[] = "frodo";
    int keylength = 5;

    reset the RC4 permutation array:
    for i from 0 to 255
    S[i] := i

    initialize the permutation array:
    j := 0
    for i from 0 to 255
    j := (j + S[i] + key[i mod keylength]) mod 256
    swap(S[i],S[j])

    Declare the array and other parameters in your main program:
    int S[256], i = 0, j = 0;

    and call reset and init to initialize the array.

    Then for each byte read from stdin using getchar() until end-of-file, compute the next RC4 "output" byte
    i := (i + 1) mod 256
    j := (j + S[i]) mod 256
    swap(S[i],S[j])
    output S[(S[i] + S[j]) mod 256]

    and exclusive-OR that byte with the input byte to decrypt it.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    well i got it to compile without errors but my output is still not matching, can anyone help me?

    Should be:
    Money is better

    Mine:
    M-yM-t&^@M-2I{dM-^BPnzM

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define SSIZE 4
    
    void initArray(int *array, int arraysize){
     int i;
     
     for (i = 0; i < arraysize; i++) {
         array[i] = 0;
     }
     
    }
    
    void resetArray(int *array, int arraysize){
     int i = 0;
     //array[SSIZE] = '\0';
     for (i = 0; i < arraysize; i++) {
         array[i] = i;
     }
    }
    
    int main(void){
    
     int s[SSIZE];
     int i, j;
        initArray(s,SSIZE);
        for (i = 0; i < SSIZE; i++ ){
            printf("%d\n",s[i]); 
        }
        resetArray(s,SSIZE);
        
        for (j = 0; j < SSIZE; j++){
            printf("%d\n",s[j]);
        }
     return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Thanks, although your code is quite different, can you explain it?

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by kctrk4
    Thanks, although your code is quite different, can you explain it?
    Well Im passing an array and initializing it, I pass the array size so I know when Ive filled all positions, you must of course adapt it to fill it properly.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    I don't understand C all that well. I already thanked you but is there anyway you can tell me the problem in my program, which is causing me to print a random mess.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Give us your last source version.

  12. #12
    Registered User
    Join Date
    Mar 2006
    Posts
    19
    Not sure what you by last source.
    Implement RC4 decryption in C as follows using the algorithm description from Wikipedia: (Wikipedia RC4 file)
    Format taken from Wikipedia

    I just don't understand what i have wrong, i already posted my full code and orginal information given. My ouput is junk and does not match the "fortune". I have been trying to work with it since before my first post.

  13. #13
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by kctrk4
    Not sure what you by last source.
    Implement RC4 decryption in C as follows using the algorithm description from Wikipedia: (Wikipedia RC4 file)
    Format taken from Wikipedia

    I just don't understand what i have wrong, i already posted my full code and orginal information given. My ouput is junk and does not match the "fortune". I have been trying to work with it since before my first post.
    The code you posted doesn't compiles give us a compilling version.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting correct output
    By blah3 in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2008, 06:54 PM
  2. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 09:33 PM
  3. strange virtual function output
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2008, 08:08 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 07:46 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21