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. AES + Base64 return unknown symbols
Qt 6.11 is out! See what's new in the release blog

AES + Base64 return unknown symbols

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 669 Views 2 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.
  • LimerL Offline
    LimerL Offline
    Limer
    wrote on last edited by Limer
    #1

    -------------------encryption codes:

    
    const QByteArray MQTT_SERVER_USER_PASSWORD_KEY = "23d655b75684433f1l525ed23826eb19";
    
    // encryption 
    m_password = m_passwordLineEdit->text();
    QByteArray encodedPassword = AESEncryption::Crypt(AESEncryption::AES_256, AESEncryption::ECB, 
                                                      m_password.toLatin1(), MQTT_SERVER_USER_PASSWORD_KEY);
    QString base64Password = encodedPassword.toBase64().data();
    cJSON_AddStringToObject(root, "password", base64Password.toUtf8().data()); // cJSON library
    
    // save to local file
    char* data = cJSON_Print(root);
    if (data)
    {
        std::ofstream outfile;
        outfile.open("setting.json");
        if (outfile.is_open())
        {
            outfile << data;
            outfile.close();
        }
    }
    free(data);
    cJSON_Delete(root);
    

    ---------------------- decryption codes:

    // read from local file
    std::ifstream infile;
    infile.open("setting.json");
    std::stringstream stream;
    stream << infile.rdbuf();
    std::string str = stream.str();
    cJSON* root = cJSON_Parse(str.c_str());
    
    // decryption
    cJSON* passwordItem = cJSON_GetObjectItem(root , "password");
    if (passwordItem)
    {
        QByteArray encodedPassword = QString::fromUtf8(passwordItem->valuestring).toLatin1();
        QByteArray plainPassword = AESEncryption::Decrypt(AESEncryption::AES_256, AESEncryption::ECB, 
                                                          QByteArray::fromBase64(encodedPassword), MQTT_SERVER_USER_PASSWORD_KEY);
        m_pwdLineEdit->setText(plainPassword.data());
        m_password = plainPassword.data();
    }
    

    I use Qt-AES for encryption and decryption.

    I need to save some information to local setting profile, when next time, users open the software, he/she can see the information. But for private informations, such as password, we should encrypt them, that is why I use AES algorithm. Because cJSON and C++ std:: use utf8, so I need to transfer to utf8, and then write to file.

    Here is the result,

    0_1566610974746_1.gif

    Why a redundant ? character? I guess it's the encoding problem, but I can't find the real reason, could someone give me any advice? THanks in advance.

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      I have a suspicion, but find out what the value of the unwanted character is and post it, please. I think somewhere in your data entry, save to sjon, or read from json, you are adding a newline or null character.

      The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

      LimerL 1 Reply Last reply
      0
      • Kent-DorfmanK Kent-Dorfman

        I have a suspicion, but find out what the value of the unwanted character is and post it, please. I think somewhere in your data entry, save to sjon, or read from json, you are adding a newline or null character.

        LimerL Offline
        LimerL Offline
        Limer
        wrote on last edited by
        #3

        @kent-dorfman \n or `\0, but when added it? It's very strange.

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

          Hi,

          Why are you making these transformations when putting the password in a QByteArray ?

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

          LimerL 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Why are you making these transformations when putting the password in a QByteArray ?

            LimerL Offline
            LimerL Offline
            Limer
            wrote on last edited by
            #5

            @sgaist You mean the m_password.toLatin1() in encryption QByteArray encodedPassword = AESEncryption::Crypt(AESEncryption::AES_256, AESEncryption::ECB, m_password.toLatin1(), MQTT_SERVER_USER_PASSWORD_KEY); ?

            1 Reply Last reply
            0

            • Login

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