Thread: Help with cipher key problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    7

    Help with cipher key problem

    Ok, Hello all, new to this forum and C for that matter, but if anyone could help at all it would be greatly appreciated.

    I have been tasked to create a program to read a simple substitution cipher key from a file, then a plaintext file, and write the converted text to the output file - no problem. Now I have to create a program to create the inverse key from the original. I can generate the key myself on paper but I can't see for the life of me how I would generate the inverse within the program. I'm not asking for it to be written out for me, just some insights onto the approach I should take would be really useful, as I've been staring at this thing for hours and it's driving me up the wall!!

    Many Thanks
    -Chris

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So have a struct containing two characters
    Code:
    struct foo { char clear; char encrypted; }
    Then have say
    Code:
    struct foo table[26];
    And initialise however you please, say
    Code:
    table[0].clear = 'a'; table[0].encrypted = 'q';
    Encryption searches for a matching .clear and outputs the .encrypted
    Decryption searchs for a matching .encrypted and outputs .clear

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    Apologies, I should have described the situation a bit better. I'm only supposed to use arrays and loops to acheive this. I have a key array within my cipher program and a loop which reads the ascii code of the character in each part of the array one by one, it then subtracts 65 and uses that value to determine what position of the cipher array to take the output character from. this allows there to be an inverse key...ie

    plaintext ABCDEFGHIJKLMNOPQRSTUVWXYZ
    key LKASDFJHGMZNXCVBQWPOERTIUY
    inverse CPNE....etc

    The inverse is such because A got turned into L, but C was turned into A, so in the inverse key, A becomes C and so on and so forth.

    So basically i need to create a program to generate this inverse key using only loops and arrays.

    Many thanks in advance
    -Chris

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what's the problem exactly? What part can't you do? You're right though, you need to create a program. So far, I don't see any of that.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So use two arrays then and forget the structure.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    7
    Have solved the problem, the solution came to me whilst I was trying to get to sleep =)

    Thanks for all your help guys

    -Chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Mouvment problem
    By Death_Wraith in forum C++ Programming
    Replies: 9
    Last Post: 06-01-2004, 06:58 PM
  3. BCB Key press problem
    By Death_Wraith in forum Game Programming
    Replies: 0
    Last Post: 05-30-2004, 03:13 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM