Types of Encryption in C++

This is a discussion on Types of Encryption in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, i was just wondering what types of encryption can be done in C++ and aren't fairly complicated to ...

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

    Types of Encryption in C++

    Hi Guys, i was just wondering what types of encryption can be done in C++ and aren't fairly complicated to learn (im new to this). Cryptology has always held my interest but i can never find information on the internet on how to do it with C++. Any methods, advice or information would be greatly appriciated!

    Thanks

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    I would think any type of encryption can be implemented in C++, it is only a matter of how complex that implementation is. One of the easiest would probably be XOR encryption. I am sure there are a couple posts floating around here somewhere that discuss it... why not do a search of the boards here and find out?

    Edit: Here is a thread that I posted in a while back that asks the user for a string and a key to encrypt that string with. It then displays the encrypted string.

    Also, here is another thread in which I posted some code for a command line run program. It asks for an input file, and an output file (to be created by the program). The program takes the input file and encrypts it (creating the output file) using a hardcoded key. It can easily be modified to accept a third command line argument with a key that could be used to encrypt the file instead of the hardcoded key.
    Last edited by hk_mp5kpdw; 12-20-2004 at 02:00 PM.
    "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

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> but i can never find information on the internet on how to do it with C++

    really?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Dec 2004
    Location
    Mercer, PA
    Posts
    25
    Hmm, if you remember all characters are really numbers. Like null = 0 , a = 97, b = 98. (NOTE: goto msdn library and search for ASCII, microsoft provides the codes - hex, dec, and char values). So really you could do something like this...


    Code:
    char x = 98 // or x = 'b'
    char++ // x now = 'c'
    Now of course there is better ways - but that is just one very simple way for encrption to be used at a personal level.
    There are books that talk about different encryption algorithms - just give it some thought.

    It would help if you made your own class for encrption - I made one called Energy and it takes care of everything for me. I simply tell it a filename - password and it does the rest.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Quote Originally Posted by Junior89
    Cryptology has always held my interest but i can never find information on the internet on how to do it with C++.
    Simple encryption in C++ is easy.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    namespace {
        string keys = "~!@#$%^&*()_+|}{\":?><,./;\'][=-`";
    
        string::value_type key(string::size_type i)
        {
            return keys[i % keys.length()];
        }
    }
    
    int main()
    {
        string s;
    
        cout<<"Enter string: ";
        if (!getline(cin, s)) {
            cerr << "getline failure" << endl;
            return 1;  // Ren is lazy
        }
    
        cout << "Encryption..." << endl;
        for (string::size_type i = 0; i < s.length(); ++i)
            s[i] ^= key(i);
        cout << s << endl;
    
        cout << "Decryption..." << endl;
        for (string::size_type i = 0; i < s.length(); ++i)
            s[i] ^= key(i);
        cout << s << endl;
    
        return 0;
    }
    Stronger encryption is harder to learn from the web. Good encryption is even harder. Maybe a book would be better.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Just to go along with encryption any compression methods, information, reccomendations, etc.. Would also be very helpful. I haved looked a little bit into Huffman's Compression but that's about it. I do know however that a good encryption program also compresses what it encrypts. So any info would be greatly appreciated! Thank you in advance!

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    My opinion might be influenced because I just finished a class in assembly.. but I think encryption at the assembly level is awesome.. you have total control over bit manipulation.. you can bascially derive your own scheme of bit rotations.. and also apply XOR masks. With even just a few manipulations you can come up with encryption that is almost totally unbreakable.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,924
    Just to go along with encryption any compression methods, information, reccomendations, etc.. Would also be very helpful.
    Take a look at the websites listed on dmoz's Cryptography directory, in particular the Cryprography FAQ (though it is inaccurate in some areas).

    Later, you might want to try out the Crypto++ library. I'm not sure how good it is, but it is a C++ library that implements a number of crypto schemes.
    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

  9. #9
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by The Brain
    My opinion might be influenced because I just finished a class in assembly.. but I think encryption at the assembly level is awesome.. you have total control over bit manipulation.. you can bascially derive your own scheme of bit rotations.. and also apply XOR masks. With even just a few manipulations you can come up with encryption that is almost totally unbreakable.
    You should have more than enough control over bits using C++'s bitwise operators:
    & (bitwise and),
    | (bitwise or),
    ^ (bitwise xor),
    ~ (1's complement aka bitwise not),
    << (bit shift left),
    >> (bitshift right).

    With these you can do whatever you want to single bytes. Manipulating the individual bits inside the bite requires a little thought but is not difficult at all.

    @The Brain - Unbreakable encryption schemes are not made by just throwing random bitwise operations on the data. Read a good book on encryption to see why.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Any book reccomendations?

  11. #11
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    c++ offers a lot of bitwise operations.. but not nearly as much as assembly has to offer. The IA-32 instruction set for example also has:

    ROR - Rotate Right
    ROL - Rotate Left
    RCR- Rotate and Carry Right
    RCL - Rotate and Carry Left
    SRC - shift right and carry
    SLC - shift left and carry

    (this is all i can think of off the top of me' head)


    Let's say your password is "braniac".. with just a few bit manipulations, you can make your password appear like this after compilation:

    10010111110010101010111111001010101011111111110101 01010101000000000000000011011010101101111110101001 01010111010101000001010101010001010111011111111110 01010101000101010101000001010111111000010110100010 01001010110100101010101010101010000000101110101011 01000000000000010101011100101011001101001001010010 10101010111110101010101010101010101010101000010101 01011111001011111001010101011111100101010101111111 11101010101010100000000000000001101101010110111111 01010010101011101010100000101010101000101011101111 11111100101010100010101010100000101011111100001011 01000100100101011010010101010101010101000000010111 01010110100000000000001010101110010101100110100100 10100101010101011111010101010101010101010101010100 00101010101111100101111100101010101111110010101010 11111111110101010101010000000000000000110110101011 01111110101001010101110101010000010101010100010101 11011111111110010101010001010101010000010101111110 00010110100010010010101101001010101010101010100000 00101110101011010000000000000101010111001010110011 01001001010010101010101111101010101010101010101010 10101000010101010111110010111110010101010111111001 01010101111111111010101010101000000000000000011011 01010110111111010100101010111010101000001010101010 00101011101111111111001010101000101010101000001010 11111100001011010001001001010110100101010101010101 01000000010111010101101000000000000010101011100101 01100110100100101001010101010111110101010101010101 01010101010100001010101011111001011111001010101011 11110010101010111111111101010101010100000000000000 00110110101011011111101010010101011101010100000101 01010100010101110111111111100101010100010101010100 00010101111110000101101000100100101011010010101010 10101010100000001011101010110100000000000001010101 11001010110011010010010100101010101011111010101010 10101010101010101010000101010101111100101111100101 01010111111001010101011111111110101010101010000000 00000000011011010101101111110101001010101110101010 00001010101010001010111011111111110010101010001010 10101000001010111111000010110100010010010101101001 01010101010101010000000101110101011010000000000000 10101011100101011001101001001010010101010101111101 01010101010101010101010101000010101010111110010111 11001010101011111100101010101111111111010101010101 00000000000000001101101010110111111010100101010111 01010100000101010101000101011101111111111001010101 00010101010100000101011111100001011010001001001010 11010010101010101010101000000010111010101101000000 00000001010101110010101100110100100101001010101010 11111010101010101010101010101010100001010101011111 00101111100101010101111110010101010111111111101010 10101010000000000000000110110101011011111101010010 10101110101010000010101010100010101110111111111100 10101010001010101010000010101111110000101101000100 10010101101001010101010101010100000001011101010110 10000000000000101010111001010110011010010010100101 01010101111101010101010101010101010101010000101010 10111110010111110010101010111111001010101011111111 11010101010101000000000000000011011010101101111110 10100101010111010101000001010101010001010111011111 11111001010101000101010101000001010111111000010110 10001001001010110100101010101010101010000000101110 10101101000000000000010101011100101011001101001001 01001010101010111110101010101010101010101010101000 01010101011111001011111001010101011111100101010101 11111111101010101010100000000000000001101101010110 11111101010010101011101010100000101010101000101011 10111111111100101010100010101010100000101011111100 00101101000100100101011010010101010101010101000000 01011101010110100000000000001010101110010101100110 10010010100101010101011111010101010101010101010101 01010000101010101111100101111100101010101111110010 10101011111111110101010101010000000000000000110110 10101101111110101001010101110101010000010101010100 01010111011111111110010101010001010101010000010101 11111000010110100010010010101101001010101010101010 10000000101110101011010000000000000101010111001010 11001101001001010010101010101111101010101010101010 10101010101000010101010111110010111110010101010111 11100101010101111111111010101010101000000000000000 01101101010110111111010100101010111010101000001010 10101000101011101111111111001010101000101010101000 00101011111100001011010001001001010110100101010101 01010101000000010111010101101000000000000010101011 10010101100110100100101001010101010111110101010101 01010101010101010100001010101011111001011111001010 10101111110010101010111111111101010101010100000000 00000000110110101011011111101010010101011101010100 00010101010100010101110111111111100101010100010101 01010000010101111110000101101000100100101011010010 10101010101010100000001011101010110100000000000001 01010111001010110011010010010100101010101011111010 10101010101010101010101010000101010101111100101111 10010101010111111001010101011111111110101010101010 00000000000000011011010101101111110101001010101110 10101000001010101010001010111011111111110010101010 00101010101000001010111111000010110100010010010101 10100101010101010101010000000101110101011010000000 00000010101011100101011001101001001010010101010101 11110101010101010101010101010101000010101010111110 01011111001010101011111100101010101111111111010101 01010100000000000000001101101010110111111010100101 01011101010100000101010101000101011101111111111001 01010100010101010100000101011111100001011010001001 00101011010010101010101010101000000010111010101101 00000000000001010101110010101100110100100101001010 10101011111010101010101010101010101010100001010101 01111100101111100101010101111110010101010111111111 10101010101010000000000000000110110101011011111101 01001010101110101010000010101010100010101110111111 11110010101010001010101010000010101111110000101101 00010010010101101001010101010101010100000001011101 01011010000000000000101010111001010110011010010010 10010101010101111101010101010101010101010101010000 10101010111110010111110010101010111111001010101011 11111111010101010101000000000000000011011010101101 11111010100101010111010101000001010101010001010111 01111111111001010101000101010101000001010111111000 01011010001001001010110100101010101010101010000000 10111010101101000000000000010101011100101011001101 00100101001010101010111110101010101010101010101010 10100001010101011111001011111001010101011111100101 01010111111111101010101010100000000000000001101101 01011011111101010010101011101010100000101010101000 10101110111111111100101010100010101010100000101011 11110000101101000100100101011010010101010101010101 00000001011101010110100000000000001010101110010101 10011010010010100101010101011111010101010101010101 01010101010000101010101111


    Last edited by The Brain; 12-21-2004 at 01:05 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  12. #12
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    Quote Originally Posted by The Brain
    ROR - Rotate Right
    ROL - Rotate Left
    RCR- Rotate and Carry Right
    RCL - Rotate and Carry Left
    SRC - shift right and carry
    SLC - shift left and carry
    Any of these operations can be obtained by combining the ones built in c/c++.

  13. #13
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    perhaps bit manipulation by itself would not be enough to provide 100% encryption.. perhaps there is some disgruntled microsoft engineer out there who would be able to sift through the exe binary and rebuild the encryption scheme... however I think just with bit manipulation alone would be enough to stop 99% of the world population from obtaining a password.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  14. #14
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    When I said that bit manipulation by itself was not enough I didn't mean that you need some weird magic technique to encrypt data (in the end any computer operation is bit manipulation). What I meant is that you need to do it after careful consideration of the algorithm you are going to use.

    Good encryption doesn't symply change each byte to another byte accoridng to some rule. For good encryption you should also break patterns in the data and add redundancy so that changes in the source data cannot be directly mapped to the encrypted data and viceversa.

  15. #15
    Registered User Kybo_Ren's Avatar
    Join Date
    Sep 2004
    Posts
    136
    If you want some good encryption, check out the RSA scheme.

    Of course, C++ only has built-in types of up to 64 bits (on most platforms), but you can implement your own prime sieve and number system.

    With good very long prime keys (1024 bits+), your encryption will be essentially uncrackable.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 11:57 AM
  2. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  3. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  4. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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