Thread: Data Encapsulation, String Classes & Text Encryption in C++

  1. #1
    Registered User
    Join Date
    Nov 2004

    Data Encapsulation, String Classes & Text Encryption in C++


    I am having trouble getting a jump start on this rather complicated program involving C++ classes and string manipulation in string classes.

    I am attempting to write a C++ program that contains a Class called Encrypt. This should contain a line of text as its private data as well as an int type key. It should also contain methods capable of encrypting, decrypting or mixing text. It would also contain appropriate mutator and accessor methods. The constructor should have three flavours. No arguments, just text and text with a key.

    In the main routine a user would be required to enter a line of text, have it encrypted and printed, decrypted and printed, and finally switched and printed. The key is to be hardcoded (the user will not enter the key value because the key will be hardcoded into the program by the programmer).

    An example of how this should work is below:

    The key is 4 and the user enters "It is very warm today" as their line of text. This would be encrypted to This would be encrypted to “Mx mw zivc aevq xshec". Theefore in this example, for the new text every letter is encrypted as four characters further along in the alphabet. A to E, B to F, C to G, W to A (wrapping around to the beginning).

    Thus, the plan is to use the key as a circular encryption key. The letter 'a' becomes 'a' + key, 'b' becomes 'b' + key...'w' becomes 'w' + key which would wrap around to the start of the alphabet if the key is 4, 'w' would become 'a' which is four alpha characters after 'w'.

    The user should be able to type in a line of text and then have displayed on the screen the encrypted text by the method of the example above, the screen should also display the decrypted text which is returning it back to it's original text as entered by the user, and lastly the switched text should be displayed.

    The switched text part would be rotating the words to the right by # of characters in key value like this:

    For example, if the key value is 4 with 'is' being key value 1, 'a' being key value 2, 'very' being key value 3 and 'warm' being key value 4:
    "It is a very warm day" would become "warm day It is a"

    Note that the key value count begins with the word to the right of the first word which is why the word 'warm' would be key value of 4 and not the word 'very'.

    I just am looking for some sort of explanation on how I can go about doing this in C++ because attempting to write this out in code has been very tedious so far. Any assistance of any kind about how to do this would be so greatly appreciated.

    This program's description is a little hard to understand so if anyone reads this and needs anything to be cleared up, I will reply back and describe it.


  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    You obviously have a handle on how the algorithm is meant to work, yet you come here without any prepared code and expect help. At the very least have a go. You will learn nothing by having one of us give you a design to flesh out or even the answer. Try, make mistakes, learn from them. If you have more specific problems then by all means come back with some code samples and we'll help you fix it up. Start through by working it all out on paper. What classes will I need? What operations will these classes support? How will they interact with each other? Start answerring questions like that and a basic design will fall into place.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3
    Registered User
    Join Date
    Nov 2004


    I'll try to put something together although I am still not at all certain of how to go about doing all the things with the letters.

    I know I'll need to make a class that will contain the three methods: encrypt, decrypt and switch which are the three things I am doing with the line of text.

    I know how classes work in that they have methods and the classes have instances of it. So that part is fine for me it is just the logic surrounding the letter manipulation in the encrypting and the word switching in the switch part.

    I'll be working on this slowly throughout the next several days so I'll be keeping in touch with this board.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    The switching part can be done in a couple ways, personally I would just call the STL rotate function after finding the appropriate location about which you would like to perform the rotation.

    For the encrypting/decrypting part what I would do is create an encrypt and a decrypt function that take a single character and return the encrypted/decrypted character. Then I would simply use these functions as arguments to the STL transform function to either encrypt or decrypt the text.

    Couldn't be simpler... that's just me.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM