how to encrypt and decrypt string with openssl? - c++

Trying to encrypt and decrypt strings with evp functions of openssl.
I tried the following source code but I got unexpected results (garbege output).
What I am missing?
#include <stdio.h>
#include <unistd.h>
#if 1
#include <openssl/evp.h>
char *se_evp_encrypt(char *ssid, char *data, int inl, char *ret, int *rb)
{
int i, tmp, ol;
EVP_CIPHER_CTX evpctx;
char key[EVP_MAX_KEY_LENGTH] = {0};
char iv[EVP_MAX_IV_LENGTH] = {0};
*ret = '\0';
strncpy(key, ssid, EVP_MAX_KEY_LENGTH);
strncpy(iv, ssid, EVP_MAX_IV_LENGTH);
EVP_EncryptInit(&evpctx, EVP_bf_cbc(), key, iv);
EVP_EncryptUpdate(&evpctx, ret, &ol, data, inl);
*rb = ol;
EVP_EncryptFinal(&evpctx, ret, &ol);
return ret;
}
char *se_evp_decrypt(char *ssid, char *ct, int inl, char *pt)
{
int ol;
EVP_CIPHER_CTX evpctx;
char key[EVP_MAX_KEY_LENGTH] = {0};
char iv[EVP_MAX_IV_LENGTH] = {0};
char final[EVP_MAX_BLOCK_LENGTH];
*pt = '\0';
strncpy(key, ssid, EVP_MAX_KEY_LENGTH);
strncpy(iv, ssid, EVP_MAX_IV_LENGTH);
EVP_DecryptInit(&evpctx, EVP_bf_cbc(), key, iv);
EVP_DecryptUpdate(&evpctx, pt, &ol, ct, inl);
if (!ol) /* there's no block to decrypt */
{
return "";
}
pt[ol] = 0;
EVP_DecryptFinal(&evpctx, final, &inl);
return pt;
}
int main(int argc, char *argv[])
{
char str[] = "abcdef123456789";
char buf[256] = "", buf2[256] = "";
int i;
se_evp_encrypt("anyssid", str, strlen(str), buf, &i);
printf("Ciphertext is %d bytes. %d\n", i, strlen(str));
se_evp_decrypt("anyssid", buf, i, buf2);
printf("Decrypted: >>%s<<\n", buf2);
}
#endif

fixed the source code in this way
#include <stdio.h>
#include <unistd.h>
#if 1
#include <openssl/evp.h>
char *se_evp_encrypt(char *ssid, char *data, int inl, char *ret, int *rb)
{
int i, tmp, ol;
EVP_CIPHER_CTX evpctx = {0};
char key[EVP_MAX_KEY_LENGTH] = {0};
char iv[EVP_MAX_IV_LENGTH] = {0};
*ret = '\0';
strncpy(key, ssid, EVP_MAX_KEY_LENGTH);
strncpy(iv, ssid, EVP_MAX_IV_LENGTH);
EVP_EncryptInit(&evpctx, EVP_bf_cbc(), key, iv);
EVP_EncryptUpdate(&evpctx, ret, &ol, data, inl);
EVP_EncryptFinal(&evpctx, ret + ol, &tmp);
*rb = ol + tmp;
return ret;
}
char *se_evp_decrypt(char *ssid, char *ct, int inl, char *pt)
{
int ol, tmp;
EVP_CIPHER_CTX evpctx;
char key[EVP_MAX_KEY_LENGTH] = {0};
char iv[EVP_MAX_IV_LENGTH] = {0};
char final[EVP_MAX_BLOCK_LENGTH];
*pt = '\0';
strncpy(key, ssid, EVP_MAX_KEY_LENGTH);
strncpy(iv, ssid, EVP_MAX_IV_LENGTH);
EVP_DecryptInit(&evpctx, EVP_bf_cbc(), key, iv);
EVP_DecryptUpdate(&evpctx, pt, &ol, ct, inl);
EVP_DecryptFinal(&evpctx, pt+ol , &tmp);
pt[ol+tmp] = 0;
return pt;
}
int main(int argc, char *argv[])
{
char str[] = "abcdef123456789";
char buf[256] = "", buf2[256] = "";
int i;
se_evp_encrypt("anyssid", str, strlen(str), buf, &i);
printf("Ciphertext is %d bytes. %d\n", i, strlen(str));
se_evp_decrypt("anyssid", buf, i, buf2);
printf("Decrypted: >>%s<<\n", buf2);
}
#endif

