Unknown problem in first encryption algorythm

This is a discussion on Unknown problem in first encryption algorythm within the C++ Programming forums, part of the General Programming Boards category; Hi all. I just recently found an old encryption program I gave up on back in highschool on an old ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    38

    Unknown problem in first encryption algorythm

    Hi all. I just recently found an old encryption program I gave up on back in highschool on an old jump drive. I was wanting to teach myself about typecasting, file I/O, and a little encryption all in the same project. I actually wrote two programs. The first takes in a phrase one letter at a time and converts them to numbers through typecasting. Next, It adds whatever number is in the file "numerical.txt" and writes the resulting number to a file called "example.txt". Whenever it comes time to undo what has been done, the second program simply reverses the process by reading from the two files and subtracting. Problem is, instead of perfectly legible letters, I simply get the symbol for null (I believe that's what it is). When I open up example.txt, the numbers are huge. (In the hundreds of thousands, instead of the standard ascii), so I'm assuming the problem is in the encryption algorithm. I'd love to finally be able to get this program to work. Anyway, here's the code:


    Encryptor:
    Code:
    
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    #include <windows.h>
    #include <process.h>
    using namespace std;
    
    
    
    
    int main()
    {
        cout<<"Personal encryptor, v 0.2\n";
        cout<<"Andrew Hodge. June 10, 2011. \n \n \n" << endl;
    
    int str;
    int alexandria;
    char input;
    int oxnhill;
    int laurel;
    oxnhill = 1;
    washington:
    
    ifstream c_file("numerical.txt");
    c_file>>laurel;
    c_file.close();
    
    
    while (oxnhill< 0){
    
    ofstream a_file ("example.txt", ios::app);
    a_file<<  "Unit line______Score_________\n";
    a_file.close();
    
    //oxnhill++;
    
    goto washington;
    
    }
        cin>>input;
        if (input==99){
    
        goto arnold_sw;
    
        }
        else{
      alexandria=(int)input ;
    
      ofstream a_file ( "example.txt", ios::app );
    a_file<<alexandria + laurel;
    a_file<<"\n"
      a_file.close();
        goto washington;
        }
    
        arnold_sw:
    
        return 0;
    
    }
    Decryptor:
    Code:
    #include <fstream>
    #include <iostream>
    #include <cstdlib>
    #include <stdio.h>
    #include <windows.h>
    #include <process.h>
    using namespace std;
    
    int main(){
    
        int alexandria;
        int laurel;
        int baltimore;
        int patomic;
        ifstream a_file ("numerical.txt");
        a_file>>laurel;
        a_file.close();
    
        ifstream b_file ("example.txt");
        b_file>>alexandria;
        b_file.close();
        baltimore= alexandria-laurel ;
    
       patomic= baltimore;
       cout<< (char) patomic ;
    
    
    
    }
    Last edited by ashinms; 06-16-2011 at 02:35 PM. Reason: ease of reading

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't see you reading words, or even characters, from a file. Is that what you intended to do? If so, you should maybe do that.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    I thought that was what I was doing. Like I said. This is from highschool. I haven't seriously written code since then. I'm just now getting back into it.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Okay for some reason I thought you were trying to read from a file. Right now you're reading characters from the keyboard until somebody hits c (which is 99). What value is your "encryptor"? If that's several hundred thousand, then that would explain what you're getting.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    Ha. No. I'm currently using 15.


    and I'm also working on a string like "end" instead of c.
    Last edited by ashinms; 06-19-2011 at 01:34 PM. Reason: additional information

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Opening your file for every single character is an extraordinarily stupid idea, but I can't see that it would actually do something weird here. You should probably have your program print out the value of laurel to make sure it really is 15 like you want it to be. (I would expect if you have difficulty opening the file, it would crash instead of just keep on, but you never know. Speaking of, you should be making sure that all your file opens actually happen.)

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    The reason I was reading one character at a time was that I had originally planned to increment "encryptor" by one each time a char was handled, but I never implemented it. Oh well. Problem solved... Thanks anyways...

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Wow, just... wow!
    Turning ifs into loops, and turning loops into gotos...
    I'm not even sure what manner of unholyness is going to occur due to using a goto to jump back before the definition of some local variables that are not POD types. I'm not sure I even want to know.

    Every time you use a goto, someone kills a kitten. Please make it stop!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    Just wanted to redeem myself. This is the completely rewritten- from scratch- program. No gotos, a clean flow, and all under one process. I got rid of all the header files I didn't need. It still needs some polish, that's for sure, and it will never be a "legit" encryption program... It was never intended to be... just a learning experience. It still reads from the file one line at a time... In a later version, the output and input will be slowly incremented in order to further complicate the encryption. I also intend to implement multithreading some day.

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    int main(){
    
    char washington;
     int alexandria;
     int arlington;
     int oxonhill;
     int silverspring;
     int bolling;
     int laurel;
    
    
    
    cout<<"P_encrypt\n";
    cout<<"Enter encryptor";
     cin>>alexandria;// My "encryptor". Instead of file input, I decided to do something a bit more "on my level"
    cout<<"Encryptor has been set to "<<alexandria<<endl;
    cout<<"Enter data to encrypt, one letter at a time. Enter letter [q] to goto decryptor."<<endl;//No, that is not a goto statement.
     do {
    
    cin>> washington;
    
        arlington= (int)washington+alexandria;
    
     ofstream one("store.txt", ios::app);
     one<<arlington<<"\n";
     one.close();
    cin.get();
     }
      while (washington!=113);// Until I figure out how to typecast an entire string (like "quit") into an int,
      // "q" will have to do.
    
    //Program automatically goes to this when encryption loop ends, rigt?
    
    cout<<"Entering decryption mode. Please enter decryptor";
    cout<<" (same as encryptor when file was written to.)";
    cin>>oxonhill;
    ifstream two("store.txt");
    if (two.is_open())
    do{
    two>>laurel;
    laurel- oxonhill;
    cout<<(char)laurel;
    
    
    cin>>bolling;
    
    }
    
    while(bolling!=100);
    
    }

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,597
    The most obviously thing lacking now is good formatting, e.g.,
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
        char washington;
        int alexandria;
        int arlington;
        int oxonhill;
        int silverspring;
        int bolling;
        int laurel;
    
        cout << "P_encrypt\n";
        cout << "Enter encryptor";
        cin >> alexandria;// My "encryptor". Instead of file input, I decided to do something a bit more "on my level"
        cout << "Encryptor has been set to "<<alexandria<<endl;
        cout << "Enter data to encrypt, one letter at a time. Enter letter [q] to goto decryptor." << endl;//No, that is not a goto statement.
        do {
            cin >> washington;
            arlington = (int)washington + alexandria;
            ofstream one("store.txt", ios::app);
            one << arlington << "\n";
            one.close();
            cin.get();
        }
        while (washington != 113);// Until I figure out how to typecast an entire string (like "quit") into an int,
        // "q" will have to do.
    
        //Program automatically goes to this when encryption loop ends, rigt?
    
        cout << "Entering decryption mode. Please enter decryptor";
        cout << " (same as encryptor when file was written to.)";
        cin >> oxonhill;
        ifstream two("store.txt");
        if (two.is_open())
        do {
            two >> laurel;
            laurel - oxonhill;
            cout << (char)laurel;
            cin >> bolling;
        }
        while (bolling != 100);
    }
    Next, what's with those variable names? You should use variable names that are descriptive.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    38
    Formatting. Got it. Variable names are cities near where I used to work.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    They're better than single character variables, but not by much. You should name variables after the information they store.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unknown problem!
    By kingdede231 in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2009, 06:13 AM
  2. Search() algorythm
    By Ducky in forum C++ Programming
    Replies: 17
    Last Post: 01-18-2008, 05:41 PM
  3. distance algorythm help
    By MicroFiend in forum Game Programming
    Replies: 9
    Last Post: 06-12-2004, 09:28 PM
  4. parameters algorythm
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 12-02-2003, 06:18 PM
  5. a segfaulting algorythm
    By demonus in forum C Programming
    Replies: 8
    Last Post: 08-11-2003, 08:06 AM

Tags for this Thread


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