Thread: Caesar's encoding problem

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    Caesar's encoding problem

    I have an exercise that is based in Caesar's encoding method.
    I have to encode ASCII characters from:
    32 to 127 with any key.
    key may be [1, any integer].

    I made two functions one for encoding end the second for decoding ....when i encode a word i cant get the original back after decoding....can you help?

    I think MOD is the problem..

    This is the encode function:
    Code:
    char *Ceasar_enc(char text[],int key) {
    char *p=text;
    long int temp;
     
     while(*p){	
    /*    127 - 32= 96 ascii values plus 32  to start from 32 not    from 0 and end to ascii char 127 not 96
    */
      temp=(*p+key)%96+32;
      *p++=(char)temp;
     }
    This is the decode function-i didnt write the decode formula because every time i write one i get faulse results
    Code:
    char *Ceasar_dec(char text[],int key) {
    char *p=text;
    long temp;
    
     while(*p){
      /* i cant find the correct decode formula */
      *p++=(char)temp;
     } 
     
     return text;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > temp=(*p+key)%96+32;
    Write it out in long form
    Code:
    temp = *p;
    temp = temp + key;
    temp = temp % 96;
    temp = temp + 32;
    So to reverse it, we do
    Code:
    temp = *p;
    temp = temp - 32;
    // getting the idea yet?

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    no i dont understand
    sorry

    when the key +ascii value are bigger than 127 then:result mod 96
    how i can get the original number?

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    The unsigned char datatype allows values from 0 to 255 - cast to unsigned char if you're worried about signed arithmetic.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    Code:
    char *Ceasar_enc(char text[],int key) {
    char *p=text;
    long int temp;
     
     while(*p){	 
      temp=(*p+key)%96+32;
      *p++=(char)temp;
     } 
     
     return text;
    }
    char *Ceasar_dec(char text[],int key) {
    char *p=text;
    long temp;
    
     while(*p){
      temp=*p;
      temp=abs(temp-32-key);
      temp=temp%96;
      *p++=(char)temp;
     } 
     
     return text;
    }
    input:theo
    encoded output:5)&0

    then when i use the second function i get:♣☼

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    i'm worried about the key..the key is any integer so i must use mod arithmetic

  7. #7
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    p.s:i encode characters in the interval 32-127(ascii values)
    so an usnigned char array is useless...

  8. #8
    Hello,

    Caesar's encoding method is quite easy if you look at it from a simple view. I found a site that may help explain in detail how an example may go. I used it as a base to creating this program and it has helped: Caesar's Cipher [with Key b=3]

    Sometimes it is always best to find a tutorial that explains how your program will work so you can reverse its content and write it in your programming language.

    I do see you are on the right track, though your encryption may be off. As I have breifly looked over your code, you compute the following:

    (current letter plus key) take remainder of 96 plus 32

    It may be me, though this formula seems partially off. For example the word "cat". The word "cat" is 99 97 116 in ASCII. A good reference is ASCIITable.com. If you take key for 3 you would increase each of the letters by 3. If we wanted to make "cat" simple to read, we could subtract 97, or 'a', from each of the letters. That would produce 2 0 19. This way we don't have to worry about large numbers for the time being. Once we encrypt "cat", it would produce 5 3 22. We know 5 would be 102, 3 would be 100, and 22 would be 119. Neither of which succseed the 127 default ASCII mark.

    Encoding
    The problem with your code may be of the following. Let's take the word of "fish" and compute your formula here with the key of 5:
    Code:
    // The Letter 'f'
    (102+5) = 107
    107 % 96 = 11
    11 + 32 = 43
    43 is a + symbol in ASCII. A simple number of 5 changed an ASCII value from 102 to 43. Reversing your encryption may be a bit difficult, but it can be done. Let's look at how this might be possible. Let's take the letter 'f' again which is now a '+' symbol.

    Decoding
    First lets, lets take the number 43. This is our new encryption for our one letter. Remember how we used 96 to take the remainder between your new number the key was added to? Yes. We can add that number to our current 43. This will give us 139. We subtract the two numbers we added. 139 - 32 = 107 and 107 - 5 = 102. We know that 102 is 'f' in ASCII.


    I hope this helps,
    - Stack Overflow
    Last edited by Stack Overflow; 11-17-2004 at 11:04 AM. Reason: More info.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    thanks my friend but is working for keys 1-90 ,
    for keys 91,92,93,94,95,96 i get chinese why?

  10. #10
    The algorithm may be off in your current code. I'm just speculating at least. Usually the ASCII characters 1-90 aren't all printable characters.

    The function isprint() is in the ctype library. This checks if the character is printable. You could pass your character or integer to see if your character is within the ASCII bounds of being printed.

    Mostly, isprint() checks if the value passed is greater than or equal to 0x20 or is less than or equal to 0x7E. I'm not saying this always applies, it is just a mere example. Here is another good reference to ASCII and Hex.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you haven't redone your algorithm yet, here's my version

    Restate the encoding process:
    Take any lower case letter ASCII value and add the key value.
    Modulo the sum of that to get a range of 0 to 95 inclusive.
    Now add 32 to the modulus result to get range of 32 to 127
    using any key that is any positive integer greater than 0.
    The result is called temp and will be casted back to a char
    for the visual expression of the encoded message.

    The following formula does that okay.

    temp = (ASCIIvalueOfChar + key) % 96 + 32;

    Develop some test cases:

    let: ASCIIvalueOfChar = 97;
    then:
    temp = 34 if key = 1
    temp = 32 if key = 95
    temp = 32 if key = 191
    temp = 32 if key = 287

    Now reverse the process to decode:

    Using the above test cases the trick is to get back to 97
    if temp is 32 and key is 95, 191, or 287.

    To solve this start out by finding mod.

    mod = temp - 32 = (ASCIIvalueOfChar + key) % 96;

    then restate the equation like this

    ASCIIvalueOfChar + key = x*96 + mod; where x is a positive integer

    and then this

    ASCIIvalueOfChar = x*96 + mod - key;

    now find x such that ASCIIvalueOfChar is in range 97 to 122;
    Code:
    for(x = 1; ASCIIvalueOfChar < 97 || ASCIIvalueOfChar > 122; ++x)
    ASCIIvalueOfChar = x * 96 + mod - key;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Problem with opening file and pathname
    By stevespai in forum C# Programming
    Replies: 3
    Last Post: 07-13-2006, 08:03 AM