질문

2010-06-23 3 views
2

을 감안할 때이 개 클래스를 사용하려면 :질문

첫 번째 클래스는 AES 암호화/복호화 및 암호화 반환/특정 키 체인을 주어 해독 된 데이터를 수행합니다.

두 번째 클래스는 암호화 할 데이터를 수집 한 다음이를 암호화/암호 해독 클래스에 전달합니다.

데이터를 수집하는 클래스에서 직접 암호화 클래스를 호출하거나 적절한 프로세스를 추상화하는 두 클래스 사이에 객체가 있어야합니까? 프로그램 수명 기간 동안 이러한 유형의 요청을 모두 처리 할 수 ​​있도록 하나의 추상 클래스 인스턴스와 하나의 암호화 인스턴스가 있어야합니까?

+1

이 데이터의 사용중인 방법을 아는 또는 무엇 당신의 디자인의 나머지의 일부처럼 보인다없이 대답하기 어렵다 ... 녹슨입니다. 예를 들어 누군가 데이터를 사용하려고 시도 할 때 데이터 소스를 래핑하고 AES 코덱으로 호출하는 한 쌍의 데코레이터 (암호화 용, 해독 용)를 갖는 것이 적절할 수 있습니다. 또는 AES 코덱을 직접 사용하는 구체적인 데이터 제공 업체 (즉 가능한 가장 간단한 솔루션)를 보유하는 것이 적절할 수 있습니다 (현재로서는). 자세한 내용이 없어도 좋은 아이디어를 나쁘게 말할 수는 없습니다. – pkh

답변

3

개인적으로 필자는 암호화 알고리즘을 나타내는 일종의 추상 인터페이스를 만들었습니다. 팩토리 기능이 키를 가져와 키가 설치된 암호화 알고리즘의 구체적인 인스턴스를 생성했습니다. 그래서 여기서 '2 류'란 '1 류'를 직접 부르 겠지만, 그 반을 실화시키는 '3 류'가있을 것입니다. 뭔가 같이 :

/* Core encryption framework definitions */ 
class CipherInstance { 
    // ... 
    public: 
    virtual void encrypt(void *, size_t) = 0; 
    virtual void decrypt(void *, size_t) = 0; 
    virtual size_t blocksize() const = 0; 
// ... 
    virtual ~CipherInstance() { } 
}; 

class Cipher { 
    public: 
    virtual CipherInstance *instantiate(const void *key, size_t keylen) = 0; 
    virtual size_t minkeysize() const = 0; 
    virtual size_t maxkeysize() const = 0; 
}; 

/* AES implementation */ 
class privateAESImpl : public Cipher { /* ... */ }; 
// This is the only public definition in the AES implementation. The privateAESImpl 
// class is a stateless singleton, and this is the only instance. Doing this instead 
// of static functions allows AES to be passed to a function taking a Cipher * 
extern privateAESImpl AES; 

// Much later: 
    CipherInstance *aes = AES.instantiate(key, keylen); 
    aes->encrypt(data, datalen); 
// or, to be more general: 
void frob(Cipher *cipher, void *key, size_t keylen, void *data, size_t datalen) { 
    CipherInstance *inst = cipher->instantiate(key, keylen); 
    inst->encrypt(data, datalen); 
} 

C#의 System.Security.Cryptography 라이브러리는 비슷한 방법을 사용 - 예를 들어, System.Security.Cryptography.SymmetricAlgorithm를 참조하십시오. 그러나 C#은 인트로 스펙 션을 지원하므로 팩토리 클래스는 필요 없으며 간단히 a static method taking a name.입니다. C++에서는 전체 팩토리 클래스가 필요합니다.

+0

질문이있는 경우이를 구현하고 다시 시도 할 것입니다 (예 : 작동하지 않을 수 있음). –

1

나는 암호화 객체를 직접 호출하지만 인터페이스를 통해 호출합니다. 호출 할 실제 암호화 객체는 런타임 중에 프로그램을 제어하는 ​​클래스에 의해 제공됩니다. 당신은 실례해야합니다

, 내 C++는

// Interface for encryption. 
class Encryptor 
{ 
    public: 
     virtual byte* encrypt(byte* data) = 0; 
} 

// Gathering class 
class Gatherer 
{ 
    private: 
     Encryptor* encryptor; 
    public: 
     Gatherer(Encryptor* encryptor) 
     { 
      this->encryptor = encryptor; 
     } 

     void doStuff() 
     { 
      // Do stuff 

      // Call encryption on gathered data 
      byte* encryptedData = encryptor->encrypt(data); 

      // Do more stuff with encrypted data. 
     } 
} 

main() 
{ 
    Encryptor* encryptor = new AESEncryptor(ENCRYPTION_KEY); 
    Gatherer gatherer = new Gatherer(encryptor); 
    gatherer->doStuff(); 
} 
}