1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
   |  
#include <iostream>
#include <iomanip>
#include <exception>
#include <stdexcept>
 
using namespace std;
 
#include "openssl/evp.h"
#include "openssl/rsa.h"
#include "openssl/err.h"
 
 
void buffer_dump(const unsigned char* buffer, size_t size)
{
  std::cout << hex;
  for (unsigned int i=0; i<size; i++)
    std::cout << (int) buffer[i] << " ";
  std::cout << dec << endl;
}
 
size_t EVP_ENCRYPT(unsigned char* bufferOut, int sizeOutput, const unsigned char* bufferIn, int sizeInput)
{
  EVP_CIPHER_CTX m_ctx;
  const char* key = "TESTKEY.........";
  unsigned char* buffer = bufferOut;
  size_t bufsize = 0;
 
  EVP_CIPHER_CTX_init(&m_ctx);
  EVP_EncryptInit_ex(&m_ctx, EVP_aes_256_cbc(), NULL, NULL, NULL);
  EVP_CIPHER_CTX_set_key_length(&m_ctx, ::strlen(key));
  if (EVP_EncryptInit_ex(&m_ctx, NULL, NULL, (const unsigned char*) key, NULL) != 1)
    std::cerr << "EVP_EncryptInit failed" << endl;
  if (EVP_EncryptUpdate(&m_ctx, bufferOut, &sizeOutput, bufferIn, sizeInput) != 1)
    std::cerr << "EVP_EncryptUpdate failed" << endl;
  bufferOut += sizeOutput;
  bufsize += sizeOutput;
  if (EVP_EncryptFinal_ex(&m_ctx, bufferOut, &sizeOutput) != 1)
    std::cerr << "EVP_EncryptFinal_ex failed" << endl;
  bufsize += sizeOutput;
  EVP_CIPHER_CTX_cleanup(&m_ctx);
  buffer_dump(bufferOut, bufsize);
  return bufsize;
}
 
size_t EVP_DECRYPT(unsigned char* bufferOut, int sizeOutput, const unsigned char* bufferIn, int sizeInput)
{
  EVP_CIPHER_CTX m_ctx;
  const char* key = "TESTKEY.........";
  unsigned char* buffer = bufferOut;
  size_t bufsize = 0;
 
  EVP_CIPHER_CTX_init(&m_ctx);
  EVP_DecryptInit_ex(&m_ctx, EVP_aes_256_cbc(), NULL, NULL, NULL);
  EVP_CIPHER_CTX_set_key_length(&m_ctx, ::strlen(key));
  if (EVP_DecryptInit_ex(&m_ctx, NULL, NULL, (const unsigned char*) key, NULL) != 1)
    std::cerr << "EVP_DecryptInit_ex failed" << endl;
  if (EVP_DecryptUpdate(&m_ctx, bufferOut, &sizeOutput, bufferIn, sizeInput) != 1)
    std::cerr << "EVP_DecryptUpdate failed" << endl;
  bufferOut += sizeOutput;
  bufsize += sizeOutput;
  if (EVP_DecryptFinal_ex(&m_ctx, bufferOut, &sizeOutput) != 1)
    std::cerr << "EVP_DecryptFinal_ex failed" << endl;
  bufsize += sizeOutput;
  EVP_CIPHER_CTX_cleanup(&m_ctx);
  buffer_dump(bufferOut, bufsize);
  return bufsize;
}
 
 
int main(int argc, char* argv[])
{
  const char* srctext = "TEST";
 
  unsigned char* bufferIn;
  unsigned char* bufferOut;
  int sizeBuffer = 0;
  int sizeOutput, sizeInput;
 
  char encbuffer[32];
  char decbuffer[32];
 
  bufferIn = (unsigned char*)(srctext);
  sizeInput = ::strlen(srctext);
  bufferOut = (unsigned char*)(encbuffer);
  sizeOutput = sizeof(encbuffer);
 
  sizeBuffer = EVP_ENCRYPT(bufferOut, sizeOutput, bufferIn, sizeInput);
  sizeBuffer = EVP_ENCRYPT(bufferOut, sizeOutput, bufferIn, sizeInput);
 
  bufferIn = (unsigned char*)(encbuffer);
  sizeInput = sizeBuffer;
  bufferOut = (unsigned char*)(decbuffer);
  sizeOutput = sizeof(decbuffer);
 
  sizeBuffer = EVP_DECRYPT(bufferOut, sizeOutput, bufferIn, sizeInput);
} | 
Partager