Thread: Caeser Cipher

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You forgot the +/- 'a' bit of the code to make 0..25 into 'a' to 'z'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    KK i finally understand all of this after playing around with it a bit however when i still try to decrypt this just prints the right amount of characters this time except it prints not "luke" like it should do.Anyway here's the code i was hoping someone could have a look at it and tell me whats the matter with the decrypt part as i thought it would work as it is similar to what Sifried showed me:

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        string cyphertext;
        string plainchar = "luke";
        int shift = 2;
        int alphabet_size = plainchar.length();
        for (int i = 0; i < alphabet_size; ++i)
        {
            cyphertext += ( ( (plainchar[i] - 'a') + shift) %26) + 'a';
            plainchar += ( ( (plainchar[i] + 'a') - shift) %26) - 'a';
        }
        cout<< "Encrypted: " << cyphertext <<endl;
        cin.get();
        cout<< "Decrypted: " << plainchar <<endl;
        cin.get();
        return 0;
    }

  3. #18
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    *sigh* One last time...

    Decompose the long expressions and you will be able to test it yourself!
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #19
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    decompose definiton = to decay, putrefy.(lol) look i have tried this before what makes you think that it will work this time?

  5. #20
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Done it sorry 4 the arigance but it works except i cant remember how to flush a string?

  6. #21
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Code:
    std::cout << "flush me" << std::flush; //just flushes
    or

    Code:
    std::cout << "flush me" << std::endl; //outputs a newline and flushes
    or

    Code:
    std::cout << "flush me" << std::ends; //outputs a null and flushes
    or

    Code:
    std::cout << "flush me";
    std::cin >> val;   //flushes cout by calling cin
    or

    Code:
    int main() {
        std:: cout << "flush me";
        return 0;
    }  //flushes by function termination
    or

    Code:
    pull the lever or push the button

    Quote Originally Posted by L_U_K_E
    decompose definiton = to decay, putrefy.(lol) look i have tried this before what makes you think that it will work this time?
    If you don't understand mathematical terms, I suggest you move out from cyphers and do something else in your code.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #22
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    I sorted it anyway by just creating a new string and using that thanx for all of your help.Here is my code:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
        string cyphertext;
        string plainchar = "luke";
        string plainchar2;
        int shift = 2;
        int alphabet_size = plainchar.length();
        for (int i = 0; i < alphabet_size; ++i)
        {
            cyphertext += ( ( (plainchar[i] - 'a') + shift) %26) + 'a';
            plainchar2 += cyphertext[i] - shift;
        }
        cout<< "Encrypted: " << cyphertext <<endl;
        cin.get();
        cout<< "Decrypted: " << plainchar2 <<endl;
        cin.get();
        return 0;
    }
    However I am still having trouble when it comes to capitals, spaces and the letters 'a' and 'z' is that because i havn't linked it round?
    Last edited by L_U_K_E; 06-28-2006 at 08:18 AM.

  8. #23
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You don't need the fstream header.

    Place a cin.ignore() before your last cin.get(). If you input test on your first cin.get(), the second will not execute as you expected.

    It's also probably better you get used to replace the less-than operator in your for loops with the not-equal one. It's more intuitive. Although not necessarily for all situations.

    As a curiosity, if you want to have the actual caeser's cypher, shift by 3, not 2. That's how he did it. Or shift by 13 to produce the ROT13 cypher made common in signatures and such.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #24
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    Thanx for all your help and i ahve made those changes hopwever i get problems if i change "luke" to numbers or the letters 'a' or 'z' is that because i havn't linked it round?

  10. #25
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Time you try to do it yourself... give it a shot
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  11. #26
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, the problem is that your entire implementation relies on the assumption that there won't be anything but lower-case alphabetic characters in the string. It's the -'a' and +'a' parts.
    To do this properly, you have to treat each character according to its class: differ between upper-case, lower-case, digits and special characters, changing the class shift for the classes and leaving specials alone.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #27
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    So do you mean that instead of typing:
    Code:
    cyphertext += ( ( (plainchar[i] - 'a') + shift) %26) + 'a';
    I would type:
    Code:
            cyphertext += ( ( (plainchar[i] - '1') + shift) %9) + '1';
    For numbers or is that like completely wrong?

  13. #28
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    '0', but the principle is right. Now you just need to find a way to abstract the common code out of it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #29
    Registered User L_U_K_E's Avatar
    Join Date
    Apr 2006
    Posts
    106
    what do you mean by 'abstract the common code out of it'?(sorry about this but by asking questions it makes it easier to learn and not make the same mistakes again).

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, you could repeat the cipher code for every class of characters.
    Or you can find a way to write the common code once in a way that allows you to reuse it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Cipher Program
    By PunchOut in forum C Programming
    Replies: 7
    Last Post: 11-22-2008, 01:12 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. My little Caesar cipher emulator
    By dead_cell in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 01:05 AM