C++ OpenSSL 3.0.8 AES加解密

  2023年后,openssl进入3.0版本,openssl的加解密代码也出现了一些变化,例如编译时会有如下错误:

error C4996: ‘AES_set_encrypt_key’: Since OpenSSL 3.0

  如果使用OpenSSL 1.1.1 sdk编译则没有上述错误,使用3.0以上的openssl sdk就会报错,那是因为3.0的不兼容1.0的sdk。如果你想继续使用已弃用的函数,并且不想更改代码,可以考虑禁用特定的编译警告。在 Visual Studio 中,你可以使用 #pragma warning(disable: 4996) 来禁用这个特定的警告。请注意,这并不是一个推荐的解决方案,因为它可能会掩盖潜在的问题。最佳的解决方案通常是更新你的代码,以使用新的API。这样可以确保你的代码与最新的库版本兼容,并且可以从新版本中获得的安全和性能改进中受益。

  下面是C++ OpenSSL 3.0 AES加解密的代码:

#pragma once

#include <iostream>
#include <vector>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <string>
#include <codecvt>
#include <sstream>
#include <iomanip>

class CEncrypt3 {
private:
    CEncrypt3() {
        const unsigned char fixed_key[32] = { 0x71, 0x02, 0x03, 0x04, 0x05, 0x06, 0x17, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x4A, 0x0F, 0x10,
                                              0x11, 0x12, 0x53, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0xFC, 0x10, 0x1E, 0x1F, 0x20 };
        const unsigned char fixed_iv[16] = { 0x21, 0x02, 0x03, 0xA4, 0x05, 0x06, 0x23, 0x08, 0x09, 0x0A, 0x1B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };

        std::copy(fixed_key, fixed_key + sizeof(key), key);
        std::copy(fixed_iv, fixed_iv + sizeof(iv), iv);
    }

public:
    static CEncrypt3& Instance() {
        static CEncrypt3 instance;
        return instance;
    }

    CEncrypt3(const CEncrypt3&) = delete;
    CEncrypt3& operator=(const CEncrypt3&) = delete;

    std::wstring Encrypt(const std::wstring& plaintext)
    {
        if (plaintext.empty())
            return L"";

        std::vector<unsigned char> ciphertext;
        EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
        if (!ctx)
        {
            throw std::runtime_error("Failed to create cipher context");
        }

        if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1)
        {
            EVP_CIPHER_CTX_free(ctx);
            throw std::runtime_error("Failed to initialize encryption");
        }

        std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
        std::string utf8_text = converter.to_bytes(plaintext);

        int len;
        ciphertext.resize(utf8_text.size() + EVP_CIPHER_block_size(EVP_aes_256_cbc()));
        if (EVP_EncryptUpdate(ctx, ciphertext.data(), &len, reinterpret_cast<const unsigned char*>(utf8_text.data()), utf8_text.size()) != 1)
        {
            EVP_CIPHER_CTX_free(ctx);
            throw std::runtime_error("Failed to encrypt data");
        }

        int padding_len;
        if (EVP_EncryptFinal_ex(ctx, ciphertext.data() + len, &padding_len) != 1)
        {
            EVP_CIPHER_CTX_free(ctx);
            throw std::runtime_error("Failed to finalize encryption");
        }

        ciphertext.resize(len + padding_len);
        EVP_CIPHER_CTX_free(ctx);

        std::wstringstream wss;
        for (auto c : ciphertext) {
            wss << std::hex << std::setw(2) << std::setfill(L'0') << (int)c;
        }

        return wss.str();
    }

    std::wstring Decrypt(const std::wstring& ciphertext)
    {
        if (ciphertext.empty())
            return L"";

        std::vector<unsigned char> ciphertext_bytes;
        for (size_t i = 0; i < ciphertext.size(); i += 2) {
            std::wstring byte_string = ciphertext.substr(i, 2);
            unsigned char byte = static_cast<unsigned char>(std::stoi(byte_string, nullptr, 16));
            ciphertext_bytes.push_back(byte);
        }

        std::vector<unsigned char> plaintext;
        EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
        if (!ctx)
        {
            throw std::runtime_error("Failed to create cipher context");
        }

        if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1)
        {
            EVP_CIPHER_CTX_free(ctx);
            throw std::runtime_error("Failed to initialize decryption");
        }

        int len;
        plaintext.resize(ciphertext_bytes.size());
        if (EVP_DecryptUpdate(ctx, plaintext.data(), &len, ciphertext_bytes.data(), ciphertext_bytes.size()) != 1)
        {
            EVP_CIPHER_CTX_free(ctx);
            throw std::runtime_error("Failed to decrypt data");
        }

        int padding_len;
        if (EVP_DecryptFinal_ex(ctx, plaintext.data() + len, &padding_len) != 1)
        {
            EVP_CIPHER_CTX_free(ctx);
            throw std::runtime_error("Failed to finalize decryption");
        }

        plaintext.resize(len + padding_len);
        EVP_CIPHER_CTX_free(ctx);

        std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
        return converter.from_bytes(reinterpret_cast<const char*>(plaintext.data()), reinterpret_cast<const char*>(plaintext.data() + plaintext.size()));
    }

