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
| using algorithmFunc = const EVP_CIPHER* (*)();
enum class Algorithm { AES_ECB_128, AES_CBC_128, AES_CFB_128, AES_OFB_128, AES_CTR_128, AES_ECB_192, AES_CBC_192, AES_CFB_192, AES_OFB_192, AES_CTR_192, AES_ECB_256, AES_CBC_256, AES_CFB_256, AES_OFB_256, AES_CTR_256 };
enum class CryptoType { DECRYPTO, ENCRYPTO };
const QMap<Algorithm, algorithmFunc> m_algorithms = { {Algorithm::AES_ECB_128, EVP_aes_128_ecb}, {Algorithm::AES_CBC_128, EVP_aes_128_cbc}, {Algorithm::AES_CFB_128, EVP_aes_128_cfb128}, {Algorithm::AES_OFB_128, EVP_aes_128_ofb}, {Algorithm::AES_CTR_128, EVP_aes_128_ctr}, {Algorithm::AES_ECB_192, EVP_aes_192_ecb}, {Algorithm::AES_CBC_192, EVP_aes_192_cbc}, {Algorithm::AES_CFB_192, EVP_aes_192_cfb128}, {Algorithm::AES_OFB_192, EVP_aes_192_ofb}, {Algorithm::AES_CTR_192, EVP_aes_192_ctr}, {Algorithm::AES_ECB_256, EVP_aes_256_ecb}, {Algorithm::AES_CBC_256, EVP_aes_256_cbc}, {Algorithm::AES_CFB_256, EVP_aes_256_cfb128}, {Algorithm::AES_OFB_256, EVP_aes_256_ofb}, {Algorithm::AES_CTR_256, EVP_aes_256_ctr} };
void AESCrypto::generateIvec(unsigned char* ivec) { QCryptographicHash hash(QCryptographicHash::Md5); hash.addData(m_key); std::string res = hash.result().toStdString(); for (int i = 0; i < AES_BLOCK_SIZE; ++i) { ivec[i] = res.at(i); } }
unsigned char ivec[AES_BLOCK_SIZE]; generateIvec(ivec);
int ret = EVP_CipherInit_ex(ctx, m_algorithms.value(m_algorithmType)(), nullptr, reinterpret_cast<unsigned char*>(m_key.data()), ivec, cryptoType == CryptoType::ENCRYPTO ? 1 : 0);
|