#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Problem with my rail fence cipher code


    The decoding part was OK.
    But there's a problem with the encoding output.
    I cant seems to solve the errors.
    Can anybody help?
    Thanks in advance.
    Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    
        int rail,mode,menu;
        char text[100];
    
        system("cls");
        printf("*~*~*~*~*~* RAIL FENCE CIPHER *~*~*~*~*~*\n\n");
    
      //  while(1)
            {
    
                printf("\n =================MENU=================\n");
                printf("\n 1. Encoding // 2. Decoding // 3. Exit\n");
                printf("\n >> Select a mode: ");
                scanf("%d",&mode);
    
                if(mode==1)
                {
                    printf("\n ============ENCODING MODE=============\n");
                    printf("\n >> Enter the value of rail: ");
                    scanf("%d",&rail);
                  //  getchar();
    
                    printf("\n >> Enter the text: ");
                    scanf("%s",&text);
    
                    encrypt(rail,text);
                }
    
                else if(mode==2)
                {
                    printf("\n ============DECODING MODE=============\n");
                    printf("\n >> Enter the value of rail: ");
                    scanf("%d",&rail);
                  //  getchar();
    
                    printf("\n >> Enter the text: ");
                    scanf("%s",&text);
    
                    decrypt(rail,text);
                }
    
                else if(mode==3)
                    return 0;
    
                else
                    return main();
    
            }
    
        printf("\n\n Return to main menu? (0. No // 1. Yes)");
        printf("\n Your choice: ");
        scanf("%d",&menu);
    
        if (menu==1)
            return main();
    
        else
            return 0;
    
    }
    
    void encrypt(int rail,char *plain)
    {
    
        char railfence[rail][100],buf[2];
        int i;
        int number=0,increment=1;
        buf[1]=";
    
        for(i=0;i<rail;i++)
        railfence[i][0]=Ħħ;
    
        for(i=0;i<strlen(plain);i++)
        {
    
            if (number+increment==rail)
                increment=-1;
    
            else if (number+increment==-1)
                increment=1;
    
            buf[0]=plain[i];
            strcat(railfence[number],buf);
            number+=increment;
    
        }
    
            for(i=0;i<rail;i++)
            printf("%s",railfence[i]);
    
    }
    
    void decrypt(int rail,char *cipher)
    {
        int cipherlength=strlen(cipher);
        int j,counter=0,number=0,increment=1,railfence[rail][100],i,count[100];
    
        for(i=0;i<cipherlength;i++)
            count[i]=0;
    
        for(i=0;i<cipherlength;i++)
            {
    
                if(number+increment==rail)
                    increment=-1;
    
                else if(number+increment==-1)
                    increment=1;
    
                railfence[number][count[number]]=i;
                ++count[number];
                number+=increment;
    
            }
    
        char buffer[1000];
    
        for(i=0;i<rail;i++)
        for(j=0;j<count[i];j++)
            {
    
                buffer[railfence[i][j]]=cipher[counter];
                ++counter;
    
            }
    
        buffer[cipherlength]=Ħħ;
        printf("\n >> Decrypted text: %s\n",buffer);
    
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    But there's a problem with the encoding output.
    I cant seems to solve the errors.
    What are the errors? If you're getting compile errors post the complete error messages, exactly as they appear in your development environment.

    Otherwise please describe what is wrong with your code. What output is your program producing, and what do you expect the program's output to be.

    Jim
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by jimblumberg
    What are the errors? If you're getting compile errors post the complete error messages, exactly as they appear in your development environment.

    Otherwise please describe what is wrong with your code. What output is your program producing, and what do you expect the program's output to be.

    Jim
    I'm creating a program for rail fence cipher.
    For example, if the input text is "programming", and the number of rail(row) is 3, then the rail fence would looks like this:

    p.....r.......i
    ..r..g..a..m..n
    ...o......m......g

    (ignore the dots)

    After encoding, the cipher text is "prirgamnomg".

    Same goes to decoding, if input the text "prirgamnomg" with rail of 3, the output will be "programming".

    But I just need to display the output text but not the rail fence.

    The decoding goes well. But the output for encoding is displaying symbols instead of characters.

    The error is in these lines:

    Code:
    buf[1]=";
    error: missing terminating " character

    Code:
    railfence[i][0]=Ħħ;
    error: stray '\241' in program
    error: stray '\261' in program

    Code:
    buffer[cipherlength]=Ħħ;
    error: stray '\241' in program
    error: stray '\261' in program



    By the way I got this code from an online source and modified a bit since I am new to C and have no idea in creating a program for rail fence cipher a.k.a zig-zag cipher.
  6. #4
  7. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,555
    Rep Power
    2338
    Code:
    buf[1]=";
    What are you trying to do with buf[1]? Make it a null character? If so,
    do this

    Code:
    buf[1] = '\0';
    As to the other,

    Code:
    buffer[cipherlength]=Ħħ;
    Explain what you're trying to do there. What are those characters doing there?

    By the way I got this code from an online source and modified a bit since I am new to C and have no idea in creating a program for rail fence cipher a.k.a zig-zag cipher.
    Oh, I see. Copy-paste. Not the way to learn.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Of course I'm trying to learn the codes line by line.
    I have been struggling for 3 weeks in creating the code myself but still no idea.
    All I know is the code have to do with ARRAY.
    I have no choice but I need to submit my program by this Friday.

    I know how to display a square matrix but have no clue with zig-zag shape matrix.
    Can someone please at least explain on how to display a zig-zag array like above?
    Thank you.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Okay after many tries finally it works and this is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char text[100];
    
    int main()
    {
    
        int rail,mode,menu;
    
        system("cls");
        printf("*~*~*~*~*~* RAIL FENCE CIPHER *~*~*~*~*~*\n\n");
    
            {
    
                printf("\n =================MENU=================\n");
                printf("\n 1. Encoding // 2. Decoding // 3. Exit\n");
                printf("\n >> Select a mode: ");
                scanf("%d",&mode);
    
                if(mode==1)
                {
                    printf("\n ============ENCODING MODE=============\n");
    
                    printf("\n >> Enter the text: ");
                    scanf("%s",&text);
    
                    printf("\n >> Enter the value of rail: ");
                    scanf("%d",&rail);
    
                    printf("\n >> Encrypted text: ");
    
                    encrypt(rail,text);
                }
    
                else if(mode==2)
                {
                    printf("\n ============DECODING MODE=============\n");
    
                    printf("\n >> Enter the text: ");
                    scanf("%s",&text);
    
                    printf("\n >> Enter the value of rail: ");
                    scanf("%d",&rail);
    
                    decrypt(rail,text);
                }
    
                else if(mode==3)
                {
                    printf("\n*~*~*~*~* THE END!! THANK YOU!! *~*~*~*~*\n\n");
                    return 0;
                }
    
                else
                    return main();
    
            }
    
        printf("\n\n Return to main menu? (0. No // 1. Yes)");
        printf("\n Your choice: ");
        scanf("%d",&menu);
    
        if (menu==1)
            return main();
    
        else
            printf("\n*~*~*~*~* THE END!! THANK YOU!! *~*~*~*~*\n\n");
            return 0;
    
    }
    
    void encrypt(int rail,char *plain)
    {
    
        char railfence[rail][100],buf[2];
        int i;
        int number=0,increment=1;
        buf[1]='\0';
    
        for(i=0;i<rail;i++)
        railfence[i][0]='\0';
    
        for(i=0;i<strlen(plain);i++)
        {
    
            if (number+increment==rail)
                increment=-1;
    
            else if (number+increment==-1)
                increment=1;
    
            buf[0]=plain[i];
            strcat(railfence[number],buf);
            number+=increment;
    
        }
    
            for(i=0;i<rail;i++)
            printf("%s",railfence[i]);
    
    }
    
    void decrypt(int rail,char *cipher)
    {
        int cipherlength=strlen(cipher);
        int j,counter=0,number=0,increment=1,railfence[rail][100],i,count[100];
    
    
    
        for(i=0;i<cipherlength;i++)
            count[i]=0;
    
        for(i=0;i<cipherlength;i++)
            {
    
                if(number+increment==rail)
                    increment=-1;
    
                else if(number+increment==-1)
                    increment=1;
    
                railfence[number][count[number]]=i;
                ++count[number];
                number+=increment;
    
            }
    
        char buffer[1000];
    
        for(i=0;i<rail;i++)
        for(j=0;j<count[i];j++)
            {
    
                buffer[railfence[i][j]]=cipher[counter];
                ++counter;
    
            }
    
        buffer[cipherlength]='\0';
        printf("\n >> Decrypted text: %s\n",buffer);
    
    }
    Thanks for the help.
    But it would be better if the rail fence is display as well.
    How to display a zig-zag array matrix? Anyone?

IMN logo majestic logo threadwatch logo seochat tools logo