Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

lads i need your help again!

Options
  • 26-03-2008 10:09am
    #1
    Registered Users Posts: 7,525 ✭✭✭


    okay, im trying to write a few peogrammes that involve encryption.
    ive to do 3 different ones, caesar cipher, vigenere cipher and cryptanalysis.

    i do not have a idea even where to start:confused:

    is it just a matter of assigning different values to set letters???:confused:

    help would be much appreciated:)


Comments

  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    You seem to have stumbled onto the wrong page - google is here.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    i do not have a idea even where to start

    is it just a matter of assigning different values to set letters???
    Might be an idea to consult Google on the different types required, plenty of information out there.


  • Registered Users Posts: 7,525 ✭✭✭kona


    i was just googling there, it doesnt throw up much.
    i now understand what it is though.

    do you just put a = d, and so on??? and include the usual libraries.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    kona wrote: »
    i was just googling there, it doesnt throw up much.
    i now understand what it is though.

    do you just put a = d, and so on??? and include the usual libraries.
    For a very basic Caesar Cypher, yes. The others will require more work, though.


  • Registered Users Posts: 7,525 ✭✭✭kona


    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main(void)

    {

    /declare variables
    char A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z

    //welcome message

    printf("welcome to my program, it will encode your information!:\n");

    //input
    printf("please enter your information, only uppercase alphabetic, no numbers please!.\n");
    scanf("%c", );


    this is the start of my code, is it good or a load of crap??

    im not sure what to do with the variables.

    can you just put it through calculations, or does that only deal with numbers.


  • Advertisement
  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Looks OK, but why the variables, what do you plan to do with them?

    The beauty of working with computers is that characters and numbers are generally interchangeable. You can convert characters to their corresponding numbers (ASCII codes) and numbers to their corresponding ASCII characters.

    An ASCII "A" has a code of 65 and an ASCII "D" has a code of 68. So to convert an "A" to a "D" you simply add three. The same goes for any shift cipher.

    The trick (which I don't know how to do in C) is converting a character to a number, adding three to it and then converting it back to a character.

    Simply writing c = c + 3 probably won't do it.

    Another pitfall is reaching the end of the alphabet. In the caesar cipher, adding 3 to an "X", should give you "A". In ASCII, adding 3 to "X" will give you "a" (lowercase). Adding 3 to "x" (lowercase) will give you "{".

    So you should also convert the entire string to uppercase and work on making sure that only characters in the range A-Z are output in the encrypted text.


  • Registered Users Posts: 7,525 ✭✭✭kona


    seamus wrote: »
    Looks OK, but why the variables, what do you plan to do with them?

    The beauty of working with computers is that characters and numbers are generally interchangeable. You can convert characters to their corresponding numbers (ASCII codes) and numbers to their corresponding ASCII characters.

    An ASCII "A" has a code of 65 and an ASCII "D" has a code of 68. So to convert an "A" to a "D" you simply add three. The same goes for any shift cipher.

    The trick (which I don't know how to do in C) is converting a character to a number, adding three to it and then converting it back to a character.

    Simply writing c = c + 3 probably won't do it.

    Another pitfall is reaching the end of the alphabet. In the caesar cipher, adding 3 to an "X", should give you "C". In ASCII, adding 3 to "X" will give you "a" (lowercase). Adding 3 to "x" (lowercase) will give you "{".

    So you should also convert the entire string to uppercase and work on making sure that only characters in the range A-Z are output in the encrypted text.


    kinda get you, can i not just declare them in uppercase or do i have to write a code to do it??
    and the variables in planning on doing the c+3 but you reckon that wont work.

    can you use the characters as integers and use i++ for example


  • Registered Users Posts: 2,494 ✭✭✭kayos


    The basic Caeser Shift is about the simplest encryption to code. All your doing is shifting the alphabet a certain number of places. To be honest if your asking how to code a Caeser Shift its sounding like homework so other than saying work with numbers rather than letters I'm not gonna say much more.

    @Edit
    Seamus said basicly the same as me.


  • Registered Users Posts: 7,525 ✭✭✭kona


    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>


    int main(void)

    {

    //declare variables
    char A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;

    float bs;

    //welcome message

    printf("welcome to my program, it will encode your information!:\n");

    //input
    printf("please enter your information, only uppercase alphabetic, no numbers please!.\n");
    scanf("%c", A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;);

    //calculations

    A = D;
    B = E;
    C = F;
    D = G;
    E = H;
    F = I;
    G = J;
    H = K;
    I = L;
    J = M;
    K = N;
    L = O;
    M = P;
    N = Q
    O = R
    P = S
    Q = T
    R = U
    S = V
    T = W
    U = X
    V = Y
    W = Z
    X = A
    Y = B
    Z = C



    //goodbye message
    printf ("HERE IS YOURE CODE!");

    return(EXIT_SUCCESS);

    }

    here is my updated code.

    as regards homework, im just asking for help, its not like im saying here do my work for me, im not even doing computers, its mech eng, and ive noe idea why ive to do this, the only moving parts are the fecking keyboard soon to be flying out the window:D


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    ^^ This won't work. Have a think about what you're trying to do. If someone enters 'GOODBYE CRUEL WORLD' for example, how should this be encoded according to a caesar cipher? What will actually happen with your program?

    You'll need to figure out a way to manipulate each character of the input string (there's your first hint - use a string, a.k.a. a null-terminated char array) individually.

    BTW, Wikipedia is another good resource for this kind of information (I'm surprised you didn't get a load of WP links in your google search)
    seamus wrote: »
    Simply writing c = c + 3 probably won't do it.

    Actually, this would work fine in C, apart from the case of x onward as you pointed out.


  • Advertisement
  • Registered Users Posts: 2,494 ✭✭✭kayos


    kona wrote: »
    kinda get you, can i not just declare them in uppercase or do i have to write a code to do it??
    and the variables in planning on doing the c+3 but you reckon that wont work.

    can you use the characters as integers and use i++ for example

    Declare what as uppercase? You dont have to declare variables for every letter in the alphabet! What Seamus means is using the likes of ToUpper()/UCase() etc to convert the string input by the user to Upper case.

    Why in gods name would you use the ++ operator in this code? Other than creating the loop that cycles through the input string and shifts each letter by the number of places you have defined.

    The reason ACSII Code + 3 will not work has been given to you. Once you hit the letter in the alphabet where the addition of the shift number will result in a value greater than Z you start running into problems. The solution is a simple check on the resulting value and then taking 26 away, if needed, and then converting that to a letter.

    This is starting to remind be of a code review I did once..... there was a input box that was only to accept a letter..... the following was the onkeypress code

    if (value != 'a' or value != 'A' etc etc etc


  • Registered Users Posts: 7,525 ✭✭✭kona


    kayos wrote: »
    Declare what as uppercase? You dont have to declare variables for every letter in the alphabet! What Seamus means is using the likes of ToUpper()/UCase() etc to convert the string input by the user to Upper case.

    Why in gods name would you use the ++ operator in this code? Other than creating the loop that cycles through the input string and shifts each letter by the number of places you have defined.

    The reason ACSII Code + 3 will not work has been given to you. Once you hit the letter in the alphabet where the addition of the shift number will result in a value greater than Z you start running into problems. The solution is a simple check on the resulting value and then taking 26 away, if needed, and then converting that to a letter.

    This is starting to remind be of a code review I did once..... there was a input box that was only to accept a letter..... the following was the onkeypress code

    if (value != 'a' or value != 'A' etc etc etc

    sorry if im not up to bill gates standard:)

    i dont have a ****ing clue


  • Registered Users Posts: 2,534 ✭✭✭FruitLover


    Start small. The letter 'A' encoded with a caesar cipher turns into the letter 'D' (A plus three letters). Write the code to do this, but write it generically, so you can plug in 'B' and get 'E', or 'C' and get 'F' etc. Confirm that this works.

    When you have code that does this, modify it so that when you put in 'X' you get 'A' instead of '[', which is 3 characters after 'X' in ASCII, and the same for 'Y' and 'Z'.

    Then you should be well on your way.


  • Registered Users Posts: 46 hugejeans


    kona wrote: »
    sorry if im not up to bill gates standard:)

    i dont have a ****ing clue


    Well, start with what you know you need. Someone already said you need a string, so create one large enough to hold the text.

    then you have to step through the string shifting each letter by 3, again as said before char + 3 will work. and you need to stop the loop at the end of the string '\0' as we all know.

    do the above first with a string constant first i.e. char word[] = "ABCD"; when you can do that move onto dealing with X Y and Z, then with user inputed strings.


  • Closed Accounts Posts: 25,848 ✭✭✭✭Zombrex


    kona wrote: »
    sorry if im not up to bill gates standard:)

    i dont have a ****ing clue

    You are kinda making a meal of this when in fact it is not that difficult.

    This is something I cooked up very quickly. It is basic (only works for lower case, and the Caesar Cipher is fixed, to change it you have to manually change the array), but it works and it should be some where for you to start.

    You can use this as a base to improve your Caesar Cipher (figure out a way to dynamically create the look up array shifted to any length rather than fixed at 2), and also to develop the other ciphers.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int caesarEncrypt (char*);
    
    int main(int argc, char *argv[])
    {
            /* Take string to be encrypted from command line
             * Remember argv[0] is the command itself
             */
            char *unencrypted;
            unencrypted = argv[1];
    
            caesarEncrypt (unencrypted);
    }
    
    int caesarEncrypt (char *string)
    {
            /* Array holding look up values, shifted by 2, so position zero (which would be a) returns 'c' */
            char cipher[26] = {'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','a','b'};
    
            printf ("Caesar Cipher - \n");
    
            /* Cycle through each character in the string */
            int strLength = strlen (string);
            int i;
            for (i=0; i < strLength; i++)
            {
                    char currentChar = string[i];
    
                    /* get ascii value of current character and then subtract 97 to make 'a' give us 0, 'b' give us 1, 'c' give us 2 etc
                     * so we can use this value in our cipher array. See a ASCII table for clarification
                     */
                    int  currentAscii = (int) currentChar; /* putting the (int) there is called "casting", you are telling the compiler that you want currentChar copied as an int value, not a char value */
                    int  currentAlpha = currentAscii - 97;
    
                    char currentCipher = cipher[currentAlpha];
                    printf ("%c - %c\n",currentChar, currentCipher);
            }
    }
    
    


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    kona wrote: »
    sorry if im not up to bill gates standard:)

    i dont have a ****ing clue

    To be completely honest, I think you need to go back and learn the basics.


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    you need to brush up on your google-fu. I found the c code for a caesar cypher in about 10 seconds.


  • Registered Users Posts: 46 hugejeans


    Just showing a different way to do the same thing Wicknight done. This is just done for upper and lower case letters entered by a user.
    #include "stdio.h"
    #define TRUE 1
    #define FALSE 2
    #define SHIFT 3
    
    
    int main(void)
    {
        char word[100];
        int i=0;
        int error = FALSE;
    
        printf("\nenter a word:\n");
        scanf("%s",&word);
    
        /*loop through each char in the string*/
        while(word[i] != '\0') 
        {
            /*check if current char is a to z (upper or lower)*/
            if((word[i] >= 'A' && word[i] <= 'Z') || (word[i] >= 'a' && word[i] <= 'z')) 
            {
                /*check special cases*/
                if(word[i] == 'X' || word[i] == 'x' || word[i] == 'Y' || word[i] == 'y' || word[i] == 'Z' || word[i] == 'z')
                {
                    /*subtract 26 before shifting*/
                    word[i] = word[i] - 26;
                }
                /*shift by needed amount*/
                word[i] += SHIFT;
                i++;
            }
            /*if not correct input set error and notify*/
            else
            {
                printf("\nthis will only work on upper and lower case letters\n");
                error = TRUE;
                break;
            }
        }
        i=0;
        /*convert string to lower case if needed*/
        while(word[i] != '\0')
        {
            if(word[i] >= 'A' && word[i] <= 'Z')
            {
                word[i] = word[i] + 'a' - 'A';
            }
            i++;
        }
        /*if there is no error print the encrypted word*/
        if(error == FALSE)
        {
            printf("\nencrypted word: %s\n", word);
        }
        return 0;
    }
    


  • Registered Users Posts: 1,922 ✭✭✭fergalr


    Kona,

    From looking at your code, specifically:
    scanf("%c", A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;

    and
    A = D;
    B = E;

    It would seem like you don't know enough about the basics to right a program of the complexity you are trying to attempt. Not that it's madly complex, but you don't seem to have the basics down enough yet... (Like trying to do some sort of crazy downhill run before you've got basic cornering sorted).
    You can probably get the code working if you keep banging at it, but it's a really frustrating way of doing things; better to spend more time on the basics, and work your way up, especially if you've got more programming courses to do.

    I see what you are trying to express in your code, and the general algorithm idea makes sense - however, there's a lot of things you'd need to think about a bit differently before your solution would work well.


    To give you some pointers (no pun intended):
    scanf("%c", A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;

    scanf is a function in C which takes in a string, and a bunch of other variables as parameters.
    The string it takes in is really important. ( "%c" in your code). This string can be called the control string. or format string, if you prefer.

    What scanf does here, is it reads stuff in from the user on the keyboard, and depending on what string you passed into scanf breaks up what the user entered into the other paramaters.

    So, for example, the code: scanf("%c", A);
    would take in whats typed by the user, get the first character of whats typed by the user, and put that character in the character variable A.

    The reason it gets the first character of whats typed by the user is because the control string "%c", is a string with just %c in it, which means "character".


    What you are doing in your code is you are passing in loads of variables into scanf, but you are only putting in one character into the control string.
    So only the first of the variables you pass into scanf (the variable you have called A) is loaded with the first character of what the user types in.

    None of the other variables (B,C,D etc) will be touched.

    This is probably not what you intended...

    What you intended was probably more like:
    scanf("%c%c%c%c", A, B, C, D);
    etc

    This would take each of the characters in the string typed by the user and put the first one in A, the second one in B, the third one in C and so on.


    The thing is, that even is isn't a very good design of program...
    This is because the program structure would need to be different for a 4 character message than for a 5 character message, and so on. (I am assuming here, that you want to write a program that does the ceaser cypher for more than just one character, as the other posters have assumed too).
    What you really want to do is write a program that works for an N character message, and work it that way.
    This is a bit more complex and tends to involve constructs like loops, which you may or may not have used yet?



    What I would suggest you do is to go and start working with simpler programs first. If you don't do this, you are unlikely to make any headway with coding, and just feel frustrated.

    Heres some things to try, in an order that will hopefully be easier, and less frustrating, and build towards what you are trying to do. Get a good book on C to read examples from, and general information on the language, and try and do some of the following:

    Make a program that takes in one character, and prints it out.
    Make a program that takes in 2 characters, and prints them out the other way round. (second character first)
    Make a program that takes in one character and prints it out 10 times (use loops)
    Make a program that takes in one character, and does the ceaser cipher on it, then prints it out.


    Starting off smaller, trying to accomplish these programs, while building your knowledge will be much more productive.
    Expect it to take a fair bit of time though (20-40hours at least) - you'll have to spend a while to learn to code...
    If you want to be able to write bigger programs yourself, thats the only way to go - alternatively, if you are just trying to pass the course, you can probably copy and paste code from the internet; but you'll just find that very frustrating in the long run, and more frustrating as the program size increases.


Advertisement