Encryption

This is a discussion on Encryption within the C Programming forums, part of the General Programming Boards category; dfgh...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    Encryption

    dfgh
    Last edited by 4658dan; 12-10-2010 at 02:23 PM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Your loop should be
    Code:
    for (count=0; count < 26; count++)
    When you add the offset to count, you should ensure that count does not exceed the alphabet bounds. Wrap it around.

    What if a letter is not found such as punctuation and spaces? How do you want those encrypted?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You can tell if a letter was not found in the alphabet by count having reached 26... (you didn't break out of the loop). In that case you can just output the untranslated character. Assign code = i so that your translating code passes the value: fprintf(myfile, "%c", alpha[code]). But you're missing some closing braces which means I have to make assumptions.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Ok, i see what you mean but how do i do it so 'z' will wrap back round to'a' if offset is 1? Also it only writes one letter to the file? Any ideas? Thanks again.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    The wrap-around issue: if count + shift is 26, it should be 0 ('Z' becomes 'A'). If the total is 27, it should become 1. See the pattern? Subract 26 to wrap it.

    I mentioned your problem with closing braces. I am not sure where you meant your fopen to go. Surely not inside the loop. Only open/create the file once. Then the character writing should be in the for (i loop. But not any deeper.

    You are not handling bothg upper and lower case yet.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Code:
    count += shift;
    if (count > 25)
        count -= 26;
    You need to move your fopen("encrypted.txt to before the for (i loop.
    You need to determine if the character was found in the alphabet at all.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    fprintf(myfile, "%c", alpha[count]); would go inside the last brace in the above snip. So it writes at the same rate as the i variable traverses through.

    The way you have the loops coded you can't tell if count ended up to be 26 because of some character + offset, or because the loop simply ran to the end (punctuation, space, etc.). I would suggest using a new variable to store the translated index. That way you can still check whether count reached 26 after the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. help needed with edit control & encryption
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2006, 08:21 PM
  3. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 10:16 PM
  4. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 10:51 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 06:45 PM

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