Why it fails to decrypt with OpenSSL
-
Hi guys, I have the following code, two functions, one to encrypt and the other to decrypt a text string, when encrypting it goes well, but when I want to decrypt, it shows me an empty string, why is this happening? Here I leave the code.
QString MainWindow::encryptPassword(const QString& password) { QProcess process; QStringList arguments; arguments << "enc" << "-aes-256-cbc" << "-a" << "-salt" << "-pass" << "pass:password" << "-in" << "-" << "-out" << "-"; process.setProgram("openssl"); process.setArguments(arguments); process.start(); if (!process.waitForStarted()) { qDebug() << "Failed to start OpenSSL process."; return QString(); } process.write(password.toUtf8()); process.closeWriteChannel(); if (!process.waitForFinished()) { qDebug() << "Failed to encrypt password with OpenSSL."; return QString(); } QString encryptedPassword = process.readAllStandardOutput(); encryptedPassword.remove('\n'); return encryptedPassword; } QString MainWindow::decryptPassword(const QString& encryptedPassword) { QProcess process; QStringList arguments; arguments << "enc" << "-aes-256-cbc" << "-a" << "-d" << "-salt" << "-pass" << "pass:password" << "-in" << "-" << "-out" << "-"; process.setProgram("openssl"); process.setArguments(arguments); process.start(); if (!process.waitForStarted()) { qDebug() << "Failed to start OpenSSL process."; return QString(); } process.write(encryptedPassword.toUtf8()); process.closeWriteChannel(); if (!process.waitForFinished()) { qDebug() << "Failed to decrypt password with OpenSSL."; return QString(); } QString decryptedPassword = process.readAllStandardOutput(); decryptedPassword.remove('\n'); return decryptedPassword; } QObject::connect(ui->pushButton, &QPushButton::clicked, this, [&](){ auto texto = ui->lineEdit->text(); ui->lineEdit_2->setText(encryptPassword(texto)); }); QObject::connect(ui->pushButton_2, &QPushButton::clicked, this, [&](){ auto decrypt = decryptPassword(ui->lineEdit_2->text()); qInfo() << decrypt; });
As you can see in the image, it encrypts the text well, but when decrypting, it shows me an empty string, any suggestion would be appreciated.
-
@SGaist The encrypt command embedded in the code works as long as
openssl
is found.$ echo -n 'sample text' | openssl enc -aes-256-cbc -a -salt -pass pass:password -in - -out - *** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better. U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=
The decrypt command fails if the Base64 encoded, encrypted data is presented without a trailing new line (as it is in the code):
# No newline == fails $ echo -n 'U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=' | openssl enc -aes-256-cbc -d -a -salt -pass pass:password -in - -out - error reading input file # Newline == OK $ echo 'U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=' | openssl enc -aes-256-cbc -d -a -salt -pass pass:password -in - -out - *** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better. sample text
-
Hi,
Might be a silly question but does it work as is on the command line ?
-
@SGaist The encrypt command embedded in the code works as long as
openssl
is found.$ echo -n 'sample text' | openssl enc -aes-256-cbc -a -salt -pass pass:password -in - -out - *** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better. U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=
The decrypt command fails if the Base64 encoded, encrypted data is presented without a trailing new line (as it is in the code):
# No newline == fails $ echo -n 'U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=' | openssl enc -aes-256-cbc -d -a -salt -pass pass:password -in - -out - error reading input file # Newline == OK $ echo 'U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=' | openssl enc -aes-256-cbc -d -a -salt -pass pass:password -in - -out - *** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better. sample text
-
@SGaist The encrypt command embedded in the code works as long as
openssl
is found.$ echo -n 'sample text' | openssl enc -aes-256-cbc -a -salt -pass pass:password -in - -out - *** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better. U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=
The decrypt command fails if the Base64 encoded, encrypted data is presented without a trailing new line (as it is in the code):
# No newline == fails $ echo -n 'U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=' | openssl enc -aes-256-cbc -d -a -salt -pass pass:password -in - -out - error reading input file # Newline == OK $ echo 'U2FsdGVkX1/e8CegmWkpiyeZATHB1mn2Au0JRukanMw=' | openssl enc -aes-256-cbc -d -a -salt -pass pass:password -in - -out - *** WARNING : deprecated key derivation used. Using -iter or -pbkdf2 would be better. sample text
-
L lincoln has marked this topic as solved on