欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于Openssl的aes_128_ecb的pkcs5padding加密解密文本

程序员文章站 2022-03-05 12:09:29
...

  网上有一堆关于openssl的aes的加密方式,搞得都痛死了,pkcs5padding的补码方式的代码确少之又少,终于在我不懈努力下搞定这玩意了,代码是C++的

CAES.h文件

#pragma once
#include <openssl/aes.h>
#include <string>
class CAES
{
public:

	static std::string aes_128_ecb_encrypt(const std::string& source, const std::string& key);
	static std::string aes_128_ecb_decrypt(const std::string& ciphertext, const std::string& key);
};

CAES.cpp文件

#include "stdafx.h"
#include "AES.h"
#include <openssl/evp.h>
#include <openssl/ssl.h>
#include "Base64.h"
#include <cassert>

struct evp_cipher_ctx_st {
	const EVP_CIPHER *cipher;
	ENGINE *engine;             /* functional reference if 'cipher' is
								* ENGINE-provided */
	int encrypt;                /* encrypt or decrypt */
	int buf_len;                /* number we have left */
	unsigned char oiv[EVP_MAX_IV_LENGTH]; /* original iv */
	unsigned char iv[EVP_MAX_IV_LENGTH]; /* working iv */
	unsigned char buf[EVP_MAX_BLOCK_LENGTH]; /* saved partial block */
	int num;                    /* used by cfb/ofb/ctr mode */
	void *app_data;             /* application stuff */
	int key_len;                /* May change for variable length cipher */
	unsigned long flags;        /* Various flags */
	void *cipher_data;          /* per EVP data */
	int final_used;
	int block_mask;
	unsigned char final[EVP_MAX_BLOCK_LENGTH]; /* possible final block */
}; /* EVP_CIPHER_CTX */

/*
 * openssl aes 128 ecb pkcs5padding 加密
 */
std::string CAES::aes_128_ecb_encrypt(const std::string& source, const std::string& key)
{
	EVP_CIPHER_CTX *ctx;
	ctx = EVP_CIPHER_CTX_new();
	int ret = EVP_EncryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (const unsigned char*)key.data(), NULL);
	assert(ret == 1);
	unsigned char* result = new unsigned char[source.length() + 64]; // 弄个足够大的空间
	int len1 = 0;
	ret = EVP_EncryptUpdate(ctx, result, &len1, (const unsigned char*)source.data(), source.length());
	assert(ret == 1);
	int len2 = 0;
	ret = EVP_EncryptFinal_ex(ctx, result + len1, &len2);
	assert(ret == 1);
	ret = EVP_CIPHER_CTX_cleanup(ctx);
	assert(ret == 1);
	EVP_CIPHER_CTX_free(ctx);
	std::string res((char*)result, len1 + len2);
	delete[] result;
	return res;
}
/*
 * openssl aes 128 ecb pkcs5padding 解密
 */
std::string CAES::aes_128_ecb_decrypt(const std::string& ciphertext, const std::string& key)
{
	EVP_CIPHER_CTX *ctx;
	ctx = EVP_CIPHER_CTX_new();
	int ret = EVP_DecryptInit_ex(ctx, EVP_aes_128_ecb(), NULL, (const unsigned char*)key.data(), NULL);
	assert(ret == 1);
	unsigned char* result = new unsigned char[ciphertext.length() + 64]; // 弄个足够大的空间
	int len1 = 0;
	ret = EVP_DecryptUpdate(ctx, result, &len1, (const unsigned char*)ciphertext.data(), ciphertext.length());
	assert(ret == 1);
	int len2 = 0;
	ret = EVP_DecryptFinal_ex(ctx, result + len1, &len2);
	assert(ret == 1);
	ret = EVP_CIPHER_CTX_cleanup(ctx);
	assert(ret == 1);
	EVP_CIPHER_CTX_free(ctx);
	std::string res((char*)result, len1 + len2);
	delete[] result;
	return res;
}


最后就是main函数了


  #include "stdafx.h"
  #include <string>
  #include <iostream>
  #include "AES.h"
  #include <openssl/aes.h>  
 
int main()
{
	std::string aes_key("helloaeshelloaes", 16);
	std::string soure = "Hello World!!";
	std::string encrypt = CAES::aes_128_ecb_encrypt(soure, aes_key);
	//std::cout << "AES加密:" + encrypt << std::endl;
	std::string decrypt = CAES::aes_128_ecb_decrypt(encrypt, aes_key);
	std::cout << "AES解密:" + decrypt << std::endl;
	system("pause");
	return 0;
}

注释的这一行控制台没办法base46的字符串出来,需要将base64转成可识别的字符串类型,就能正常的输出了,后面我会将Base64转string的代码放出来

相关标签: Openssl