Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. STL files into readable text
Forum Updated to NodeBB v4.3 + New Features

STL files into readable text

Scheduled Pinned Locked Moved Unsolved Brainstorm
25 Posts 5 Posters 5.4k 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.
  • M Offline
    M Offline
    Mani_kandan
    wrote on last edited by
    #1

    I have problem in reading the stl files for encryption and decryption , while reading the stl files it is already in ASCII binary text so while encrypting it is becoming a problem , so I need a solution How to convert the stl file into readable text or stl file encrypting and decrypting .

    jsulmJ 1 Reply Last reply
    0
    • M Mani_kandan

      I have problem in reading the stl files for encryption and decryption , while reading the stl files it is already in ASCII binary text so while encrypting it is becoming a problem , so I need a solution How to convert the stl file into readable text or stl file encrypting and decrypting .

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mani_kandan said in STL files into readable text:

      ASCII binary text

      What is ASCII binary text? Text is not binary.
      You also do not need text to encrypt, binary data can also be encrypted.
      How do you do encryption?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mani_kandan
        wrote on last edited by
        #3

        I need to encrypt the stl file, previously I encrypted the .obj file the steps I followed is 1st I will read the .obj file and encrypt the contents and store it as file then I will read the encrypted file and decrypt the contents and store it file and I will get the same contents before encrypting . I was following the same steps for .stl file to but couldn't able to encrypt and decrypt the contents while reading its its unreadable thats the problem.

        jsulmJ 1 Reply Last reply
        0
        • M Mani_kandan

          I need to encrypt the stl file, previously I encrypted the .obj file the steps I followed is 1st I will read the .obj file and encrypt the contents and store it as file then I will read the encrypted file and decrypt the contents and store it file and I will get the same contents before encrypting . I was following the same steps for .stl file to but couldn't able to encrypt and decrypt the contents while reading its its unreadable thats the problem.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Mani_kandan said in STL files into readable text:

          while reading its its unreadable

          In what way unreadable?!
          Are you reading it as text file?
          Simply read it as binary...

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mani_kandan
            wrote on last edited by
            #5

            the code for reading it as binary

            JonBJ 1 Reply Last reply
            0
            • M Mani_kandan

              the code for reading it as binary

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Mani_kandan
              As you can see, we simply do not understand what you are asking/what your problem is. Perhaps trying again to rephrase might make it clearer?

              M 1 Reply Last reply
              0
              • M Offline
                M Offline
                Mani_kandan
                wrote on last edited by
                #7

                In simple words I need to encrypt and decrypt the .stl file , please help me out of this.

                @JonB said in STL files into readable text:

                @Mani_kandan
                As you can see, we simply do not understand what you are asking/what your problem is. Perhaps trying again to rephrase might make it clearer?

                In simple word I need to encrypt and decrypt the .stl file

                JonBJ 1 Reply Last reply
                0
                • M Mani_kandan

                  In simple words I need to encrypt and decrypt the .stl file , please help me out of this.

                  @JonB said in STL files into readable text:

                  @Mani_kandan
                  As you can see, we simply do not understand what you are asking/what your problem is. Perhaps trying again to rephrase might make it clearer?

                  In simple word I need to encrypt and decrypt the .stl file

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Mani_kandan
                  That is just repeating the same thing.

                  I don't think there is a Qt class for encrypt/decrypt, so use some C++ library/utility/roll your own for that.

                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @Mani_kandan
                    As you can see, we simply do not understand what you are asking/what your problem is. Perhaps trying again to rephrase might make it clearer?

                    M Offline
                    M Offline
                    Mani_kandan
                    wrote on last edited by
                    #9

                    @JonB Yeah I m using openssl for that encryption and decryption are but I didn't the desired output . But this step Has been working in .obj file . I feel the problem in stl file contains some unreadable text thats why I m not getting the ouput. can stl file can be converted into readable text

                    JonBJ 1 Reply Last reply
                    0
                    • M Mani_kandan

                      @JonB Yeah I m using openssl for that encryption and decryption are but I didn't the desired output . But this step Has been working in .obj file . I feel the problem in stl file contains some unreadable text thats why I m not getting the ouput. can stl file can be converted into readable text

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Mani_kandan said in STL files into readable text:

                      stl file contains some unreadable text

                      I (and I think I can speak for @jsulm too!) do not know what you mean by " some unreadable text". Either a file is readable or it is not. Files do not have "some parts" which are not readable. Also as we have said before, encryption/decryption does not require "text" so don't know what that is about either.

                      can stl file can be converted into readable text

                      Don't know, and don't know what "readable text" has got to do with anything. And STL file/format has nothing to do with Qt, so questions about that might be asked/sought elsewhere.

                      1 Reply Last reply
                      1
                      • M Offline
                        M Offline
                        Mani_kandan
                        wrote on last edited by
                        #11

                        is there any step in qt for encrypting and decrypting the file using openssl

                        raven-worxR 1 Reply Last reply
                        0
                        • M Mani_kandan

                          is there any step in qt for encrypting and decrypting the file using openssl

                          raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by raven-worx
                          #12

                          @Mani_kandan
                          stl are encoded either in ASCII (plain text) or binary format.
                          the binary format is not security encrypted, so OpenSSL wont help you here.

                          you will need to follow the binary format specification and read the binary directly.
                          pseudo code taken from wikipedia:

                          UINT8[80]         -   Header
                          UINT32            -   triangle count
                          foreach triangle
                            REAL32[3]       -    vector
                            REAL32[3]       -    Vertex 1
                            REAL32[3]       -    Vertex 2
                            REAL32[3]       -    Vertex 3
                            UINT16          -    attribute byte count
                          end
                          

                          what is your actual use case? do you want to display the STL file?

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          M 2 Replies Last reply
                          1
                          • raven-worxR raven-worx

                            @Mani_kandan
                            stl are encoded either in ASCII (plain text) or binary format.
                            the binary format is not security encrypted, so OpenSSL wont help you here.

                            you will need to follow the binary format specification and read the binary directly.
                            pseudo code taken from wikipedia:

                            UINT8[80]         -   Header
                            UINT32            -   triangle count
                            foreach triangle
                              REAL32[3]       -    vector
                              REAL32[3]       -    Vertex 1
                              REAL32[3]       -    Vertex 2
                              REAL32[3]       -    Vertex 3
                              UINT16          -    attribute byte count
                            end
                            

                            what is your actual use case? do you want to display the STL file?

                            M Offline
                            M Offline
                            Mani_kandan
                            wrote on last edited by
                            #13

                            @raven-worx thanks @raven-worx but I don't know how to read the binary directly

                            KroMignonK M raven-worxR 3 Replies Last reply
                            0
                            • M Mani_kandan

                              @raven-worx thanks @raven-worx but I don't know how to read the binary directly

                              KroMignonK Offline
                              KroMignonK Offline
                              KroMignon
                              wrote on last edited by
                              #14

                              @Mani_kandan said in STL files into readable text:

                              but I don't know how to read the binary directly

                              You could take a look at this https://github.com/sreiter/stl_reader

                              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                              1 Reply Last reply
                              0
                              • M Mani_kandan

                                @raven-worx thanks @raven-worx but I don't know how to read the binary directly

                                M Offline
                                M Offline
                                Mani_kandan
                                wrote on last edited by
                                #15

                                @Mani_kandan code for ascii to string conversion in qt

                                1 Reply Last reply
                                0
                                • M Mani_kandan

                                  @raven-worx thanks @raven-worx but I don't know how to read the binary directly

                                  raven-worxR Offline
                                  raven-worxR Offline
                                  raven-worx
                                  Moderators
                                  wrote on last edited by
                                  #16

                                  @Mani_kandan
                                  to me it looks like you are missing some basic fundamentals in programming/C++/Qt
                                  and therefore IMO this task seems a bit too much to begin with

                                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                                  If you have a question please use the forum so others can benefit from the solution in the future

                                  1 Reply Last reply
                                  1
                                  • raven-worxR raven-worx

                                    @Mani_kandan
                                    stl are encoded either in ASCII (plain text) or binary format.
                                    the binary format is not security encrypted, so OpenSSL wont help you here.

                                    you will need to follow the binary format specification and read the binary directly.
                                    pseudo code taken from wikipedia:

                                    UINT8[80]         -   Header
                                    UINT32            -   triangle count
                                    foreach triangle
                                      REAL32[3]       -    vector
                                      REAL32[3]       -    Vertex 1
                                      REAL32[3]       -    Vertex 2
                                      REAL32[3]       -    Vertex 3
                                      UINT16          -    attribute byte count
                                    end
                                    

                                    what is your actual use case? do you want to display the STL file?

                                    M Offline
                                    M Offline
                                    Mani_kandan
                                    wrote on last edited by
                                    #17

                                    @raven-worx any one knows how to encrypt and decrypt the file in qt I m using openssl library for that

                                    M 1 Reply Last reply
                                    0
                                    • M Mani_kandan

                                      @raven-worx any one knows how to encrypt and decrypt the file in qt I m using openssl library for that

                                      M Offline
                                      M Offline
                                      Mani_kandan
                                      wrote on last edited by
                                      #18

                                      mainwindow.h

                                      #ifndef MAINWINDOW_H
                                      #define MAINWINDOW_H
                                      
                                      #include <QMainWindow>
                                      #include <QTextEdit>
                                      #include <QString>
                                      #include <QDebug>
                                      #include <QFile>
                                      #include <QTextStream>
                                      #include <QLabel>
                                      #include <QFileDialog>
                                      #include <QMessageBox>
                                      
                                      // OpenSSL
                                      #include <openssl/rsa.h>
                                      #include <openssl/engine.h>
                                      #include <openssl/aes.h>
                                      #include <openssl/pem.h>
                                      #include <openssl/conf.h>
                                      #include <openssl/evp.h>
                                      #include <openssl/err.h>
                                      #include <openssl/bio.h>
                                      #include <openssl/rand.h>
                                      #include <openssl/randerr.h>
                                      
                                      #define PADDING RSA_PKCS1_PADDING
                                      #define KEYSIZE 32
                                      #define IVSIZE 32
                                      #define BLOCKSIZE 256
                                      #define SALTSIZE 8
                                      
                                      
                                      QT_BEGIN_NAMESPACE
                                      namespace Ui { class MainWindow; }
                                      QT_END_NAMESPACE
                                      
                                      class MainWindow : public QMainWindow
                                      {
                                          Q_OBJECT
                                      
                                      public:
                                          MainWindow(QWidget *parent = nullptr);
                                          ~MainWindow();
                                      
                                          /**
                                           * @brief Encrypt a byte array with AES 256 CBC
                                           * @param data The byte array to encrypt
                                           * @return QByteArray
                                           */
                                          QByteArray encryptAES(QByteArray passphrase, QByteArray &data);
                                      
                                          /**
                                           * @brief Decrypt a byte array with AES 256 CBC
                                           * @param data The byte array to decrypt
                                           * @return QByteArray
                                           */
                                          QByteArray decryptAES(QByteArray passphrase, QByteArray &data);
                                      
                                          /**
                                           * @brief Get a byte array filled with random information
                                           * @param size The size of the byte array to generate
                                           * @return QByteArray
                                           */
                                          QByteArray randomBytes(int size);
                                      
                                      private slots:
                                      
                                          void on_encrypt_clicked();
                                      
                                          void on_save_enc_clicked();
                                      
                                          void on_decrypt_clicked();
                                      
                                          void on_save_dec_clicked();
                                      
                                          void on_selecting_clicked();
                                      
                                      private:
                                          Ui::MainWindow *ui;
                                      
                                          QByteArray encrypted;
                                          QString passphrase = "password";
                                      
                                      };
                                      #endif // MAINWINDOW_H
                                      
                                      
                                      .**CPP**
                                      
                                      #include "mainwindow.h"
                                      #include "./ui_mainwindow.h"
                                      #include "cipher.h"
                                      
                                      
                                      MainWindow::MainWindow(QWidget *parent)
                                          : QMainWindow(parent)
                                          , ui(new Ui::MainWindow)
                                      {
                                          ui->setupUi(this);
                                      }
                                      
                                      MainWindow::~MainWindow()
                                      {
                                          delete ui;
                                      }
                                      QByteArray MainWindow::encryptAES(QByteArray passphrase, QByteArray &data)
                                      {
                                          QByteArray msalt = randomBytes(SALTSIZE);
                                          int rounds = 1;
                                          unsigned char key[KEYSIZE];
                                          unsigned char iv[IVSIZE];
                                      
                                          const unsigned char* salt = (const unsigned char*) msalt.constData();
                                          const unsigned char* password = (const unsigned char*) passphrase.constData();
                                      
                                          int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt,password, passphrase.length(),rounds,key,iv);
                                      
                                          if(i != KEYSIZE)
                                          {
                                              qCritical() << "EVP_BytesToKey() error: " << ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
                                          EVP_CIPHER_CTX_init(en);
                                      
                                          if(!EVP_EncryptInit_ex(en, EVP_aes_256_cbc(),NULL,key, iv))
                                          {
                                              qCritical() << "EVP_EncryptInit_ex() failed " << ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          char *input = data.data();
                                          int len = data.size();
                                      
                                          int c_len = len + AES_BLOCK_SIZE, f_len = 0;
                                          unsigned char *ciphertext = (unsigned char*)malloc(c_len);
                                      
                                          if(!EVP_EncryptInit_ex(en, NULL, NULL, NULL, NULL))
                                          {
                                              qCritical() << "EVP_EncryptInit_ex() failed " << ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          // May have to repeat this for large files
                                      
                                          if(!EVP_EncryptUpdate(en, ciphertext, &c_len,(unsigned char *)input, len))
                                          {
                                              qCritical() << "EVP_EncryptUpdate() failed " << ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          if(!EVP_EncryptFinal(en, ciphertext+c_len, &f_len))
                                          {
                                              qCritical() << "EVP_EncryptFinal_ex() failed "  << ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          len = c_len + f_len;
                                          EVP_CIPHER_CTX_cipher(en);
                                      
                                          //ciphertext
                                      
                                          QByteArray encrypted = QByteArray(reinterpret_cast<char*>(ciphertext), len);
                                          QByteArray finished;
                                          finished.append("Salted__");
                                          finished.append(msalt);
                                          finished.append(encrypted);
                                      
                                          free(ciphertext);
                                      
                                          return finished;
                                      }
                                      
                                      QByteArray MainWindow::decryptAES(QByteArray passphrase, QByteArray &data)
                                      {
                                          QByteArray msalt;
                                          if(QString(data.mid(0,8)) == "Salted__")
                                          {
                                              msalt = data.mid(8,8);
                                              data = data.mid(16);
                                          }
                                          else
                                          {
                                              qWarning() << "Could not load salt from data!";
                                              msalt = randomBytes(SALTSIZE);
                                          }
                                      
                                          int rounds = 1;
                                          unsigned char key[KEYSIZE];
                                          unsigned char iv[IVSIZE];
                                          const unsigned char* salt = (const unsigned char*)msalt.constData();
                                          const unsigned char* password = (const unsigned char*)passphrase.data();
                                      
                                          int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt,password, passphrase.length(),rounds,key,iv);
                                      
                                          if(i != KEYSIZE)
                                          {
                                              qCritical() << "EVP_BytesToKey() error: " << ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          EVP_CIPHER_CTX *de=EVP_CIPHER_CTX_new();
                                          EVP_CIPHER_CTX_init(de);
                                      
                                          if(!EVP_DecryptInit_ex(de,EVP_aes_256_cbc(), NULL, key,iv ))
                                          {
                                              qCritical() << "EVP_DecryptInit_ex() failed" << ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          char *input = data.data();
                                          int len = data.size();
                                      
                                          int p_len = len, f_len = 0;
                                          unsigned char *plaintext = (unsigned char *)malloc(p_len + AES_BLOCK_SIZE);
                                      
                                          //May have to do this multiple times for large data???
                                          if(!EVP_DecryptUpdate(de, plaintext, &p_len, (unsigned char *)input, len))
                                          {
                                              qCritical() << "EVP_DecryptUpdate() failed " <<  ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          if(!EVP_DecryptFinal_ex(de,plaintext+p_len,&f_len))
                                          {
                                              qCritical() << "EVP_DecryptFinal_ex() failed " <<  ERR_error_string(ERR_get_error(), NULL);
                                              return QByteArray();
                                          }
                                      
                                          len = p_len + f_len;
                                      
                                          EVP_CIPHER_CTX_cleanup(de);
                                      
                                      
                                          QByteArray decrypted = QByteArray(reinterpret_cast<char*>(plaintext), len);
                                          free(plaintext);
                                      
                                          return decrypted;
                                      
                                      }
                                      
                                      QByteArray MainWindow::randomBytes(int size)
                                      {
                                      
                                          unsigned char *arr= new unsigned char[size];
                                          RAND_bytes(arr,size);
                                          // int arr[size];
                                          //int RAND_bytes(unsigned char *buf,int num);
                                      
                                          QByteArray buffer = QByteArray(reinterpret_cast<char*>(arr), size);
                                          return buffer;
                                      }
                                      void MainWindow::on_selecting_clicked()
                                      {
                                          QString filename=QFileDialog::getOpenFileName(this,"Open a file","F:/SoftwareTeam/Otherfiles/Manikandan/E&D_File");
                                          QFile file(filename);
                                      
                                          if(!file.open(QFile::ReadOnly | QFile::Text))
                                          {
                                              QMessageBox::warning(this,"title","file not open");
                                      
                                          }
                                          QTextStream in(&file);
                                          QString text = in.readAll();
                                          qDebug()<<text;
                                          ui->read_txt->setPlainText(text);
                                      
                                      
                                          file.close();
                                      }
                                      
                                      void MainWindow::on_encrypt_clicked()
                                      {
                                          qDebug() << "Testing AES...";
                                      
                                          Cipher cWrapper;
                                      
                                      
                                          QByteArray plain = ui->read_txt->toPlainText().toLocal8Bit();
                                          encrypted = cWrapper.encryptAES(passphrase.toLatin1(),plain);
                                          ui->read_txt->setPlainText(encrypted);
                                          qDebug()<<"The plain text is:";
                                          qDebug() << plain;
                                          qDebug()<<"The text is encrypted";
                                          qDebug() << encrypted;
                                      
                                      
                                      }
                                      
                                      void MainWindow::on_save_enc_clicked()
                                      {
                                          QString filename = QFileDialog::getSaveFileName(this,"Save as","F:/SoftwareTeam/Otherfiles/Manikandan/E&D_File");
                                          if(filename.isEmpty())
                                              return;
                                      
                                          QFile file(filename);
                                      
                                          if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
                                              return;
                                      
                                          QTextStream out(&file);
                                          out<< ui->read_txt->toPlainText()<<"\n";
                                      
                                          file.close();
                                      
                                      }
                                      
                                      
                                      void MainWindow::on_decrypt_clicked()
                                      {
                                      
                                          qDebug() << "Decrypting AES:";
                                          Cipher cWrapper;
                                          QByteArray decrypted = cWrapper.decryptAES(passphrase.toLatin1(),encrypted);
                                          qDebug()<<"The text is decrypted";
                                          qDebug() << decrypted;
                                           ui->read_txt->setText(decrypted);
                                      
                                      
                                      }
                                      
                                      
                                      void MainWindow::on_save_dec_clicked()
                                      {
                                          //QString filter= "tr("Open 3D Object"),"",tr("3D Object(*.obj);;All files(*)");
                                          //QString filename = QFileDialog::getSaveFileName(this,"Save as","F:/SoftwareTeam/Otherfiles/Manikandan/E&D_File/objdec.obj/");
                                          QString filename = QFileDialog::getSaveFileName(this,
                                                  tr("Save As"), "",
                                                  tr("3D Object (*.obj);;All Files (*)"));
                                                  //tr("DCM File (*.dcm);;All Files (*)"));
                                                  //tr("STL File (*.stl);;All Files (*)"));
                                          if(filename.isEmpty())
                                              return;
                                      
                                          QFile file(filename);
                                      
                                          if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
                                              return;
                                      
                                          QTextStream out(&file);
                                          out<< ui->read_txt->toPlainText()<<"\n";
                                      
                                          file.close();
                                      
                                      
                                      }
                                      M 1 Reply Last reply
                                      0
                                      • M Mani_kandan

                                        mainwindow.h

                                        #ifndef MAINWINDOW_H
                                        #define MAINWINDOW_H
                                        
                                        #include <QMainWindow>
                                        #include <QTextEdit>
                                        #include <QString>
                                        #include <QDebug>
                                        #include <QFile>
                                        #include <QTextStream>
                                        #include <QLabel>
                                        #include <QFileDialog>
                                        #include <QMessageBox>
                                        
                                        // OpenSSL
                                        #include <openssl/rsa.h>
                                        #include <openssl/engine.h>
                                        #include <openssl/aes.h>
                                        #include <openssl/pem.h>
                                        #include <openssl/conf.h>
                                        #include <openssl/evp.h>
                                        #include <openssl/err.h>
                                        #include <openssl/bio.h>
                                        #include <openssl/rand.h>
                                        #include <openssl/randerr.h>
                                        
                                        #define PADDING RSA_PKCS1_PADDING
                                        #define KEYSIZE 32
                                        #define IVSIZE 32
                                        #define BLOCKSIZE 256
                                        #define SALTSIZE 8
                                        
                                        
                                        QT_BEGIN_NAMESPACE
                                        namespace Ui { class MainWindow; }
                                        QT_END_NAMESPACE
                                        
                                        class MainWindow : public QMainWindow
                                        {
                                            Q_OBJECT
                                        
                                        public:
                                            MainWindow(QWidget *parent = nullptr);
                                            ~MainWindow();
                                        
                                            /**
                                             * @brief Encrypt a byte array with AES 256 CBC
                                             * @param data The byte array to encrypt
                                             * @return QByteArray
                                             */
                                            QByteArray encryptAES(QByteArray passphrase, QByteArray &data);
                                        
                                            /**
                                             * @brief Decrypt a byte array with AES 256 CBC
                                             * @param data The byte array to decrypt
                                             * @return QByteArray
                                             */
                                            QByteArray decryptAES(QByteArray passphrase, QByteArray &data);
                                        
                                            /**
                                             * @brief Get a byte array filled with random information
                                             * @param size The size of the byte array to generate
                                             * @return QByteArray
                                             */
                                            QByteArray randomBytes(int size);
                                        
                                        private slots:
                                        
                                            void on_encrypt_clicked();
                                        
                                            void on_save_enc_clicked();
                                        
                                            void on_decrypt_clicked();
                                        
                                            void on_save_dec_clicked();
                                        
                                            void on_selecting_clicked();
                                        
                                        private:
                                            Ui::MainWindow *ui;
                                        
                                            QByteArray encrypted;
                                            QString passphrase = "password";
                                        
                                        };
                                        #endif // MAINWINDOW_H
                                        
                                        
                                        .**CPP**
                                        
                                        #include "mainwindow.h"
                                        #include "./ui_mainwindow.h"
                                        #include "cipher.h"
                                        
                                        
                                        MainWindow::MainWindow(QWidget *parent)
                                            : QMainWindow(parent)
                                            , ui(new Ui::MainWindow)
                                        {
                                            ui->setupUi(this);
                                        }
                                        
                                        MainWindow::~MainWindow()
                                        {
                                            delete ui;
                                        }
                                        QByteArray MainWindow::encryptAES(QByteArray passphrase, QByteArray &data)
                                        {
                                            QByteArray msalt = randomBytes(SALTSIZE);
                                            int rounds = 1;
                                            unsigned char key[KEYSIZE];
                                            unsigned char iv[IVSIZE];
                                        
                                            const unsigned char* salt = (const unsigned char*) msalt.constData();
                                            const unsigned char* password = (const unsigned char*) passphrase.constData();
                                        
                                            int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt,password, passphrase.length(),rounds,key,iv);
                                        
                                            if(i != KEYSIZE)
                                            {
                                                qCritical() << "EVP_BytesToKey() error: " << ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
                                            EVP_CIPHER_CTX_init(en);
                                        
                                            if(!EVP_EncryptInit_ex(en, EVP_aes_256_cbc(),NULL,key, iv))
                                            {
                                                qCritical() << "EVP_EncryptInit_ex() failed " << ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            char *input = data.data();
                                            int len = data.size();
                                        
                                            int c_len = len + AES_BLOCK_SIZE, f_len = 0;
                                            unsigned char *ciphertext = (unsigned char*)malloc(c_len);
                                        
                                            if(!EVP_EncryptInit_ex(en, NULL, NULL, NULL, NULL))
                                            {
                                                qCritical() << "EVP_EncryptInit_ex() failed " << ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            // May have to repeat this for large files
                                        
                                            if(!EVP_EncryptUpdate(en, ciphertext, &c_len,(unsigned char *)input, len))
                                            {
                                                qCritical() << "EVP_EncryptUpdate() failed " << ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            if(!EVP_EncryptFinal(en, ciphertext+c_len, &f_len))
                                            {
                                                qCritical() << "EVP_EncryptFinal_ex() failed "  << ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            len = c_len + f_len;
                                            EVP_CIPHER_CTX_cipher(en);
                                        
                                            //ciphertext
                                        
                                            QByteArray encrypted = QByteArray(reinterpret_cast<char*>(ciphertext), len);
                                            QByteArray finished;
                                            finished.append("Salted__");
                                            finished.append(msalt);
                                            finished.append(encrypted);
                                        
                                            free(ciphertext);
                                        
                                            return finished;
                                        }
                                        
                                        QByteArray MainWindow::decryptAES(QByteArray passphrase, QByteArray &data)
                                        {
                                            QByteArray msalt;
                                            if(QString(data.mid(0,8)) == "Salted__")
                                            {
                                                msalt = data.mid(8,8);
                                                data = data.mid(16);
                                            }
                                            else
                                            {
                                                qWarning() << "Could not load salt from data!";
                                                msalt = randomBytes(SALTSIZE);
                                            }
                                        
                                            int rounds = 1;
                                            unsigned char key[KEYSIZE];
                                            unsigned char iv[IVSIZE];
                                            const unsigned char* salt = (const unsigned char*)msalt.constData();
                                            const unsigned char* password = (const unsigned char*)passphrase.data();
                                        
                                            int i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), salt,password, passphrase.length(),rounds,key,iv);
                                        
                                            if(i != KEYSIZE)
                                            {
                                                qCritical() << "EVP_BytesToKey() error: " << ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            EVP_CIPHER_CTX *de=EVP_CIPHER_CTX_new();
                                            EVP_CIPHER_CTX_init(de);
                                        
                                            if(!EVP_DecryptInit_ex(de,EVP_aes_256_cbc(), NULL, key,iv ))
                                            {
                                                qCritical() << "EVP_DecryptInit_ex() failed" << ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            char *input = data.data();
                                            int len = data.size();
                                        
                                            int p_len = len, f_len = 0;
                                            unsigned char *plaintext = (unsigned char *)malloc(p_len + AES_BLOCK_SIZE);
                                        
                                            //May have to do this multiple times for large data???
                                            if(!EVP_DecryptUpdate(de, plaintext, &p_len, (unsigned char *)input, len))
                                            {
                                                qCritical() << "EVP_DecryptUpdate() failed " <<  ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            if(!EVP_DecryptFinal_ex(de,plaintext+p_len,&f_len))
                                            {
                                                qCritical() << "EVP_DecryptFinal_ex() failed " <<  ERR_error_string(ERR_get_error(), NULL);
                                                return QByteArray();
                                            }
                                        
                                            len = p_len + f_len;
                                        
                                            EVP_CIPHER_CTX_cleanup(de);
                                        
                                        
                                            QByteArray decrypted = QByteArray(reinterpret_cast<char*>(plaintext), len);
                                            free(plaintext);
                                        
                                            return decrypted;
                                        
                                        }
                                        
                                        QByteArray MainWindow::randomBytes(int size)
                                        {
                                        
                                            unsigned char *arr= new unsigned char[size];
                                            RAND_bytes(arr,size);
                                            // int arr[size];
                                            //int RAND_bytes(unsigned char *buf,int num);
                                        
                                            QByteArray buffer = QByteArray(reinterpret_cast<char*>(arr), size);
                                            return buffer;
                                        }
                                        void MainWindow::on_selecting_clicked()
                                        {
                                            QString filename=QFileDialog::getOpenFileName(this,"Open a file","F:/SoftwareTeam/Otherfiles/Manikandan/E&D_File");
                                            QFile file(filename);
                                        
                                            if(!file.open(QFile::ReadOnly | QFile::Text))
                                            {
                                                QMessageBox::warning(this,"title","file not open");
                                        
                                            }
                                            QTextStream in(&file);
                                            QString text = in.readAll();
                                            qDebug()<<text;
                                            ui->read_txt->setPlainText(text);
                                        
                                        
                                            file.close();
                                        }
                                        
                                        void MainWindow::on_encrypt_clicked()
                                        {
                                            qDebug() << "Testing AES...";
                                        
                                            Cipher cWrapper;
                                        
                                        
                                            QByteArray plain = ui->read_txt->toPlainText().toLocal8Bit();
                                            encrypted = cWrapper.encryptAES(passphrase.toLatin1(),plain);
                                            ui->read_txt->setPlainText(encrypted);
                                            qDebug()<<"The plain text is:";
                                            qDebug() << plain;
                                            qDebug()<<"The text is encrypted";
                                            qDebug() << encrypted;
                                        
                                        
                                        }
                                        
                                        void MainWindow::on_save_enc_clicked()
                                        {
                                            QString filename = QFileDialog::getSaveFileName(this,"Save as","F:/SoftwareTeam/Otherfiles/Manikandan/E&D_File");
                                            if(filename.isEmpty())
                                                return;
                                        
                                            QFile file(filename);
                                        
                                            if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
                                                return;
                                        
                                            QTextStream out(&file);
                                            out<< ui->read_txt->toPlainText()<<"\n";
                                        
                                            file.close();
                                        
                                        }
                                        
                                        
                                        void MainWindow::on_decrypt_clicked()
                                        {
                                        
                                            qDebug() << "Decrypting AES:";
                                            Cipher cWrapper;
                                            QByteArray decrypted = cWrapper.decryptAES(passphrase.toLatin1(),encrypted);
                                            qDebug()<<"The text is decrypted";
                                            qDebug() << decrypted;
                                             ui->read_txt->setText(decrypted);
                                        
                                        
                                        }
                                        
                                        
                                        void MainWindow::on_save_dec_clicked()
                                        {
                                            //QString filter= "tr("Open 3D Object"),"",tr("3D Object(*.obj);;All files(*)");
                                            //QString filename = QFileDialog::getSaveFileName(this,"Save as","F:/SoftwareTeam/Otherfiles/Manikandan/E&D_File/objdec.obj/");
                                            QString filename = QFileDialog::getSaveFileName(this,
                                                    tr("Save As"), "",
                                                    tr("3D Object (*.obj);;All Files (*)"));
                                                    //tr("DCM File (*.dcm);;All Files (*)"));
                                                    //tr("STL File (*.stl);;All Files (*)"));
                                            if(filename.isEmpty())
                                                return;
                                        
                                            QFile file(filename);
                                        
                                            if(!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
                                                return;
                                        
                                            QTextStream out(&file);
                                            out<< ui->read_txt->toPlainText()<<"\n";
                                        
                                            file.close();
                                        
                                        
                                        }
                                        M Offline
                                        M Offline
                                        Mani_kandan
                                        wrote on last edited by
                                        #19

                                        @Mani_kandan in this code i m reading the file and encrypting the contents . I need a solution encrypt and decrypt the complete file without reading

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • M Mani_kandan

                                          @Mani_kandan in this code i m reading the file and encrypting the contents . I need a solution encrypt and decrypt the complete file without reading

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @Mani_kandan said in STL files into readable text:

                                          I need a solution encrypt and decrypt the complete file without reading

                                          What? How do you want to encrypt/decrypt a file without reading it?
                                          Can you please try to explain better when asking?

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          M 1 Reply Last reply
                                          1

                                          • Login

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