private:
    unsigned char key[32];
    unsigned char iv[16];
};



int main()
{
    CEncrypt3& instance = CEncrypt3::Instance();

    std::wstring plaintext = L"中文加密解密容易出现乱码";

    std::wstring ciphertext = instance.Encrypt(plaintext);
    std::wstring decrypted_text = instance.Decrypt(ciphertext);

    std::wcout << L"Original text: " << plaintext << std::endl;
    std::wcout << L"Decrypted text: " << decrypted_text << std::endl;

    return 0;
}

CEncrypt3用了单例模式,在其它地方调用时,使用CEncrypt3::Instance().Encrypt(plaintext)即可。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
0、此例程调试环境 运行uname -a的结果如下: Linux freescale 3.0.35-2666-gbdde708-g6f31253 #1 SMP PREEMPT Thu Nov 30 15:45:33 CST 2017 armv7l GNU/Linux 简称2017 armv7l GNU/Linux 1、openssl 直接处理AES的API 在openssl/aes.h定义。是基本的AES库函数接口,可以直接调用,但是那个接口是没有填充的。而如果要与Java通信,必须要有填充模式。所以看第2条。 2、利用openssl EVP接口 在openssl/evp.h中定义。在这个接口中提供的AES是默认是pkcs5padding方式填充方案。 3、注意openssl新老版本的区别 看如下这段 One of the primary differences between master (OpenSSL 1.1.0) and the 1.0.2 version is that many types have been made opaque, i.e. applications are no longer allowed to look inside the internals of the structures. The biggest impact on applications is that: 1)You cannot instantiate these structures directly on the stack. So instead of: EVP_CIPHER_CTX ctx; you must instead do: EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); .... EVP_CIPHER_CTX_free(ctx); 2)You must only use the provided accessor functions to access the internals of the structure. 4、注意加密的内容是数据不限制是否为字符串 openssl接口加密的是数据,不限制是否为字符串,我看到有些人在加密时使用strlen(),来获取要加密的长度,如果是对字符串加密的话没有问题,如果不是字符串的话,用它获取的长度是到第一个0处,因为这个函数获取的是字符串长度,字符串是以零为终止的。 5、在调用EVP_EncryptFinal_ex时不要画蛇添足 正常加解密处理过程,引用网友的代码如下,经测试正确。 int kk_encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext) { EVP_CIPHER_CTX *ctx; int len; int ciphertext_len; ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv); //EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv); EVP_EncryptUpdate(ctx, ciphertext, &len;, plaintext, plaintext_len); ciphertext_len = len; EVP_EncryptFinal_ex(ctx, ciphertext + len, &len;); ciphertext_len += len; EVP_CIPHER_CTX_free(ctx); return ciphertext_len; } int kk_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) { EVP_CIPHER_CTX *ctx; int len; int plaintext_len; ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, EVP_aes_128_cbc(), NULL, key, iv); //EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, key, iv); EVP_DecryptUpdate(ctx, plaintext, &len;, ciphertext, ciphertext_len); plaintext_len = len; EVP_DecryptFinal_ex(ctx, plaintext + len, &len;); plaintext_len += len; EVP_CIPHER_CTX_free(ctx); return plaintext_len; } 我看到有人提供的代码在加密长度正好是16字节的整数倍时特意不去调用EVP_EncryptFinal_ex,这实在是画蛇添足啊,不论什么情况下,最后一定要调用EVP_EncryptFinal_ex一次,然后结束加密过程。 6、Base64陷阱 如果用到了base64,要注意如下: 1)base64算法是将3个字节变成4个可显示字符。所以在如果数据长度不是3字节对齐时,会补0凑齐。 2)在解密时先要解base64,再解AES。在解base64后,要减掉补上的0。算法就去查看base64后的字符串尾处有几个=号,最多是2个,如果正好要加密的数据是3的倍数,不需要补0,那么base64后的数据尾处就没有=,如果补了1个0,就有一个=号。 算法如下: int encode_str_size = EVP_EncodeBlock(base64, en, el); int length = EVP_DecodeBlock(base64_out, base64, encode_str_size ); //EVP_DecodeBlock内部同样调用EVP_DecodeInit + EVP_DecodeUpdate + Evp_DecodeFinal实现,但是并未处理尾部的'='字符,因此结果字符串长度总是为3的倍数 while(base64[--encode_str_size] == '=') length--; 算法网友提供,测试正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

令狐掌门

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值