Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

AES + Base64 return unknown symbols



  • -------------------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.



  • 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.



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


  • Lifetime Qt Champion

    Hi,

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



  • @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); ?


Log in to reply