使用C++进行加密和解密

 神话海青_769 发布于 2023-01-30 14:41

我有一个缓冲区,我正在添加一些纯文本.我想使用openssl AES加密来加密文本,然后将其解密,然后将其打印回屏幕.

代码正在运行,没有错误.

#include 
#include 
#include 
#include 
#include 
using namespace std;

void main()
{

// Buffers
unsigned char inbuffer[1024];
unsigned char encryptedbuffer[1024];
unsigned char outbuffer[1024];


// CODE FOR ENCRYPTION
//--------------------
unsigned char oneKey[] = "abc";
AES_KEY key; 

AES_set_encrypt_key(oneKey,128,&key);
AES_set_decrypt_key(oneKey,128,&key);

//--------------------


string straa("hello world\n");
memcpy((char*)inbuffer,straa.c_str(),13);


printf("%s",inbuffer);
//this prints out fine

AES_encrypt(inbuffer,encryptedbuffer,&key);
//printf("%s",encryptedbuffer);
//this is expected to pring out rubbish, so is commented

AES_decrypt(encryptedbuffer,outbuffer,&key);
printf("%s",outbuffer);
//this is not pringint "hello world"

getchar();

}

我知道一旦放入新缓冲区"encryptedbuffer"和"outbuffer",它们不会以空终止"\ 0",但即便如此,通过打印出原始数据,我只是在垃圾后解密,在解密结束时,我假设\ 0也应该被解密,因此printf应该打印corectly.

任何人都知道如何使decyption工作?

还有任何想法如何使用C++库打印缓冲区,也许是cout,而不是printf?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有