I am trying to write an encryption - decryption programm, which uses gost89 to encrypt and decrypt data. Everything works fine, but when I try to cast QString to unsigned char and use it as a key, the programm fails to decrypt.
The code:
#include <cstdio>
#include <iostream>
#include <openssl/conf.h>
#include <openssl/err.h>
#include <openssl/engine.h>
#include <openssl/evp.h>
#include <fstream>
#include <QString>
#include <QDebug>
#include <QFile>
using std::cerr;
using std::endl;
void encryptdata(QString pass, QString data){
OPENSSL_add_all_algorithms_conf();
ENGINE *engine_gost = ENGINE_by_id("gost");
const EVP_CIPHER * cipher_gost = EVP_get_cipherbyname("gost89");
unsigned char *key = (unsigned char * )"password";
qDebug() << (char*)key;
unsigned char * iv = (unsigned char * ) "12345678";
unsigned char *text = (unsigned char*)"Hello World";
int text_len = 11;
unsigned char ciph[512];
int ciph_len = 0;
EVP_CIPHER_CTX * ctx = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(ctx);
int init = EVP_EncryptInit_ex(ctx, cipher_gost, engine_gost, key, iv);
int enc = EVP_EncryptUpdate(ctx, ciph, &ciph_len, text, text_len);
std::ofstream myfile;
myfile.open ("example.bin");
for (int i = 0; i < text_len; i ){
myfile << ciph[i];
}
myfile.close();
EVP_CIPHER_CTX_free(ctx);
}
void decryptdata(){
OPENSSL_add_all_algorithms_conf();
ENGINE *engine_gost1 = ENGINE_by_id("gost");
const EVP_CIPHER * cipher_gost1 = EVP_get_cipherbyname("gost89");
unsigned char * key1 = (unsigned char * ) "password";
qDebug() << (char*)key1;
unsigned char * iv1 = (unsigned char * ) "12345678";
unsigned char text1[512];
int text_len1 = 11;
unsigned char ciph1[512];
int ciph_len1 = 0;
std::ifstream yourfile;
yourfile.open ("example.bin");
yourfile >> text1;
yourfile.close();
qDebug() << text1;
EVP_CIPHER_CTX * ctx1 = EVP_CIPHER_CTX_new();
EVP_CIPHER_CTX_init(ctx1);
int init = EVP_DecryptInit_ex(ctx1, cipher_gost1, engine_gost1, key1, iv1);
int enc = EVP_DecryptUpdate(ctx1, ciph1, &ciph_len1, text1, text_len1);
//int enc1 = EVP_DecryptFinal(ctx, ciph, &ciph_len);
for (int i = 0; i < text_len1; i ){
std::cout << ciph1[i];
}
std::cout << std::endl;
EVP_CIPHER_CTX_free(ctx1);
}
int main(){
//unsigned char t[512] = {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};
QString pss = "password";
QString dat = "Hello World";
encryptdata(pss, dat);
decryptdata();
}
I've tried lots of different casting methods, but they did not help
CodePudding user response:
unsigned char*
- implies it's a set of bytes that's read/write.
However "password" is const char*
- it's a readonly string literal.
It's dangerous to cast "password" to unsigned char*
.
You need to spend more effort in understanding memory management and read/writable memory.
Because you want it come from a QString, I would recommend the following steps:
- Consider using QString toUtf8() to convert the QString to a QByteArray
- Consider either (1) providing unsigned char* raw access to the QByteArray(), e.g. via data() or, (2) to copy it out to a allocated memory buffer.
CodePudding user response:
Finally completed the task. Yeah, the problem was really in the key. The encryption-decryption programm was firstly written by my stupid lector, and I had to repair it. The key size must be EXACTLY 256 bit(unsigned char key[32]), otherwise it is going to **** up everything.