title: AES/CFB8 with OpenSSL
body: <p>I recently started a small side-project to recreate the Minecraft Server in <strong>C++</strong> but I stumbled on a problem.
  I need to use the <strong>AES/CFB8</strong> Cipher according to <a href="https://wiki.vg/Protocol_Encryption#Symmetric_Encryption" rel="nofollow noreferrer">this link</a> with continuous updating (not finished and restarted every packet).</p>
  <p>I surfed trough the internet to try to find a proper solution but could not figure out one that was actually working or it was using the command line interface.
  Any help is welcomed !</p>
  <p>Cipher.h</p>
  <pre class="lang-cpp prettyprint-override"><code>class Cipher {
  public:
      enum CipherType {
          ENCRYPT,
          DECRYPT
      };
  
  private:
      EVP_CIPHER_CTX* ctx;
      CipherType type;
      bool hasFinished;
  
  public:
      Cipher(const char* key, const char* iv, CipherType type);
      ~Cipher();
  
      int update(char* in, int inl, char* out);
      int final(char* out);
  
      int getMinimumBufLength(int size);
  };
  </code></pre>
  <p>Cipher.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>Cipher::Cipher(const char* key, const char* iv, CipherType type) : type(type), hasFinished(false)
  {
      ctx = EVP_CIPHER_CTX_new();
      EVP_CIPHER_CTX_init(ctx);
  
      if(type == CipherType::ENCRYPT)
          EVP_EncryptInit_ex(ctx, EVP_aes_256_cfb8(), nullptr, (unsigned char*)key, (unsigned char*)iv);
      else
          EVP_DecryptInit_ex(ctx, EVP_aes_256_cfb8(), nullptr, (unsigned char*)key, (unsigned char*)iv);
      
  }
  
  Cipher::~Cipher()
  {
      EVP_CIPHER_CTX_free(ctx);
  }
  
  int Cipher::getMinimumBufLength(int size)
  {
      return type == CipherType::DECRYPT ? size : size + AES_BLOCK_SIZE * 2;
  }
  
  int Cipher::update(char* in, int inl, char* out)
  {
      int len;
      EVP_CipherUpdate(ctx, (unsigned char*)out, &amp;len, (unsigned char*)in, inl);
  
      return len;
  }
  
  int Cipher::final(char* out)
  {
      int len;
      EVP_EncryptFinal_ex(ctx, (unsigned char*)out, &amp;len);
  
      return len;
  }
  </code></pre>
  <p>Cipher-test.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>TEST_CASE(&quot;Cipher AES/CFB8&quot;) 
  {
      char* key = &quot;AAAAAAAAAZEFRGUINJFKDFIVJ&quot;;
      char* iv = &quot;AUJVFEUB&quot;;
      Cipher encryptionCipher(key, iv, Cipher::CipherType::ENCRYPT);
      Cipher decryptionCipher(key, iv, Cipher::CipherType::DECRYPT);
  
      std::string data = &quot;Hello World ! This is some soon to be encrypted string !&quot;;
      char encData[encryptionCipher.getMinimumBufLength(data.size() + 1)];
      int outLen = encryptionCipher.update(data.data(), data.size() + 1, encData);
      int fLen = encryptionCipher.final(encData + outLen);
      
      CHECK(data != std::string(encData));
  
      char decData[decryptionCipher.getMinimumBufLength(outLen + fLen)];
      outLen = decryptionCipher.update(encData, outLen + fLen, decData);
      decryptionCipher.final(decData + outLen);
  
      CHECK(data == std::string(decData));
  }
  </code></pre>
  <p>Thanks in advance</p>
  <p>EDIT: I am just wondering if there was a need for supplementary code because of the &quot;continuous&quot; aspect of the Cipher.</p>