Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why it fails to decrypt with OpenSSL
Forum Updated to NodeBB v4.3 + New Features

Why it fails to decrypt with OpenSSL

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lincoln
    wrote on 2 Sept 2023, 17:22 last edited by
    #1

    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;
        });
    

    ae047320-61a4-47cb-b3dc-98bd131eeece-image.png

    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.

    Solitary wolf

    1 Reply Last reply
    0
    • S SGaist
      2 Sept 2023, 19:01

      Hi,

      Might be a silly question but does it work as is on the command line ?

      C Offline
      C Offline
      ChrisW67
      wrote on 3 Sept 2023, 05:12 last edited by
      #3

      @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 1 Reply Last reply 3 Sept 2023, 16:08
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 2 Sept 2023, 19:01 last edited by
        #2

        Hi,

        Might be a silly question but does it work as is on the command line ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        C 1 Reply Last reply 3 Sept 2023, 05:12
        0
        • S SGaist
          2 Sept 2023, 19:01

          Hi,

          Might be a silly question but does it work as is on the command line ?

          C Offline
          C Offline
          ChrisW67
          wrote on 3 Sept 2023, 05:12 last edited by
          #3

          @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 1 Reply Last reply 3 Sept 2023, 16:08
          2
          • C ChrisW67
            3 Sept 2023, 05:12

            @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 Offline
            L Offline
            lincoln
            wrote on 3 Sept 2023, 16:08 last edited by
            #4

            @ChrisW67 Thanks for your answer, by the way I mention that I was testing the QAESEncryption class, and it also gave me a good result. thanks again for your comment

            Solitary wolf

            1 Reply Last reply
            0
            • L lincoln has marked this topic as solved on 3 Sept 2023, 16:08

            1/4

            2 Sept 2023, 17:22

            • Login

            • Login or register to search.
            1 out of 4
            • First post
              1/4
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved