Thread: Hi, encryption program

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Hi, encryption program

    Hi im relatively new to programming and I am working on an encryption program. I know what I want to do but im a bit lost, youll understand when you see my code.

    This is just the start of my code by the way

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    
    {
    
        char unencrypted;
        char encrypted;
    
        cout <<  "Enter unencrypted English:"
                << endl;
        cin >> unencrypted;
    Basically, there will be no algorithm. I want to state like a= f, b = z, c =q and I want each letter in the code the user has submitted to be translated. Any advice would be greatly appreciated.

  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
    So basically you want to map between two character sets

    Code:
    char clear[] = "abc";
    char cypher[] = "fzq";
    Find the position of a letter in the clear array, and use that position to index the cypher array

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    3
    sorry i dont understand what you posted just then. Sorry but could you clarify

    thanks alot

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use a for loop

    As in
    for ( i = 0 ; i < n ; i++ ) if ( clear[i] == unencrypted )
    etc etc

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Sorry

    Sorry, i didnt get that either.

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    you can use a switch statement, that would make it real easy

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Last chance

    for ( i = 0 ; i < n ; i++ ) if ( clear[i] == unencrypted ) encrypted = cypher[i];

  8. #8
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Something like this
    Code:
    #include  <iostream>
    #include  <string>
    using namespace std;
    char Translate(char);
    
    
    
    
    int main() {
    int i;
    int length;
    string unencrypted;
    string encrypted;
    
    cout <<"Enter unencryped english:" <<endl;
    cin >> unencrypted;
    length=unencrypted.length();
    
    
    
    for(i=0; i < length; i++) {
    unencrypted[i]=Translate(unencrypted[i]); 
    }
    
    
    
    
    cout <<unencrypted;
    return 0;
    }
    
    
    
    
    
    
    char Translate(char a) {
    if(a == 'a') return 'a';
    if(a == 'b') return 'c';
    if(a == 'c') return '2';
    else return '.'; // default
    //and so on.....//
    }
    you understood?

    edit: Code tags added by Govtcheez - USE CODE TAGS, JERKS!
    edit2: Wait, there was no indentation to begin with...

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Now if you only could use code tags.

  10. #10
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    and instead of
    Code:
    char Translate(char a) {
    if(a == 'a') return 'a';
    if(a == 'b') return 'c';
    if(a == 'c') return '2';
    else return '.'; // default
    //and so on.....//
    use a switch statement because it's faster then checking all the ifs
    Code:
    char Translate(char a) {
    switch (a)
    {
         case 'a':
              return 'a';
              break;
         case 'b';
              return 'c';
              break;
         case 'c';
              return '2';
              break;
         // and so on
    }

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Da-Nuka
    Code:
    int length;
    
    length = unencrypted.length();
    To be correct, length should be described as string::size_type and not an int.

    Quote Originally Posted by Da-Nuka
    Code:
    string unencrypted;
    
    for(i=0; i < length; i++) {
    unencrypted[ i ]=Translate(unencrypted[ i ]); 
    }
    
    cout <<unencrypted;
    I think you meant encrypted instead of unencrypted above. Also, be careful, because unless you've allocated space for the string you are playing with fire. Easiest way to deal with that might be:

    Code:
    for(i=0; i < length; i++)
    {
        encrypted += Translate(unencrypted[ i ]); 
    }
    Personally I'd use a map<char,char> to store the unencrypted(key)/encrypted(value) pairs but that might be a bit advanced for a beginner.
    Last edited by hk_mp5kpdw; 01-03-2005 at 11:23 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    I am new to C++ programming and thought I would give this a try. I
    think this is what you were wanting to do. All you have to do is change what maps to what.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    string text, encText;
    char translate(char a)
    {
         switch(a)
         {
                   case 'a':
                        return 'b';
                   case 'b':
                        return 'c';                    
                   case 'c':
                        return 'd';
                   case 'd':
                        return 'e';
                   case 'e':
                        return 'f';                    
                   case 'f':
                        return 'g'; 
                   case 'g':
                        return 'h';
                   case 'h':
                        return 'i';                    
                   case 'i':
                        return 'j';
                   case 'j':
                        return 'k';
                   case 'k':
                        return 'l';                    
                   case 'l':
                        return 'm'; 
                   case 'm':
                        return 'n';
                   case 'n':
                        return 'o';                    
                   case 'o':
                        return 'p';
                   case 'p':
                        return 'q';
                   case 'q':
                        return 'r';                    
                   case 'r':
                        return 's'; 
                   case 's':
                        return 't';
                   case 't':
                        return 'u';                    
                   case 'u':
                        return 'v';
                   case 'v':
                        return 'w';
                   case 'w':
                        return 'x';                    
                   case 'x':
                        return 'y';  
                   case 'y':
                        return 'z';                    
                   case 'z':
                        return 'a';                                                                                                   
                   default:
                        return ' ';     
         }
    }
    int main()
    {
        cout << "Enter the string to be encripted:\n";
        cin >> text;
    
        for (int i = 0; i < text.size(); i++)
        {
            encText += translate(text[i]);
        }
        cout << encText << endl;
        cin >> encText;
    }
    Last edited by Loctan; 01-03-2005 at 03:07 PM.

  13. #13
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    In theory, that would work, but it's a bit lengthy. If you wanted efficiency, merely add one to each character in the word, as you can see in my(more like Thantos' fix of) program, that would cut off a heck of a lot of program sizeness in the other. It's in the Encryption Algorithm thread.

  14. #14
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Thanks for the tip. I didn't know that you could treat the characters as integers and just add one. For anyone who doesn't understand, essentially my entire translate function could be changed to:

    Code:
    char translate(char a)
    {
         return (a + 1);
    }

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Don't forget to add a conditional before your return statement: z doesn't fit in with the rest of the alphabet in this case. I hope you keep going with this project and add some features like handling upper AND lower case.

    edit: Most of the upper case alphabet will also be handled by adding one, but I meant to account for z and Z.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption Program (help me please)
    By Arkanos in forum Windows Programming
    Replies: 7
    Last Post: 10-30-2005, 08:01 PM
  2. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  3. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM