Thread: Classy question

  1. #1
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603

    Classy question

    A serious question this time.

    I'm writing a C++ implementation of a DES encryptor (yeah, I know, but it's not really a problem with the encryptor code as such). As a bit of background, the DES algorithm consists of 3 steps.

    1) Derive 16 subkeys from a user supplied key

    2) Encrypt data with those subkeys

    3) decrypt data with those subkeys

    From this, and wanting to keep it as OO as possible, I decided to create an encryptor class. The constructor for this class is the part that derives the subkeys. There is an encrypt method in this class, and a decrypt method in this class.

    First Q. Does this seem a reasonable way to structure my class? This way, for each change of key I can create a new encryptor object, ready to call the encrypt / decrypt methods.

    Next Q. Can I call functions from my constructor? If so how - where do I put the definitions etc.

    Last Q.

    Code:
    <snip>
    class clsInitKeys             {
    						char strKeyString[17];
    						char strKeyn[17][49];
    	
    	
              public:
    						char* GetKey(int x);
    						clsInitKeys(char y[17] = "0123456789ABCDEF");
    					};
    <snip>
    
    clsInitKeys::clsInitKeys(char y[17])
    {
    
    	strcpy (strKeyString, y);
    
    <snip>
    
    int main(void)
    {
    char key[17] = "0123456789abcdef";
    
    <snip>
    I really don't like passing that char array to the constructor. Is there a way I can pass a pointer instead??


    THX,

    Rob.
    Visit entropysink.com - It's what your PC is made for!

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Does this seem a reasonable way to structure my class?

    Yes.

    > Can I call functions from my constructor?

    What functions? It's generally advisable to have your constructor do as little work as possible. It should only really initialise variables.

    >I really don't like passing that char array to the constructor. Is there a way I can pass a pointer instead??

    Arrays are passed by address. You are passing a pointer to the first element.

  3. #3
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603
    What functions? It's generally advisable to have your constructor do as little work as possible. It should only really initialise variables.
    Mmmmmm that's my problem really, 'cos at the moment my constructor is doing loads of stuff by deriving the keys for me. Trouble is, I can't think of a better place to put that key derivation code. If I put it in a seperate member function, I'd have to remember to call that function before I could use my encryptor object. At the moment, I just create an encryptor object, and the subkeys are there ready to use.

    I think I may have asked the last Q badly. At the moment, I am passing the whole string to the constructor (I think). What I want to do is just pass it the address of where the string is located(??), and that way not have to use that (ugly) strcpy.
    Visit entropysink.com - It's what your PC is made for!

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Mmmmmm that's my problem really, 'cos at the moment my constructor is doing loads of stuff by deriving the keys for me. Trouble is, I can't think of a better place to put that key derivation code. If I put it in a seperate member function, I'd have to remember to call that function before I could use my encryptor object. At the moment, I just create an encryptor object, and the subkeys are there ready to use.
    I suppose there's an argument for it being in the constructor; to intialise the keys. If you want to break things down, you could split the classes. Have a encryptor object that contains a key object, and call the key methods from your constructor. You could also expose some of these key methods through your encryptor object, if it was necessary to modify the key after it had been created.

    What I want to do is just pass it the address of where the string is located(??), and that way not have to use that (ugly) strcpy.
    If you mean just store a pointer to the external string rather than copy the string, then this would be dangerous as the orginal string would have to be kept alive for the duration of the encryption object. If you're worried about uglyness, use std::strings which look much nicer.

  5. #5
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603
    call the key methods from your constructor
    Just want to get this straight in my head..... you mean call the key methods from my encryptor object constructor, right? And presumably I could also create my key object from within the encryptor constructor (if I understand correctly).

    BTW, I can't envisage wanting to change the keys once created - I think I'd prefer to destroy the "old" encryptor object and create a new one with the new key.

    Thanks for your help - very much appreciated.
    Visit entropysink.com - It's what your PC is made for!

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Just want to get this straight in my head..... you mean call the key methods from my encryptor object constructor, right? And presumably I could also create my key object from within the encryptor constructor (if I understand correctly).
    Yes, there's probably other ways, but that's one way of making sure that one function isn't doing too much work on its own. If your encryptor object has exclusive access to the key object, you may aswell drop the whole key declaration into the encryptors private section.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM