我有一个缓冲区,我正在添加一些纯文本.我想使用openssl AES加密来加密文本,然后将其解密,然后将其打印回屏幕.
代码正在运行,没有错误.
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string>
#include <openssl/aes.h>
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();
}
Run Code Online (Sandbox Code Playgroud)
我知道一旦放入新缓冲区"encryptedbuffer"和"outbuffer",它们不会以空终止"\ 0",但即便如此,通过打印出原始数据,我只是在垃圾后解密,在解密结束时,我假设\ 0也应该被解密,因此printf应该打印corectly.
任何人都知道如何使decyption工作? …