1

I wrote a bunch of crypto algorithms as classes and now I want to implement encryption modes (generalized modes shown in wikipedia, not the specific ones in the algorithms' specifications). How would I write a function that can accept any of the classes?

edit:

here's what i want to accomplish

class mode{
  private:
    algorithm_class

  public:
    mode(Algorithm_class, key, mode){
       algorithm_class = Algorithm_class(key, mode);

    }

};
calccrypto
  • 8,583
  • 21
  • 68
  • 99
  • 2
    I'm tempted to just guess and say "use templates", but it really depends on what you intend to do with the class. – trutheality Jun 05 '11 at 04:09
  • i tried templates, but its not working. ill edit my post to show what i want to do – calccrypto Jun 05 '11 at 04:10
  • 3
    I think the best solution is to create a base abstract algorithm class and make all your implementations inherit from it. – trutheality Jun 05 '11 at 04:15
  • What is the type of `algorithm_class` (is it `Algorithm_class` or anything) ? – iammilind Jun 05 '11 at 04:17
  • the one with the 'a' is simply the internal copy of the inputted algorithm – calccrypto Jun 05 '11 at 04:57
  • 1
    If you really want to pass in a "class as parameter", you need to use templates. But as others have pointed out, the standard OO way to do this is just to use inheritance. Create an abstract base class; create specific algos as derived classes; let the caller create an instance of a particular crypto algorithm and pass it in to the `mode` constructor. – Nemo Jun 05 '11 at 05:01
  • Also, NEVER, repeat NEVER use crypto code you wrote in a production system. Always use code from a reputable source, from experts who have some idea what sorts of problems and weaknesses that come up. – David Thornley Jun 06 '11 at 21:29

2 Answers2

3

You can use abstract classes:

class CryptoAlgorithm
{
   public:
      // whatever functions all the algorithms do
      virtual vector<char> Encrypt(vector<char>)=0;
      virtual vector<char> Decrypt(vector<char>)=0;
      virtual void SetKey(vector<char>)=0;
      // etc
}

// user algorithm
class DES : public CryptoAlgorithm
{
    // implements the Encrypt, Decrypt, SetKey etc
}
// your function
class mode{
public:
    mode(CryptoAlgorithm *algo) // << gets a pointer to an instance of a algo-specific class
           //derived from the abstract interface
         : _algo(algo) {}; // <<- make a "Set" method  to be able to check for null or
                       // throw exceptions, etc
private:
    CryptoAlgorithm *_algo;
}

// in your code
...
_algo->Encrypt(data);
...
//

In this way when you call _algo->Encrypt - you don't know and don't care about which specific algorithm you're using, just that it implements the interface all the crypto algorithms should be implementing.

littleadv
  • 20,100
  • 2
  • 36
  • 50
2

Well, how about

template<class AlgorithmType>
class mode{
  private:
    AlgorithmType _algo;

  public:
    mode(const AlgorithmType& algo)
      : _algo(algo) {}
};

?

No need for mode and key parameters, as the algorithm can be created by the user:

mode<YourAlgorithm> m(YourAlgorithm(some_key,some_mode));
Xeo
  • 129,499
  • 52
  • 291
  • 397