I am trying to encrypt and decrypt a communication between a C++ library and a PHP server using OPENSSL library in both of them. I want to use the Blowfish CBC algorithm but it seems that the results are different between the C++ code and the PHP code. The C++ code is taken from here:
This is the PHP code:
<?php
function strtohex($x)
{
$s='';
foreach (str_split($x) as $c) $s.=sprintf("%02X",ord($c));
return($s);
}
$encryptedMessage = openssl_encrypt("input", "BF-CBC", "123456", 0, "initvect");
echo $encryptedMessage;
echo "\n";
echo strtohex($encryptedMessage);
The PHP output is this:
x9jDa2WMwvQ=
78396A446132574D7776513D
This is the c++ code:
bool do_encrypt(const char *in, unsigned char *out, int *outlen, unsigned char *key, unsigned char *iv)
{
int buflen, tmplen;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), nullptr, key, iv);
if (!EVP_EncryptUpdate(&ctx, out, &buflen, (unsigned char*)in, strlen(in)))
{
return false;
}
if (!EVP_EncryptFinal_ex(&ctx, out + buflen, &tmplen))
{
return false;
}
buflen += tmplen;
*outlen = buflen;
EVP_CIPHER_CTX_cleanup(&ctx);
return true;
}
unsigned char output[2048] = { 0 };
int outLen;
auto result = do_encrypt("input", output, &outLen, (unsigned char*)"123456", (unsigned char*)"initvect");
BIGNUM *outputStr = BN_new();
BN_bin2bn(output, outLen, outputStr);
cout << base64_encode(output, outLen) << "\n";
cout << BN_bn2hex(outputStr) << "\n";
The C++ output is this:
EfRhhWqGmSQ=
11F461856A869924
Can someone please help me understand what I'm doing wrong? Any help will be very much appreciated.
Thanks!
Edit 1:
I managed to fix the C++ code after jww's answer and it worked well. I was missing the EVP_CIPHER_CTX_set_key_length However, I couldn't make the PHP code return the same thing and eventually we decided to move to AES and it now works flawlessly. Thanks!
For your OpenSSL code, I believe you need to call EVP_CIPHER_CTX_set_key_length to tell the library the key is only 6 bytes.
Let me throw Crypto++ into the arena below. OpenSSL and Crypto++ will converge on the right answer once you add the missing EVP_CIPHER_CTX_set_key_length OpenSSL call. The right answer is 32CEBA7E046431EB (in hex).
I don't know what's going on with PHP:
x9jDa2WMwvQ=
78396A446132574D7776513D
Considering x is ASCII 0x78, 9 is ASCII 0x39, I'm guessing you hex encoded the Base64 string.
$ cat test.cxx
#include "blowfish.h"
#include "modes.h"
#include "channels.h"
#include "filters.h"
#include "base64.h"
#include "hex.h"
using namespace CryptoPP;
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
const byte key[] = "123456"; // 6
const byte iv[] = "initvect"; // 8
CBC_Mode<Blowfish>::Encryption encryptor;
encryptor.SetKeyWithIV(key, 6, iv, 8);
string r1, r2;
ChannelSwitch chsw;
Base64Encoder e1(new StringSink(r1));
HexEncoder e2(new StringSink(r2));
chsw.AddDefaultRoute(e1);
chsw.AddDefaultRoute(e2);
string msg = "input";
StringSource ss(msg, true,
new StreamTransformationFilter(encryptor,
new Redirector(chsw)));
cout << r1 << endl;
cout << r2 << endl;
return 0;
}
The test program results in:
$ ./test.exe
Ms66fgRkMes=
32CEBA7E046431EB
Here's the OpenSSL portion of things. Notice EVP_EncryptInit_ex is called twice. First, EVP_EncryptInit_ex is called to set the block cipher EVP_bf_cbc. The key length is set with EVP_CIPHER_CTX_set_key_length. Then second, EVP_EncryptInit_ex is called to set the key and iv.
#include <openssl/evp.h>
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
typedef unsigned char byte;
int main()
{
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
int rc;
const byte key[] = "123456"; // 6
const byte iv[] = "initvect"; // 8
rc = EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, 0, 0);
if (rc != 1)
throw runtime_error("EVP_EncryptInit_ex failed");
rc = EVP_CIPHER_CTX_set_key_length(&ctx, 6);
if (rc != 1)
throw runtime_error("EVP_CIPHER_CTX_set_key_length failed");
rc = EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv);
if (rc != 1)
throw runtime_error("EVP_EncryptInit_ex failed");
const byte msg[] = "input";
byte buf[32];
int len1 = sizeof(buf), len2 = sizeof(buf);
rc = EVP_EncryptUpdate(&ctx, buf, &len1, msg, 5);
if (rc != 1)
throw runtime_error("EVP_EncryptUpdate failed");
rc = EVP_EncryptFinal_ex(&ctx, buf+len1, &len2);
if (rc != 1)
throw runtime_error("EVP_EncryptFinal_ex failed");
for(unsigned int i=0; i<len1+len2; i++)
cout << std::hex << setw(2) << setfill('0') << (int)buf[i];
cout << endl;
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
Related
I'm trying to encrypt data with RSA in a C program, that I send with POST method on my server and then decrypt it in PHP. For both program I use OpenSSL to encrypt / decrypt the data.
I have the error "error:0407109F:rsa routines:RSA_padding_check_PKCS1_type_2:pkcs decoding error" during the decryption in PHP and no data is return by the openssl_private_decrypt function.
Here is my C program :
RSA *createRSA(unsigned char *key, int public)
{
RSA *rsa = RSA_new();
BIO *keybio;
keybio = BIO_new_mem_buf(key, -1);
if (keybio == NULL)
return 0;
if (public)
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
else
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
return rsa;
}
char *rsa_encode(char *json)
{
char *data;
RSA *rsa;
int encrypted_length = 0;
char pub[] = "MY_PUBLIC_KEY"; //RSA public key of size 8192
rsa = createRSA((unsigned char *)pub, 1);
if (!(data = malloc(RSA_size(rsa))))
exit(0);
encrypted_length = RSA_public_encrypt((int)strlen(json), (unsigned char *)json, (unsigned char *)data, rsa, RSA_PKCS1_PADDING);
RSA_free(rsa);
if (encrypted_length > 0)
return data;
return NULL;
}
char *bin2hex(const unsigned char *key, int size)
{
char *hex;
int i = 0;
if (!(hex = malloc((size * 2 + 1) * sizeof(char))))
handleError();
while (i < size)
{
sprintf((char *)(hex + (i * 2)),"%02x", key[i]);
i++;
}
hex[i * 2] = '\0';
return hex;
}
Here is my PHP program :
$hexa = $_POST["data"];
$data = hexToStr($hexa);
$privateKey = openssl_get_privatekey("file://private.pem");
$decrypted = "";
$ret = openssl_private_decrypt($data, $decrypted, $privateKey, OPENSSL_PKCS1_PADDING);
echo openssl_error_string() . "\n";
I don't understand where is the error, for both program I use OPENSSL_PKCS1_PADDING so why OpenSSL error tell me that the error is on my padding ?
I finally found why decryption didn't work.
I was using this line to encode in hexa in C :
encoded = bin2hex(data, (int)strlen((char *)data);
data (the encrypted data) is unsigned char * and can contain null bytes. But I was using strlen which stop on first null byte. So my encrypted data was truncated when it was sended on server.
So the solution is to use the length of data that is given in return value of len = RSA_public_encrypt((int)strlen((char *)src), src, encrypted, rsa, RSA_PKCS1_PADDING); instead of (int)strlen((char *)data);
Thanks you all for your help !
I'm not a programmer by trade, so please bear with me...
I have an application I am using that unfortunately stores passwords in plaintext in MySQL, which is something I do not want. As the program does makes use of the OpenSSL library, I have access to the aes functions.
Below I've cobbled together demo code that uses these functions to encrypt a test string and uses MD5 to hash it (since the encrypted text is binary):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#include <openssl/md5.h>
char *str2md5(const char *str, int length) {
int n;
MD5_CTX c;
unsigned char digest[16];
char *out = (char*)malloc(33);
MD5_Init(&c);
while (length > 0) {
if (length > 512) {
MD5_Update(&c, str, 512);
} else {
MD5_Update(&c, str, length);
}
length -= 512;
str += 512;
}
MD5_Final(digest, &c);
for (n = 0; n < 16; ++n) {
snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
}
return out;
}
int main(int argc, char* argv[]) {
AES_KEY aesKey_;
unsigned char userKey_[16];
unsigned char in_[16];
unsigned char out_[16];
strcpy(userKey_,"1234567890abcdef");
strcpy(in_,"texttoencrypt");
fprintf(stdout,"Original message: %s\n", in_);
AES_set_encrypt_key(userKey_, 128, &aesKey_);
AES_encrypt(in_, out_, &aesKey_);
char *output = str2md5(out_, strlen(out_));
fprintf(stdout,"MD5 of Encrypted message: %s\n", output);
AES_set_decrypt_key(userKey_, 128, &aesKey_);
AES_decrypt(out_, in_,&aesKey_);
fprintf(stdout,"Recovered Original message: %s\n", in_);
return 0;
}
This outputs:
Original message: texttoencrypt
MD5 of Encrypted message: 3675b450ae0415e5a8521b9bb7ee01ba
Recovered Original message: texttoencrypt
Now in PHP I am using this code to generate the various AES-128 encrypted strings and similarly, MD5ing the result:
<?php
$methods = openssl_get_cipher_methods();
$plain = "texttoencrypt";
$password = "1234567890abcdef";
foreach ($methods as $method) {
if (preg_match('/AES-128/', $method)) {
$encrypted = openssl_encrypt($plain, $method, $password);
$decrypted = openssl_decrypt($encrypted, $method, $password);
echo $method . ' : ' . md5($encrypted) . ' ; ' . $decrypted . "\r\n";
}
}
?>
Output:
AES-128-CBC : 08d6f8e2ae21a7a506fabf91adcc3b63 ; texttoencrypt
AES-128-CFB : ce10ea28d7607bd6514e478e025e47c6 ; texttoencrypt
AES-128-CFB1 : 6adde484b8bee26f9b1ca7856634586d ; texttoencrypt
AES-128-CFB8 : aea100f1473c0a3d6380dd0f28585e19 ; texttoencrypt
AES-128-ECB : 08d6f8e2ae21a7a506fabf91adcc3b63 ; texttoencrypt
AES-128-OFB : ce10ea28d7607bd6514e478e025e47c6 ; texttoencrypt
Unfortunately, I am not getting a match to the 3675b450ae0415e5a8521b9bb7ee01ba generated by the C code. I've tried just about every comment I've seen on the PHP manual pages and here on SE, but can't get a match.
I can't modify the C code, just the PHP... so any pointers on how to get PHP to match the C output is certainly appreciated!
AES_encrypt(in_, out_, &aesKey_);
char *output = str2md5(out_, strlen(out_));
Who is taking care of null terminating out so strlen works as expected? Certainly not AES_encrypt.
Moreover in strcpy(userKey_,"1234567890abcdef"); you are copying 17 bytes of data (you have to count the null terminator) to an array 16 of char.
What will be the C++ equivalemt command for below mentioned php command:
$command = shell_exec("sqlldr {$connect_string} control={$ctl_file_name} log={$log_file_name}");
So based on your comments a solution that would work would be to use popen(3):
#include <cstdio>
#include <iostream>
#include <string>
int main()
{
// Set file names based on your input etc... just using dummies below
std::string
ctrlFileName = "file1",
logFileName = "file2",
cmd = "sqlldr usr/pwd#LT45 control=" + ctrlFileName + " log=" + logFileName ;
std::cout << "Executing Command: " << cmd << std::endl ;
FILE* pipe = popen(cmd.c_str(), "r");
if (pipe == NULL)
{
return -1;
}
char buffer[128];
std::string result = "";
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
{
result += buffer;
}
}
std::cout << "Results: " << std::endl << result << std::endl ;
pclose(pipe);
}
Try forkpty, you get a file descriptor which you can use to read from the other pseudoterminal.
This is my c++ code:
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QFile>
#include <QByteArray>
QByteArray UnZip (QString zipfilename)
{
QFile infile(zipfilename);
infile.open(QIODevice::ReadOnly);
//QByteArray uncompressedData = infile.readAll();
QByteArray uncompressedData = qUncompress(infile.readAll());
infile.close();
return uncompressedData;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//QString path = "/Users/davide/Desktop/fh8RkbUf";
QString path = "/Users/davide/Desktop/test";
QByteArray data = UnZip(path);
qDebug() << "message";
return a.exec();
}
; that returns qUncompress: Z_DATA_ERROR: Input data is corrupted
However, data should be zlib compressed and php's gzuncompress($data) works fine.
Also, cat file | uncompress returns errors.
The code php side is
$data = gzcompress($data, 6);
$success = file_put_contents($file, $data);
I got a problem implementing a PHP programm in C++. It is about the PHP/Perl function unpack. I don't know how to do the follwing in C++ (no problem in reading a file... but how do i unpack("C*") the read contents).
<?php
$file = fopen("bitmaskt.dat", "rb");
//create the data stream
$matrix_x = unpack("C*", fread($file, 286));
$matrix_y = unpack("C*", fread($file, 286));
$mask_data = unpack("C*", fread($file, 286));
$reed_ecc_codewords = ord(fread($file, 1));
$reed_blockorder = unpack("C*", fread($file, 128));
fclose($file);
?>
Currently, I'm very hopeless solving this problem on my own - I'm searching for days, all I found are questions... Is there any free unpack() c++ implementation out there? :-(
Perl's documentation for pack covers the templates used for pack and unpack.
Say you generated bitmaskt.dat with
#! /usr/bin/perl
use warnings;
use strict;
open my $fh, ">", "bitmaskt.dat" or die "$0: open: $!";
my #data = (42) x 286;
print $fh pack("C*" => #data);
print $fh pack("C*" => #data);
print $fh pack("C*" => #data);
print $fh pack("C" => 7);
print $fh pack("C*" => (1) x 128);
close $fh or warn "$0: close";
You might read it with
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
typedef unsigned char datum_t;
typedef std::vector<datum_t> buf_t;
std::istream &read_data(std::istream &in, buf_t &buf, size_t n)
{
std::istreambuf_iterator<char> it(in.rdbuf()), eos;
while (it != eos && n-- != 0)
buf.push_back(static_cast<datum_t>(*it++));
return in;
}
For example:
int main()
{
std::ifstream bm("bitmaskt.dat", std::ifstream::binary | std::ifstream::in);
struct {
buf_t buf;
size_t len;
std::string name;
} sections[] = {
{ buf_t(), 286, "matrix_x" },
{ buf_t(), 286, "matrix_y" },
{ buf_t(), 286, "mask_data" },
{ buf_t(), 1, "reed_ecc_codewords" },
{ buf_t(), 128, "reed_blockorder" },
};
const int n = sizeof(sections) / sizeof(sections[0]);
for (int i = 0; n - i > 0; i++) {
if (!read_data(bm, sections[i].buf, sections[i].len)) {
std::cerr << "Read " << sections[i].name << " failed" << std::endl;
return 1;
}
}
const int codeword = 3;
std::cout << (unsigned int) sections[codeword].buf[0] << '\n';
return 0;
}
Output:
7
I don't know about any general implementation of unpack for C++, but that doesn't seem to be the thing you need anyway.
if matrix_x is defined somewhere as unsigned char matrix_x[286] and you have an opened input stream inFile
then what you need to do is inFile.get(matrix_x, 286). This reads 286 bytes from the input and places them in the array pointed to by matrix_x.