Related

What is the C++ equivalent of this C code

I'm trying to learn how to access c++ library from c, I understood I've to use extern "C", in order to test it, I started with the below working c code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void c_free_string(const char *str){
free(str);
}
char *concat(const char *str1, const char *str2)
{
char *res;
const char del[] = ", ";
res = malloc(strlen(str1) + strlen(str2) + strlen(del) + 1);
if (!res) {
fprintf(stderr, "malloc() failed: insufficient memory!\n");
return EXIT_FAILURE;
}
strcpy(res, str1);
strcat(res, del);
strcat(res, str2);
printf("Result: '%s'\n", res);
return res;
}
int main(void) {
const char str1[] = "First";
const char str2[] = "Second";
char* s = concat(str1, str2);
printf("Result: '%s'\n", s);
c_free_string(s);
return EXIT_SUCCESS;
}
I wanted to write the equivalent c++ code of the above
I want to be ensure using the correct declaration to be able to do c binding with the lib generated from this c++ code.
I started with the belowm but stuck:
#include <iostream>
#include <sstream>
extern "C"
void c_free_string(const char *str){
free(str);
}
extern "C"
char *concat(const char *str1, const char *str2)
{
std::cout << "Hello from C++";
stringstream ss;
ss << str1 << ", " << str2;
string res = ss.str();
std::cout << "Result: "<<res;
return res;
}
Thanks for the comments, the answer looks to be:
#include <iostream>
#include <sstream>
extern "C"
void c_free_string(char *str){
free(str);
}
extern "C"
char *concat(const char *str1, const char *str2)
{
printf("welcome from c++\n");
char *res;
const char del[] = ", ";
res = (char*) malloc(strlen(str1) + strlen(str2) + strlen(del) + 1);
if (!res) {
fprintf(stderr, "malloc() failed: insufficient memory!\n");
exit(EXIT_FAILURE);
}
strcpy(res, str1);
strcat(res, del);
strcat(res, str2);
printf("Result: '%s'\n", res);
return res;
}
UODATE
A cleaner version based on Paul McKenzie feedback:
#include <iostream>
#include <string>
#include <cstring>
extern "C"
void c_free_string(char *str){
delete [] str;
}
extern "C"
char *concat(const char *str1, const char *str2)
{
const char* del = ", ";
int len1 = strlen(str1);
int len2 = strlen(del);
int len3 = strlen(str2);
char *res = new char [len1 + len2 + len3 + 1]{};
strcpy(res, str1);
strcpy(res + len1, del);
strcpy(res + len1 + len2, str2);
return res;
}
int main()
{
char *test = concat("abc", "123");
std::cout << test;
c_free_string(test);
}

update openssl, do not work hmac

there is code
#include <stdio.h>
#include <string.h>
#include <openssl/hmac.h>
int main() {
const char key[] = "012345678";
char data[] = "hello world";
unsigned char* result;
unsigned int len = 20;
result = (unsigned char*)malloc(sizeof(char) * len);
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha1(), NULL);
HMAC_Update(&ctx, (unsigned char*)&data, strlen(data));
HMAC_Final(&ctx, result, &len);
HMAC_CTX_cleanup(&ctx);
printf("HMAC digest: ");
for (int i = 0; i != len; i++)
printf("%02x", (unsigned int)result[i]);
printf("\n");
free(result);
}
output HMAC digest: e19e220122b37b708bfb95aca2577905acabf0c0
I can not update this code for openssl 1.1.0.
The new version changed the syntax hmac.
#include <iostream>
#include <string.h>
#include <openssl/hmac.h>
using namespace std;
int main() {
const char key[] = "012345678";
char data[] = "hello world";
int datalen = strlen(data);
int keylen = strlen(key);
unsigned char* result;
unsigned int len = 20;
result = (unsigned char*)malloc(sizeof(char) * len);
cout << datalen << endl;
cout << keylen << endl;
cout << len << endl;
HMAC_CTX *HMAC_CTX_new();
int HMAC_CTX_reset(HMAC_CTX *ctx);
int HMAC_Init_ex(HMAC_CTX *ctx, const char key, int keylen, const EVP_MD EVP_sha1());
int HMAC_Update(HMAC_CTX * ctx, char &data, int datalen);
int HMAC_Final(HMAC_CTX * ctx, char result, int &len);
int HMAC_CTX_cleanup(HMAC_CTX * ctx);
printf("HMAC digest: ");
for (int i = 0; i != len; i++)
printf("%02x", (unsigned int)result[i]);
printf("\n");
free(result);
return 0;
}
output HMAC digest: 0000000000000000000000000000000000000000
I will be glad of any help

