1

私は一連の暗号化アルゴリズムをクラスとして作成しましたが、暗号化モードを実装したいと考えています (アルゴリズムの仕様の特定のものではなく、ウィキペディアに示されている一般化されたモード)。クラスのいずれかを受け入れることができる関数をどのように作成しますか?

編集:

これが私が達成したいことです

class mode{
  private:
    algorithm_class

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

    }

};
4

2 に答える 2

3

抽象クラスを使用できます。

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);
...
//

このように電話をかける_algo->Encryptと、使用している特定のアルゴリズムがわからず、気にせず、すべての暗号化アルゴリズムが実装する必要のあるインターフェイスが実装されているだけです。

于 2011-06-05T04:23:58.367 に答える
2

さて、どうですか

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

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

アルゴリズムはユーザーが作成できるためmode、パラメーターは必要ありません。key

mode<YourAlgorithm> m(YourAlgorithm(some_key,some_mode));
于 2011-06-05T04:18:00.180 に答える