TheMan68
TheMan68

Reputation: 1469

QML and RSA Encryption

Hi I'm trying to find the best way to generate public and private keys using only js and qml. This would also involve encrypting and decryption of messages between peer to peer.

I've found this library: https://github.com/travist/jsencrypt/blob/master/bin/jsencrypt.js

But the problem with using libraries that were designed for a browser is that I never seem to be able to figure them out with the

import filePath as JsRASCrypto

Does anyone have any recommendations on how exactly I could get a library like this one working or any other ways that I could achieve this?

Thank you

Upvotes: 0

Views: 1160

Answers (1)

mike510a
mike510a

Reputation: 2168

Fairly easy to implement this by downloading https://github.com/bricke/Qt-AES and adding the files qaesencryption.h and qaesencryption.cpp into your project, then create a controller class which can access the QAESEncryption methods and make a few Q_INVOKABLE typed methods to handle the encryption then expose the AES controller class to your QML engine

no problem

aes.h

#ifndef AES_H
#define AES_H

#include <QObject>

class AES : public QObject
{
    Q_OBJECT
public:
    explicit AES(QObject *parent = nullptr);
    Q_INVOKABLE QVariant encrypt(QString plainText, QString key);


    Q_INVOKABLE QVariant decrypt(QString encodedText, QString key);
signals:

public slots:
};

#endif // AES_H

aes.cpp

#include "aes.h"
#include "qaesencryption.h"

AES::AES(QObject *parent) : QObject(parent)
{

}

QVariant AES::encrypt(QString plainText, QString key)
{
  QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
  QByteArray encodedText = encryption.encode(plainText, key);
  return QVariant::fromValue(encodedText);
}

QVariant AES::decrypt(QString encodedText, QString key)
{
  QAESEncryption encryption(QAESEncryption::AES_128, QAESEncryption::ECB);
  QByteArray decodedText = encryption.decode(encodedText, key);
  return QVariant::fromValue(decodedText);
}

----

main.cpp

Heres where to register QML type

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "aes.h"
int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    
    
    /* register AES class into QML */
    qmlRegisterType<AES>("com.myapp.demo", 1, 0, "AES");
    
    
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

----
main.qml --
import QtQuick 2.10
import QtQuick.Window 2.10
import com.myapp.demo 1.0

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    /* make an instance of AES */
    AES {
        id: aes
    }
   
    /* call invokable method */
    Component.onCompleted: {
        alert(aes.encrypt("testing encryption", "secretKey"))
    }
}

Hope this helps someone out there figure things out

Upvotes: 2

Related Questions