Decrypt AES in C++ Example

I need some help with decrypt a char array in C++ using AES decrypt with Open SSL library. I already done encryption mode and works fine, but decryption is not working.
This is the Encrypt Function:
string Encrypt(char *Key, char *Msg, int size)
{
static char* Res;
static const char* const lut = "0123456789ABCDEF";
string output;
AES_KEY enc_key;
Res = (char *)malloc(size);
AES_set_encrypt_key((unsigned char *)Key, 128, &enc_key);
for(int vuelta = 0; vuelta <= size; vuelta += 16)
{
AES_ecb_encrypt((unsigned char *)Msg + vuelta, (unsigned char *)Res + vuelta, &enc_key, AES_ENCRYPT);
}
output.reserve(2 * size);
for (size_t i = 0; i < size; ++i)
{
const unsigned char c = Res[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
free(Res);
return output;
}
This is the Decrypt Function (not working):
char * Decrypt( char *Key, char *Msg, int size)
{
static char* Res;
AES_KEY dec_key;
Res = ( char * ) malloc( size );
AES_set_decrypt_key(( unsigned char * ) Key, 128, &dec_key);
for(int vuelta= 0; vuelta<=size; vuelta+=16)
{
AES_ecb_encrypt(( unsigned char * ) Msg+vuelta, ( unsigned char * ) Res+vuelta, &dec_key, AES_DECRYPT);
}
return (Res);
}
This is an Example of the Main function that call the methods, the problem is thar no mather how i print the "Res" variable in the Decrypt function, it always show random ASCII values, and i like to show the result in a string like the Encrypt function:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "openSSL/aes.h"
using namespace std;
int main(int argc, char const *argv[])
{
char key[16];
char message[128];
char enc_message[128];
string s_key = "THIS_IS_THE_KEY_";
string s_message = "Hello World !!!";
memset(key, 0, sizeof(key));
strcpy(key, s_key.c_str());
memset(message, 0, sizeof(message));
strcpy(message, s_message.c_str());
string response = Encrypt(key, message, sizeof(message));
cout<<"This is the Encrypted Message: "<<response<<endl;
memset(enc_message, 0, sizeof(enc_message));
strcpy(enc_message, response.c_str());
Decrypt(key, enc_message, sizeof(enc_message));
return 0;
}
Any improve in this methods?
I wanted to put the answer to how I solved it: The problem with my example was that I was trying to use the decrypt function with a HEXADECIMAL STRING and it should be done with an ASCII STRING with the values ​​as delivered by the encryption function.
That is, instead of trying to decrypt a string like this: 461D019896EFA3
It must be decrypted with a string like this: #(%_!#$
After that, the decryption will be delivered in ASCII values. They must be passed to Hexadecimal and finally to a String.
Here is the example that worked for me:
string Decrypt_string(char *Key, string HEX_Message, int size)
{
static const char* const lut = "0123456789ABCDEF";
int i = 0;
char* Res;
AES_KEY dec_key;
string auxString, output, newString;
for(i = 0; i < size; i += 2)
{
string byte = HEX_Message.substr(i, 2);
char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
auxString.push_back(chr);
}
const char *Msg = auxString.c_str();
Res = (char *)malloc(size);
AES_set_decrypt_key((unsigned char *)Key, 128, &dec_key);
for(i = 0; i <= size; i += 16)
{
AES_ecb_encrypt((unsigned char *)Msg + i, (unsigned char *)Res + i, &dec_key, AES_DECRYPT);
}
output.reserve(2 * size);
for (size_t i = 0; i < size; ++i)
{
const unsigned char c = Res[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
int len = output.length();
for(int i = 0; i < len; i += 2)
{
string byte = output.substr(i, 2);
char chr = (char) (int)strtol(byte.c_str(), NULL, 16);
newString.push_back(chr);
}
free(Res);
return newString;
}

OpenSSL AES 128 CBC \0 crash enrypted char*

I have problem with OpenSSL AES ( I use aes.h ) :
get binary file (.pdf , .jpg ) or some .xml , .txt about 5000 chars , then i enrypt base64.
When I try enrypt AES i get bad size ( Random 400 , 200 , 50 ) , my AESKey is random 128bits from chars : [abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ,.-##&*oeOE¯_ ]
I think that problem is '\0' enrypted char but i don't know can I extort entry
(string can save char array with \0 element but unsigned char* and char* is aborted )
this is my code :
std::string PFHelper::ASE_encode(std::string in, wchar_t* KS)
{
//const unsigned char* aes_input = reinterpret_cast<const unsigned char *> (in.c_str());
unsigned char* aes_input = new unsigned char[in.length()];
strcpy((char*)aes_input, in.c_str());
std::string KS_string = PFHelper::ConvertFromUtf8ToString(KS);
unsigned char* aes_key = new unsigned char[16];
strcpy((char*)aes_key, KS_string.c_str());
/* Input data to encrypt */
unsigned char iv[AES_BLOCK_SIZE];
memset(iv, 0x00, AES_BLOCK_SIZE);
const size_t encslength = ((in.length() + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
/* Buffers for Encryption and Decryption */
unsigned char * enc_out = new unsigned char [encslength];
//unsigned char * dec_out = new unsigned char[in.length()];
memset(enc_out, 0, encslength);
//memset(dec_out, 0, in.length());
AES_KEY enc_key;
AES_set_encrypt_key(aes_key, 128, &enc_key);
AES_cbc_encrypt(aes_input, enc_out, encslength, &enc_key, iv, AES_ENCRYPT);
//AES_KEY decrypt;
//memset(iv, 0x00, AES_BLOCK_SIZE);
//AES_cbc_encrypt((unsigned char*)enc_out, dec_out, encslength, &decrypt, iv, AES_DECRYPT);
//std::string returned = ConvertFromUnsignedCharToString(enc_out);
memset(aes_key, 0x00, 16);
memset(aes_input, 0x00, in.length());
return ConvertFromUnsignedCharToString(enc_out);
}
Sample value :
KS (AESKey) : L"F-ZTNW meOJLK1s5"
in(5464chars) : PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVR.....
out(51chars) : "©¦ľ‘Ň·rnoŚ8nžęwřëůl2ěY ßJ2¨ßňO× ohX­‚Ž~ŚČ”E
I try EVP and typical char key and problem is the same
//set back to normal
unsigned char* aes_input = new unsigned char[in.length()];
strcpy((char*)aes_input, in.c_str());
unsigned char* dec_out = new unsigned char[in.length()];
memset(dec_out, 0, in.length());
dec_out[in.length()] = '\0';
/* A 256 bit key */
unsigned char *key = (unsigned char *)"01234567890123456789012345678901";
/* A 128 bit IV */
unsigned char *iv = (unsigned char *)"01234567890123456";
int lenght;
int c_len = in.length() + AES_BLOCK_SIZE;
//Set up encryption
int f_len = 0;
EVP_CIPHER_CTX *ctx;
ctx = EVP_CIPHER_CTX_new();
if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv) != 1)
{
wcout << L"1";
}
if (EVP_EncryptUpdate(ctx, dec_out, &lenght, aes_input, in.length()) != 1)
{
wcout << L"2";
}
if (EVP_EncryptFinal_ex(ctx, dec_out, &lenght) != 1)
{
wcout << L"3";
}
return ConvertFromUnsignedCharToString(dec_out);
}
Here's an example which demonstrates how to use std::strings to manage buffers while using OpenSSL's EVP interfaces. It also avoids the extra copying you are doing. You still need to improve your keying strategy.
You should provide a zeroize'ing allocator. You should consider an Authenticated Encryption mode.
Compile it with g++ -std=c++11 test.cxx -o test.exe -lcrypto.
#include <iostream>
#include <string>
#include <memory>
#include <stdexcept>
using namespace std;
#include <openssl/evp.h>
#include <openssl/rand.h>
static const unsigned int KEY_SIZE = 16;
static const unsigned int BLOCK_SIZE = 16;
typedef unsigned char byte;
using EVP_CIPHER_CTX_free_ptr = std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)>;
void gen_keys(byte key[KEY_SIZE], byte iv[BLOCK_SIZE]);
void encrypt(const byte key[KEY_SIZE], const byte iv[BLOCK_SIZE], const string& ptext, string& ctext);
void decrypt(const byte key[KEY_SIZE], const byte iv[BLOCK_SIZE], const string& ctext, string& rtext);
int main(int argc, char* argv[])
{
// plaintext, ciphertext, recovered text
string ptext = "Now is the time for all good men to come to the aide of their country";
string ctext, rtext;
byte key[KEY_SIZE], iv[BLOCK_SIZE];
gen_keys(key, iv);
encrypt(key, iv, ptext, ctext);
decrypt(key, iv, ctext, rtext);
cout << "Recovered message:\n" << rtext << endl;
return 0;
}
void gen_keys(byte key[KEY_SIZE], byte iv[BLOCK_SIZE])
{
int rc = RAND_bytes(key, KEY_SIZE);
if (rc != 1)
throw runtime_error("RAND_bytes key failed");
rc = RAND_bytes(iv, BLOCK_SIZE);
if (rc != 1)
throw runtime_error("RAND_bytes for iv failed");
}
void encrypt(const byte key[KEY_SIZE], const byte iv[BLOCK_SIZE], const string& ptext, string& ctext)
{
EVP_CIPHER_CTX_free_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
int rc = EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cbc(), NULL, key, iv);
if (rc != 1)
throw runtime_error("EVP_EncryptInit_ex failed");
// Cipher text will be upto 16 bytes larger than plain text
ctext.resize(ptext.size()+16);
int out_len1 = (int)ctext.size();
rc = EVP_EncryptUpdate(ctx.get(), (byte*)&ctext[0], &out_len1, (const byte*)&ptext[0], (int)ptext.size());
if (rc != 1)
throw runtime_error("EVP_EncryptUpdate failed");
int out_len2 = (int)ctext.size() - out_len1;
rc = EVP_EncryptFinal_ex(ctx.get(), (byte*)&ctext[0]+out_len1, &out_len2);
if (rc != 1)
throw runtime_error("EVP_EncryptFinal_ex failed");
ctext.resize(out_len1 + out_len2);
}
void decrypt(const byte key[KEY_SIZE], const byte iv[BLOCK_SIZE], const string& ctext, string& rtext)
{
EVP_CIPHER_CTX_free_ptr ctx(EVP_CIPHER_CTX_new(), ::EVP_CIPHER_CTX_free);
int rc = EVP_DecryptInit_ex(ctx.get(), EVP_aes_128_cbc(), NULL, key, iv);
if (rc != 1)
throw runtime_error("EVP_DecryptInit_ex failed");
// Recovered text will be smaller than cipher text, not larger
rtext.resize(ctext.size());
int out_len1 = (int)rtext.size();
rc = EVP_DecryptUpdate(ctx.get(), (byte*)&rtext[0], &out_len1, (const byte*)&ctext[0], (int)ctext.size());
if (rc != 1)
throw runtime_error("EVP_DecryptUpdate failed");
int out_len2 = (int)rtext.size() - out_len1;
rc = EVP_DecryptFinal_ex(ctx.get(), (byte*)&rtext[0]+out_len1, &out_len2);
if (rc != 1)
throw runtime_error("EVP_DecryptFinal_ex failed");
rtext.resize(out_len1 + out_len2);
}

openssl c++ 3DES file encryption decryption fail

I'm using openssl c lib to encrypt decrypt a file using 3DES, If the file contains a few words, the file will be encrypted and decrypted ok, But if the source plain file contains more words, it decrypted incorrectly. Any suggestions would be kind!
#include <stdio.h>
#include <stdlib.h>
#include <openssl/pem.h>
#include <openssl/conf.h>
#include <openssl/x509v3.h>
#include <openssl/pkcs12.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>
#include <openssl/md5.h>
#include <openssl/rc4.h>
#include <iostream>
using namespace std;
#define MAX_PATH 512
#define BUFLEN 2048
void DoEncrypt(const char* srcfile, const char* enc_file)
{
char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
char iv[EVP_MAX_IV_LENGTH] = "my iv";
char ciphertext[BUFLEN*2] = {0};
char plaintext[BUFLEN] = {0};
FILE* fpread = fopen(srcfile, "rb");
int iReadLen = 0;
if (NULL == fpread)
{
cout<<"do encrypt read src file fail" <<endl;
return;
}
FILE* fpwrite = fopen(enc_file, "w+");
if (NULL == fpwrite)
{
cout<<"enc_file to create fail" <<endl;
fclose(fpread);
return;
}
const EVP_CIPHER* cipherType = EVP_des_ede3_ecb();
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
int out_len;
EVP_EncryptInit(&ctx, cipherType, (const unsigned char*)mykey, (const unsigned char*)iv);
while ( (iReadLen = fread(plaintext, 1, BUFLEN, fpread)) > 0 )
{
memset(ciphertext, 0x00, sizeof(ciphertext));
EVP_EncryptUpdate(&ctx, (unsigned char*)ciphertext, &out_len, (const unsigned char*)plaintext, iReadLen);
fwrite(ciphertext, 1, out_len, fpwrite);
memset(plaintext, 0x00, sizeof(plaintext));
}
EVP_EncryptFinal(&ctx, (unsigned char*)ciphertext, &out_len);
fwrite(ciphertext, 1, out_len, fpwrite);
EVP_CIPHER_CTX_cleanup(&ctx);
fclose(fpread);
fclose(fpwrite);
cout<< "DoEncrypt finish" <<endl;
}
void DoDecrypt(const char* enc_file, const char* dec_file)
{
char mykey[EVP_MAX_KEY_LENGTH] = "this's my key";
char iv[EVP_MAX_IV_LENGTH] = "my iv";
char ciphertext[BUFLEN*2] = {0};
char plaintext[BUFLEN] = {0};
FILE* fpread = fopen(enc_file, "rb");
if (NULL == fpread)
{
cout<<"DoDecrypt enc file open fail" <<endl;
return;
}
FILE* fpwrite = fopen(dec_file, "w+");
if (NULL == fpwrite)
{
cout<< "DoDecrypt open dec file create fail" <<endl;
fclose(fpread);
return;
}
const EVP_CIPHER* cipherType = EVP_des_ede3_ecb();
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_DecryptInit(&ctx, (const EVP_CIPHER*)cipherType, (const unsigned char*)mykey, (const unsigned char*)iv);
int iReadLen, out_len;
while ( (iReadLen = fread(plaintext, 1, BUFLEN, fpread)) > 0 )
{
memset(ciphertext, 0x00, sizeof(ciphertext));
EVP_DecryptUpdate(&ctx, (unsigned char*)ciphertext, &out_len, (const unsigned char*)plaintext, iReadLen);
fwrite(ciphertext, 1, out_len, fpwrite);
memset(plaintext, 0x00, sizeof(plaintext));
}
EVP_DecryptFinal(&ctx, (unsigned char*)ciphertext, &out_len);
fwrite(ciphertext, 1, out_len, fpwrite);
EVP_CIPHER_CTX_cleanup(&ctx);
fclose(fpread);
fclose(fpwrite);
cout<< "DoDecrypt finished" <<endl;
}
int main(void)
{
const char* srcfile = "abc.txt";
const char* enc_file = "abc.txt.enc";
const char* dec_file = "abc.txt.dec";
DoEncrypt(srcfile, enc_file);
DoDecrypt(enc_file, dec_file);
return 0;
}
You need "w+b" instead of "w+" in your fopen calls.