STL files into readable text
-
@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... -
the code for reading it as binary
-
@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 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
-
@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.
-
@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
-
@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.
-
is there any step in qt for encrypting and decrypting the file using openssl
-
@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?
-
@raven-worx thanks @raven-worx but I don't know how to read the binary directly
-
@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
-
@Mani_kandan code for ascii to string conversion in qt
-
@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 -
@raven-worx any one knows how to encrypt and decrypt the file in qt I m using openssl library for that
-
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(); }
-
@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
-
@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? -
@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?