Thread: Encryption questions...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Encryption questions...

    Hi guys, it's been a while! Anyways, ive been dying to find a good tutorial or something on the begginner-intermediate level in encryption. I cant seem to find much on it though. Ive already done numerous tuts on X-OR and i did one on 64-bit but it was very vague. Can anyone point me in the right direction? Thanks!

    PS-Also, im sure this gets asked alot but is there any special procedure for saving files to a Windows XP equipped machine? When i co ofstream ("whatever"") it doesnt save. Any ideas? Thanks a bunch!

  2. #2
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you mean

    ofstream out("filename.ext");
    /*
    this will open a file in the same
    directory as your program under the filename.ext , unless
    you run the program from debug mode (at least under .NET)
    then it will be in your project directory instead of the debug/release folder.
    */


    that will open a file as long as you have

    #include <fstream>


    out << "words";


    will write to the file,

    as for tutorials on encryption never really looked at
    one, you can see some code i and some other people posted
    for a encryption contest, in the contest boards on this
    forum.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    ?

    Thats what i meant, its been awhile and its all starting to come back to me. so even if i put

    Code:
    ...
    ofstream fout("c:/my documents/whatever...");
    fout<<"Words...";
    fout.close() 
    ...
    It wouldnt save it in the specified Directory?

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    "c:\\my documents\\whatever\\whatever.txt"


    the directory must exsist for this operation,
    if you need to create a directory should look
    up CreateDirectory() i believe that it for windows.

  5. #5
    Registered User ~Kyo~'s Avatar
    Join Date
    Jun 2004
    Posts
    320
    Quote Originally Posted by ILoveVectors
    "c:\\my documents\\whatever\\whatever.txt"


    the directory must exsist for this operation,
    if you need to create a directory should look
    up CreateDirectory() i believe that it for windows.
    "C:/my documents/whatever/whatever.txt" should work as well... I posted something about \\ and / a day or two ago...

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    I couldnt really find any usefull info on encryption over the net so I got a couple books, just look at amazon.com and you should find some good ones with good reviews.

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Some good beginner books:
    Modern Cryptography by Wenbo Mao
    Applied Cryptography by Bruce Schneier
    and, of course, the freely available Handbook of Applied Cryptography
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Quote Originally Posted by Zach L.
    Some good beginner books:
    Modern Cryptography by Wenbo Mao
    Applied Cryptography by Bruce Schneier
    and, of course, the freely available Handbook of Applied Cryptography
    Yeah i got Applied Crytography but i cant make heads or tails of it past like Protocals, very Complicated MAthematics and not very clear, i will the the free one you mentioned, thanks!

    BTW - Modulus is % in C++ right? (And is Modulus and Modulo the same thing?)

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You're going to have to delve into the mathematics to learn more about cryptography. Look for some abstract / modern algebra books. Dover sells some realtively inexpensively -- some are quite clear, others are extremely difficult to slog through.

    x = y mod M
    read: x is congruent to y mod (or modulo) M
    M is the modulus

    % is the mod operator in C/C++, returning the least of the set [ x mod M ].
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ive passed, Pre-Algebra, Honors Algebra I, and Honors Algebra II with an A average. But the mathematics are only a small part of the confusion. The diagrams in the book arent very concise and it doesnt show mathematically the algorithm for most of them. And even if i do figure out how it works implementing it is a whole other story. Anyway i decided to give One-Time Pads a try. Heres my code, i need someone to look it over for me, dont worry its organized (perhaps too organized and commented) I cant get it to work properly. If the letter O gets passed through it when it is decryhpted it becomes a 5. A few other letters do this to me but i cannot decipher (thats a pun) what the problem is. Have a look please. Also ill have a look at the free one form the internet perhaps its more concise. Thanks again!

    Code:
    #include <iostream>
    #include <fstream>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
        //infinite loop
        while(1)
        {
            //clear the screen
            system("cls");
            //title
            cout<<"++++++++++++++++++++\n";
            cout<<"+   ONE-TIME PAD   +\n";
            cout<<"+      V. 1.0      +\n";
            cout<<"++++++++++++++++++++\n";
            cout<<"\n\n";
            //menu
            cout<<"[1] Encrypt\n";
            cout<<"[2] Decrypt\n";
            cout<<"[3] Help Info\n";
            cout<<"[4] Exit\n\n";
            //choose
            int mchoice;
            cout<<"Choice: ";
            cin>>mchoice;
            //evaulate
            if(mchoice==1)
            {
                //encrypt
                //clear input buffer
                cin.ignore();
                //init vars
                char ptext[56];
                char key[56];
                char ctext[56];
                int gchoice;
                //begin process
                //get plaintext
                cout<<"Plain Text: ";
                cin.getline(ptext, 56);
                //set ints accordingly
                int j = strlen(ptext);
                int i = strlen(ptext);
                cout<<"\nGenerate Key?(1=Yes, 2=No): ";
                cin>>gchoice;
                //evaluate
                time_t seconds;
                time(&seconds);
                srand((unsigned int) seconds);
                if(gchoice==1)
                {
                    //generate random key
                    cin.ignore();
                    for(int k=0; k<j; k++)
                    {
                        key[k]=char((rand()%26)+65);
                    }
                    cout<<key; 
                    cin.ignore();   
                }
                if(gchoice==2)
                {
                    cin.ignore();
                    //enter your own key
                    cout<<"\nKEY: ";
                    cin.getline(key, 56);
                }
                //encrypt
                for(int l=0; l<i; l++)
                {
                    ctext[l]=((ptext[l]+key[l])%26)+65;
                }
                cout<<ctext;
                cin.ignore();               
                continue;
            }
            if(mchoice==2)
            {
                //decrypt
                //clear input buffer
                cin.ignore();
                //define all Decryption vars
                char Dptext[56];
                char Dkey[56];
                char Dctext[56];
                //gather info.
                cout<<"Cipher Text: ";
                cin.getline(Dctext, 56);
                cout<<"Key: ";
                cin.getline(Dkey, 56);
                //check values
                if(strlen(Dctext)==strlen(Dkey))
                {
                    //begin decrypt
                }else{
                    cout<<"Incorrect Alignment of Key/Cipher.";
                    cin.ignore();
                    continue;
                }        
                //decrypt
                for(int z=0; z<(strlen(Dkey)); z++)
                {
                    Dptext[z]=(((Dctext[z]-Dkey[z])%26)+65);
                }
                cout<<"\n\nDONE-----\n\n";
                cout<<Dptext;
                cin.ignore();    
                continue;
            }
            if(mchoice==3)
            {
                //help info
                cin.ignore();
                ifstream hin("help.txt");
                char hh;
                while(hin.get(hh))
                {
                    cout<<hh;
                }
                cin.ignore();
                continue;
            }        
            if(mchoice==4)
            {
                //exit
                cout<<"Exitting...";
                break;
            }
            else{
                //invalid response
                cout<<"\n\nINCORRECT RESPONSE\n\n\a";
                cin.ignore();
                cin.ignore();
                continue;
            }
        }
        return 0;
    }
    PS-Its not finished yet, im going to implement saving, loading etc. thats why thre fstream and windows headers are included but not implemented... yet. Thanks again!

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Ive passed, Pre-Algebra, Honors Algebra I, and Honors Algebra II with an A average.
    Different algebra (or rather, what you learn in mid/high school is a very small concrete example of algebra). Abstract algebra as in group theory, ring theory, field theory, etc is what you need. Groups in particular, are simple in concept, and many important theorems are quite easy to understand (and prove), but are necessary for cryptography.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Quote Originally Posted by Junior89
    Ive passed, Pre-Algebra, Honors Algebra I, and Honors Algebra II with an A average.

    Ive passed Pre-Algebra, Algebra I, Algebra 2, Algebra3,
    geometery, trigonometry, calculus and college algebra
    with all A's, not to mention i took Alg2 and Geo at the same
    time.

    but im sure that not very impressive im sure some the guys
    here have had up to calculus V or better.

    oh and the college alg i took online.

  13. #13
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    BTW - Modulus is % in C++ right?
    C++ doesn't have a native modulus, as it's used mathematically, but the % (remainder) operator comes close enough almost all of the time.

    (And is Modulus and Modulo the same thing?)
    It depends on who you ask. The most widely accepted description among programmers, that I know of, is that modulo is the operation that repeatedly divides by M until there is a remainder less than M and the modulus is M.
    Just because I don't care doesn't mean I don't understand.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Ok can anyone figure out why my code isnt working? Zach perhaps you could provide an example of one of these important easy to understand theories so i can research it further. Thanks!

  15. #15
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Ok can anyone figure out why my code isnt working?
    You shift your range by using the remainder operator. That makes it harder to restore the cipher text. A much easier approach for the OTP is XORing the plain text with the key:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main() {
      char ptext[56] = "This is a test";
      char key[56], ctext[56], dptext[56];
      size_t len = strlen(ptext);
      size_t i;
    
      // Generate random key
      for (i = 0; i < len; i++)
        key[i] = rand();
    
      cout << ptext << '\n';
    
      // Encrypt
      for (i = 0; i < len; i++)
        ctext[i] = ptext[i] ^ key[i];
    
      cout << ctext << '\n';
    
      // Decrypt
      for (i = 0; i < len; i++)
        dptext[i] = ctext[i] ^ key[i];
    
      cout << dptext << '\n';
    
      return 0;
    }
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 10:16 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 06:45 PM