Thread: Encoding program

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    4

    Encoding program

    Hey,

    I've been doing really basic programs for fun. This one has me stumped though.

    I'm trying to write an encoding and decoding program. I'm sure I can get the decoding if I can just get it to encode right.ber.

    Basically what I'm trying to do is assign random numbers to the letters of the alphabet. Like a = 6, b = 98 etc etc. Then I will have the user input the letter and I can't figure out for the life of me how to take that letter and assign it to its number. Then If i could get that to work I will have an encoding number that will be multiplied times the number which will then output. For decoding I can just reverse the process.

    Also, this is just letters, if you guys have any suggestions for words that would be great too.

    Sorry for the noob question, I'm trying to teach myself C++ and it gets a little complicated. But you guys seem pretty knowledgable

    Love the tutorials by the way!
    I'll post my code so you can get an idea of what I'm trying to do.

    Code:
    #include "Library.h"
    
    int main()
    {
    	char letter;
    	cout << "Enter a letter\n";
    	cin >> letter;
                   /*
                   Somewhere in here I need
                   to assign the letter a number so 
                   I can multiply it out....
                   */
                   int message;
    	int encodingnum;
    	encodingnum = 384739;  // Any random number wil work
    	message = letter * encodingnum;
    	cout << "Encoded number is\n" << message << endl;
    	return main();
    }
    Anyway,
    Thanks in advance,
    Nate
    Last edited by natey14; 01-13-2006 at 01:50 PM.

  2. #2
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    the faq for generating random numbers would be a start..

    here is a quick start of getting an array of numbers.. very rough but hopefully will give you an idea or 2...
    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main(){
        srand(time(0));
        int arr[26];
        for(int i=0; i<26; ++i){ 
            arr[i] = rand() % 100; // number between 0 -100
            cout << static_cast<char>(97+i) << " " <<  arr[i] << endl;
        }
        return 0;
    }
    <edit>you still have to consider that you will need to check that there are no duplicates, and also preserve this *random* encoding key so that it can be decoded.</edit> and fixed code up.
    Last edited by xhi; 01-13-2006 at 02:13 PM.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    4
    Ya thats the thing about random numbers....It would be a lot harder to decrypt but hard to code. Ok that gives me some ideas, thanks

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Well your in my neck of the woods, i personally love Encryption and Ciphers. Now i have moved on to Windows Programming (i am learning now) but i spent a long time writing some basic encryption schemes. I understand what you are trying to do but i have a question for you. Firstly the algorithm you are implementing, are you trying to make it secure or just for fun? Because this algorithm (if you can call it that) that i have shown here is not very secure but fun.

    Have you tried an XOR program yet? If you are interested read up on One-Time Pads, they are the only Encryption Schemes that can NEVER be broken, and the programming is quite simple. However if you would like to keep at your current project heres how i would do it. Its a bit more complicated than you may be able to understand now but soon if you look into each aspect of it it shouldn't be too difficult to comprehend. You may want to add/take away from it what you like. I just whipped it up but it should work well. You should be able to make a more efficient/less memory consuming version soon, i could have but i was rushing to complete it . Before you start using it though try to understand it. What it does is take all of the letters of the alphabet, turns them into their ASCII values then multiplies that number by a random one. The only thing you need to decrypt is to know the random number. Once again this isnt very secure, but it is fun . Next you could try to add a function that lets someone type and message and then encrypt the message, after that you could add a function that converts all the encrypted numbers back to letters of the alphabet. There are a lot of way to manipulate the program so have fun!

    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    void encrypt(int randomnumber, int numarray[30], char alphabet[30]);
    
    int main()
    {
        char alphabet[]="abcdefghijklmnopqrstuvwxyz";
        int numarray[30];
        int randomnumber;
        int mchoice;
        
        //infinite loop
        while(1)
        {
            //clearscreen
            system("cls");
            //title
            cout<<"Random Number Encryption Scheme\n";
            cout<<"===============================\n\n";
            //menu
            cout<<"(1) Encrypt\n";
            cout<<"(2) Decrypt\n";
            cout<<"(3) Exit\n\n";
            //get choice
            cout<<"Choice: ";
            cin>>mchoice;
            cin.ignore();
            //evaluate
            if(mchoice==1)
            {
                //ENCRYPT
                srand(time(0));
                randomnumber=(rand() % 100);
                encrypt(randomnumber, numarray, alphabet);
                cout<<"Encrypted numbers:\n";
                cout<<"Random Number is "<<randomnumber<<"\n";
                for(int i=0; i<26; i++)
                {
                    cout<<alphabet[i]<<" - "<<numarray[i]<<"\n";
                }
                cout<<"\n\nPress Enter to Continue...";
                cin.ignore();    
                continue;
            }
            else if(mchoice==2)
            {
                //DECRYPT
                cout<<"Enter Number: ";
                cin>>randomnumber;
                cin.ignore();
                encrypt(randomnumber, numarray, alphabet);
                for(int j=0; j<26; j++)
                {      
                    cout<<numarray[j]<<" - "<<alphabet[j]<<"\n";
                }    
                cin.ignore();
                continue;
            }
            else if(mchoice==3)
            {
                //Exit
                cout<<"Press Enter To Exit...";
                cin.ignore();
                break;
            }
            else{
                cout<<"Incorrect Choice!";
                cin.ignore();
                continue;
            }
        }
        return 0;
    }    
    
    void encrypt(int randomnumber, int numarray[27], char alphabet[27])
    {
        for(int i=0; i<26; i++)
        {
            if(alphabet[i]=='\0')
            {
                break;
            }    
            numarray[i]=((int)alphabet[i] * randomnumber);
        }
    }
    Good luck to you!
    Last edited by Junior89; 01-14-2006 at 01:32 AM.
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program quits unexpected
    By skelesp in forum C Programming
    Replies: 34
    Last Post: 12-10-2008, 09:10 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  4. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM