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
    #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
                                  • jsulmJ jsulm

                                    @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?

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

                                    @jsulm in the above code i m reading the contents inside the file and doing encryption and decryption. I have to know how to encrypt a complete and store its as encrypted file and decrypt the encrypted file .

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • M Mani_kandan

                                      @jsulm in the above code i m reading the contents inside the file and doing encryption and decryption. I have to know how to encrypt a complete and store its as encrypted file and decrypt the encrypted file .

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

                                      @Mani_kandan I really fail to understand what exact problem you have!
                                      You are already reading whole file (though as text which is wrong for binary files).
                                      You also write a file already.
                                      So what EXACT problem/question do you have?

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

                                      M 1 Reply Last reply
                                      1
                                      • jsulmJ jsulm

                                        @Mani_kandan I really fail to understand what exact problem you have!
                                        You are already reading whole file (though as text which is wrong for binary files).
                                        You also write a file already.
                                        So what EXACT problem/question do you have?

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

                                        @jsulm I don't know how to explain it, but I need to encrypt the file as a file

                                        jsulmJ JonBJ 2 Replies Last reply
                                        0
                                        • M Mani_kandan

                                          @jsulm I don't know how to explain it, but I need to encrypt the file as a file

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

                                          @Mani_kandan Sorry I give up as I have no idea what the problem is!
                                          You are already reading the file, encrypting its content and writing encrypted content into another file.
                                          No idea what you are asking...

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

                                          1 Reply Last reply
                                          2

                                          • Login

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