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. Encrypt and Decrypt the password entered in QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Encrypt and Decrypt the password entered in QLineEdit

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 14.8k Views 3 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.
  • Pradeep KumarP Offline
    Pradeep KumarP Offline
    Pradeep Kumar
    wrote on last edited by
    #1

    Hi,

    I wan to encrypt and decrypt the value entered in QLineEdit,
    I used QCryptographicHash for encryption, can we have decryption using QCryptographicHash.

    Or any other method can i use,
    Please provide the guidance?.

    Thanks,

    Pradeep Kumar
    Qt,QML Developer

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by dheerendra
      #2

      You can do it. U can use simple base64 encoding as well. Not sure how strong u wud like to have encryption. You can use simple one suggested in https://wiki.qt.io/Simple_encryption_with_SimpleCrypt

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      3
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by
        #3

        Hi,

        Encryption example

        QString strValue = m_pQLineEdit->text();

        QByteArray pswNsalt (strValue.toStdString().c_str()) ;
        pswNsalt.append(STR_SALT_KEY) ;
        hashedSaltedPsw = QCryptographicHash::hash(pswNsalt, QCryptographicHash::Sha256).toHex() ;
        qDebug() << "encrypted psw: " << hashedSaltedPsw << endl ;
        

        How can i decrypt the value of hashedSaltedPsw

        Thanks,

        Pradeep Kumar
        Qt,QML Developer

        1 Reply Last reply
        0
        • Pradeep KumarP Offline
          Pradeep KumarP Offline
          Pradeep Kumar
          wrote on last edited by
          #4

          I used

          ```
          

          QByteArray ba;
          ba.append(string);
          return ba.toBase64();

          
          for encrypting.
          
          and 
          
          

          QByteArray ba;
          ba.append(string);
          return QByteArray::fromBase64(ba);

          
          for decrypting.
          
          Thanks,

          Pradeep Kumar
          Qt,QML Developer

          1 Reply Last reply
          2
          • Pradeep KumarP Offline
            Pradeep KumarP Offline
            Pradeep Kumar
            wrote on last edited by
            #5

            for decrypt how can it be done

            QByteArray pswNsalt (strValue.toStdString().c_str()) ;
            pswNsalt.append(STR_SALT_KEY) ;
            hashedSaltedPsw = QCryptographicHash::hash(pswNsalt, QCryptographicHash::Sha256).toHex() ;
            qDebug() << "encrypted psw: " << hashedSaltedPsw << endl ;

            for the value hashedSaltedPsw ?.

            Just want to know using QCryptographicHash?

            Thanks,

            Pradeep Kumar
            Qt,QML Developer

            A 1 Reply Last reply
            0
            • dheerendraD Offline
              dheerendraD Offline
              dheerendra
              Qt Champions 2022
              wrote on last edited by
              #6

              If you just want to use it for as password, use a QCryptographicHash generate Hash password, save to the file. During ycompare, hash the input and compare it to the saved password.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              5
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Just one important detail: base64 is by no mean related to encryption of cryptography. It just allows to represent binary data as text so you can for example send said binary data to a REST service using JSON.

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

                1 Reply Last reply
                7
                • Pradeep KumarP Pradeep Kumar

                  for decrypt how can it be done

                  QByteArray pswNsalt (strValue.toStdString().c_str()) ;
                  pswNsalt.append(STR_SALT_KEY) ;
                  hashedSaltedPsw = QCryptographicHash::hash(pswNsalt, QCryptographicHash::Sha256).toHex() ;
                  qDebug() << "encrypted psw: " << hashedSaltedPsw << endl ;

                  for the value hashedSaltedPsw ?.

                  Just want to know using QCryptographicHash?

                  Thanks,

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by ambershark
                  #8

                  @Pradeep-Kumar Hash values are one way. It only can be encrypted not decrypted.

                  Base64 like previously stated by @SGaist is not encryption at all. It is also easily recognizable so it would be quite obvious how it was "scrambled".

                  The proper way to use a hash for passwords is to do it one way. So the user enters their password, you run the hash on it and save the hashed value. Then when you need to authenticate the password your get the user input again, hash it, and then compare the hash to the previously saved hash.

                  This is how most operating systems save and validate user passwords.

                  Hashing is best for encrypting passwords because they can't be decrypted. So nobody gets your password. If you want to encrypt where you can decrypt as well you will need to use an actual algorithm. Something like AES, blowfish, twofish, etc. There are many secure ones out there with public domain implementations.

                  Be warned though that using a symmetrical algorithm like the ones above will require you to have a decrypt key or password. If this is part of an application storing that decrypt key will make it easy to hack and get the key with a debugger and/or hex editor. The only secure way is to use PPK encryption which is much harder to implement but you can google public/private key encryption for more info on that.

                  Hope that helps.

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  1 Reply Last reply
                  8

                  • Login

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