C/C++ 使用 openssl 进行 AES/ECB/PKCS5Padding 加密解密
2022/8/8 1:23:14
本文主要是介绍C/C++ 使用 openssl 进行 AES/ECB/PKCS5Padding 加密解密,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在 java 上进行 AES128/ECB/PKCS5Padding 加密解密是很简单的
public static String aesDecrypt(String str,String key) throws Exception{ Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE,new SecretKeySpec(Base64.getDecoder().decode(key),"AES")); byte[] bytes = Base64.getDecoder().decode(str.getBytes(StandardCharsets.UTF_8)); byte[] ret = cipher.doFinal(bytes); return new String(ret,"UTF-8"); } public static String aesEncrypt(String str,String key) throws Exception { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(Base64.getDecoder().decode(key),"AES")); return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes())); }
AES 加解密使用的key本身是 16 字节(128位),但是key可能不是ascii里的可见字符,有时候我们把 key 进行 base64 编码之后,当作明文传输使用。真正把 key 用来加密解密的时候,需要先 base64 解码来得到原本的 key。
同样的,经过AES加密的信息也有可能不是每一个字节都是ascii可见字符,我们同样将加密后的密文进行base64编码之后再展示。
同样的操作,用 C/C++ 进行就复杂得多,C/C++ 自己本身没有对应的库,不过好在有很多开源的实现,这里用 openssl 作为演示
#include <openssl/evp.h> #include <vector> std::string AES128ECBEncrypt(const std::string& base64key, const std::string& str) { std::vector<unsigned char> key; if (!Base64Decode(base64key, key)) return ""; EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); int encLen = 0; int outlen = 0; size_t outsize = ((str.size() / 16)+1) * 16; std::vector<unsigned char> encData(outsize); EVP_CipherInit_ex(ctx, EVP_aes_128_ecb(), NULL, key.data(), NULL, 1); EVP_CipherUpdate(ctx, encData.data(), &outlen, (const unsigned char*)str.c_str(), (int)str.size()); encLen = outlen; EVP_CipherFinal_ex(ctx, encData.data() + outlen, &outlen); encLen += outlen; EVP_CIPHER_CTX_free(ctx); return std::move(Base64Encode(encData.data(), encLen)); } std::string AES128ECBDecrypt(const std::string& base64Key, const std::string& base64str) { std::vector<unsigned char> buffer; if (!Base64Decode(base64str, buffer)) { return ""; } std::vector<unsigned char> key; if (!Base64Decode(base64Key, key)) { return ""; } EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new(); int encLen = 0; int outlen = 0; BUFFER encData(buffer.size()); EVP_CipherInit_ex(ctx, EVP_aes_128_ecb(), NULL, key.data(), NULL, 0); EVP_CipherUpdate(ctx, encData.data(), &outlen, buffer.data(), (int)buffer.size()); encLen = outlen; EVP_CipherFinal(ctx, encData.data() + outlen, &outlen); encLen += outlen; EVP_CIPHER_CTX_free(ctx); return std::move(std::string((char*)encData.data(), encLen)); }
这篇关于C/C++ 使用 openssl 进行 AES/ECB/PKCS5Padding 加密解密的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升