Advertisement
captainIBM

fenetreprincipale

Dec 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "fenetreprincipale.h"
  2. #include "ui_fenetreprincipale.h"
  3. #include "string.h"
  4. #include <vector>
  5. #include <ctype.h>
  6. #include <iostream>
  7. #include <bitset>
  8. #include <sstream>
  9. #include <QApplication>
  10. string file_way;
  11. string matrice_way;
  12. std::string intermed1;
  13. std::string intermed2;
  14. //string file_size;
  15. string file_content;
  16. int file_size = 4;
  17. int indice_matriciel[4];
  18.  
  19. MaFenetre::MaFenetre() : QWidget()
  20. {
  21.     setFixedSize(400, 150);
  22.  
  23.     m_chargerFichier = new QPushButton("Charger un fichier", this);
  24.     m_chargerFichier->move(20, 20);
  25.  
  26.     m_chargerMatrice = new QPushButton("Charger une matrice", this);
  27.     m_chargerMatrice->move(20, 80);
  28.  
  29.     m_cryptage = new QPushButton("Crypter le fichier", this);
  30.     m_cryptage->move(250, 20);
  31.  
  32.     m_decryptage = new QPushButton("Decrypter le fichier", this);
  33.     m_decryptage->move(250, 80);
  34.  
  35.  
  36.     QObject::connect(m_chargerFichier, SIGNAL(clicked(bool)), this, SLOT(chargerFichier()));
  37.     QObject::connect(m_chargerMatrice, SIGNAL(clicked(bool)), this, SLOT(chargerMatrice()));
  38.     QObject::connect(m_cryptage, SIGNAL(clicked(bool)), this, SLOT(cryptage()));
  39.     QObject::connect(m_decryptage, SIGNAL(clicked(bool)), this, SLOT(decryptage()));
  40.  
  41. }
  42.  
  43. void MaFenetre::chargerFichier(){
  44.     QString fichier = QFileDialog::getOpenFileName(this, "Ouvrir un fichier", QString(), "Fichier (*.*)");
  45.     file_way = fichier.toStdString();
  46.  
  47.  
  48. }
  49.  
  50.  
  51. void MaFenetre::chargerMatrice(){
  52.     QString matrice = QFileDialog::getOpenFileName(this, "Ouvrir une matrice", QString(), "Fichier(*.txt)");
  53.     matrice_way = matrice.toStdString();
  54.  
  55.  
  56.  
  57. }
  58.  
  59. void MaFenetre::cryptage(){
  60.     QMessageBox::warning(this, "Atention", "Vous allez crypter un fichier !");
  61.     std::string lien_out;
  62.     std::string file_content;
  63.         string lien = file_way;
  64.         lien_out = lien+"c";
  65.         ifstream fichier(lien, ios::binary);
  66.         long size_div;
  67.         fichier.seekg(0,fichier.end);
  68.         long size = fichier.tellg();
  69.         fichier.seekg (0);
  70.         size_div = size / 4;
  71.         char lettre;
  72.         string intermed;
  73.         string first_part;
  74.         string last_part;
  75.         char symbol;
  76.  
  77.         ofstream fichier_out(lien_out, ios::out);
  78.  
  79.         for(int i= 0; i < size-1; i++){
  80.             fichier.get(lettre);
  81.             bitset<8> B(lettre);
  82.             intermed = B.to_string();
  83.  
  84.             first_part = intermed.substr(0,4);
  85.             last_part = intermed.substr(4,8);
  86.  
  87.  
  88.             symbol = crypt(first_part);
  89.             cout << symbol;
  90.             fichier_out.put(symbol);
  91.  
  92.             symbol = crypt(last_part);
  93.         }
  94.         cout << endl;
  95.  
  96. }
  97.  
  98. void MaFenetre::decryptage(){
  99.         char lettre;
  100.         string last_part;
  101.         char lettre_decrypte;
  102.         string lien_in = file_way;
  103.  
  104.  
  105.         string lien_out = lien_in.substr(0, (lien_in.size()-1));
  106.  
  107.         matrice();
  108.  
  109.         ifstream fichier(lien_in, ios::binary);
  110.  
  111.         fichier.seekg(0,fichier.end);
  112.         long size = fichier.tellg();
  113.         fichier.seekg (0);
  114.  
  115.         ofstream fichier_out(lien_out, ios::out);
  116.  
  117.         cout << "Taille du fichier: " << size << endl;
  118.  
  119.         for(int i = 0; i < size/2; i++){
  120.  
  121.             fichier.get(lettre);
  122.             bitset<8> B(lettre);
  123.             intermed1 = B.to_string();
  124.             fichier.get(lettre);
  125.             bitset<8> A(lettre);
  126.             intermed2 = A.to_string();
  127.             lettre_decrypte = decrypt();
  128.             fichier_out.put(lettre_decrypte);
  129.  
  130.         }
  131.  
  132.         fichier.close();
  133.         fichier_out.close();
  134.  
  135.         cout << "decryption terminée" << endl;
  136.  
  137. }
  138.  
  139.  
  140.  
  141. char MaFenetre::crypt(std::string file_content){
  142.         int block = file_size/4;                      // calcul le nombre de block a mettre
  143.         string prepare[4][block];                     // chaine dans laquelle sera stocké les blocks de 4 bits
  144.         int nb_1 = 0;
  145.         int avancement = 0;
  146.         int bit_selectionne = 0;
  147.         string str = "";
  148.  
  149.         // Separation du binaire dans le tableau a 2 dimmension "prepare" par groupe de 4
  150.  
  151.         for(int i = 0; i < block; i++){
  152.             for(int j = 0; j < 4; j++){
  153.                 prepare[j][i] = file_content.substr(avancement,1);
  154.                 avancement++;
  155.             }
  156.         }
  157.  
  158.         // permet de compter le nombre de 1 dans un groupe de 4 pour preparer le tableau de la simplification matriciel
  159.  
  160.         for(int i = 0; i < 4; i++){
  161.             if(prepare[i][bit_selectionne] == "1")
  162.                 nb_1++;
  163.         }
  164.  
  165.         // ------------------------------- RECUPERATION DE LA MATRICE -------------------------------
  166.  
  167.         // ouverture du fichier contenant la matrice
  168.  
  169.         ifstream fichier(matrice_way, ios::in);    // emplacement de la matrice a changer dans le programme avec GUI
  170.  
  171.         string chaine;
  172.         int debut = 5;
  173.  
  174.         // verrification de la bonne ouverture, lecture du fichier avec enregistrement dans la variable chaine puis fermeture
  175.  
  176.         if(fichier){
  177.             getline(fichier, chaine);
  178.             fichier.close();
  179.         }
  180.  
  181.         else{
  182.             cout << "Impossible d'ouvrir le fichier !" << endl;
  183.         }
  184.  
  185.  
  186.         int matrice_size = (chaine.size()-9)/4;
  187.         string matrice[matrice_size][4];
  188.  
  189.         for(int i = 0; i < 4; i++){
  190.             for(int j = 0; j < matrice_size; j++){
  191.                 matrice[j][i] = chaine.substr(debut, 1);
  192.                 debut++;
  193.             }
  194.             debut++;
  195.         }
  196.  
  197.         // ------------------------------- PARTIE CREATION SHORT_MATRICE -------------------------------
  198.  
  199.         string short_matrice[matrice_size][nb_1];
  200.         int colonne = 0;
  201.  
  202.         // Inscription des lignes de la matrice utilile dans le short_matrice
  203.  
  204.         for(int i = 0; i < 4; i++){
  205.             if(file_content[i] == '1'){
  206.                 for(int j = 0; j < matrice_size; j++){
  207.                     short_matrice[j][colonne] = matrice[j][i];
  208.                 }
  209.                 colonne++;
  210.             }
  211.         }
  212.  
  213.         for(int i = 0; i < nb_1; i++){
  214.             for(int j = 0; j < matrice_size; j++){
  215.             }
  216.         }
  217.  
  218.         // ------------------------------- PARTIE DE CRYPTAGE MATRICIEL -------------------------------
  219.  
  220.         int matrice_int[matrice_size][nb_1];
  221.         int et_logique[matrice_size];
  222.         int intermediaire;
  223.         string finnal = "";
  224.  
  225.         // transformation de la matrice de string en int
  226.  
  227.         // c'est la que ca plante
  228.         for(int i = 0; i < nb_1; i++){
  229.             for(int j = 0; j < matrice_size; j++){
  230.                 matrice_int[j][i] = atoi(short_matrice[j][i].c_str());
  231.             }
  232.         }
  233.  
  234.         for(int i = 0; i < nb_1; i++){
  235.             for(int j =0; j < matrice_size; j++){
  236.             }
  237.         }
  238.  
  239.         if(nb_1 == 1){
  240.             for(int i = 0; i < matrice_size; i++){
  241.                 et_logique[i] = matrice_int[i][0];
  242.             }
  243.         }
  244.  
  245.         else{
  246.  
  247.             for(int i = 0; i < matrice_size; i++){
  248.                 intermediaire = matrice_int[i][0] ^ matrice_int[i][1];
  249.                 for(int j = 0; j < nb_1-2; j++){
  250.                     intermediaire = intermediaire ^ matrice_int[i][j+2];
  251.                 }
  252.                 et_logique[i] = intermediaire;
  253.             }
  254.         }
  255.  
  256.         // affichage de la sting et_logique
  257.  
  258.         // transformation du et_logique en string
  259.  
  260.         for(int i = 0; i < matrice_size; i++){
  261.             //cout << et_logique[i];
  262.         }
  263.  
  264.         for(int i = 0; i < matrice_size; i++){
  265.             finnal = finnal + std::to_string(et_logique[i]);
  266.         }
  267.  
  268.  
  269.         std::stringstream sstream(finnal);
  270.         cout << "a = " << finnal << endl;
  271.         while(sstream.good())
  272.         {
  273.             string output;
  274.             std::bitset<8> bits;
  275.             sstream >> bits;
  276.             char symbol = char(bits.to_ulong());
  277.             output += symbol;
  278.  
  279.             cout << "a = " << symbol << endl;
  280.             return symbol;
  281.         }
  282.         return 0;
  283. }
  284.  
  285.  
  286.  
  287.  
  288.  
  289. void MaFenetre::matrice(){
  290.  
  291.     ifstream fichier(matrice_way, ios::in);
  292.  
  293.     string chaine;
  294.     int debut = 5;
  295.  
  296.     if(fichier){
  297.         getline(fichier, chaine);
  298.         fichier.close();
  299.     }
  300.  
  301.     else{
  302.         cout << "Impossible d'ouvrir le fichier !" << endl;
  303.     }
  304.  
  305.  
  306.     int matrice_size = (chaine.size()-9)/4;
  307.     string matrice[matrice_size][4];
  308.  
  309.     for(int i = 0; i < 4; i++){
  310.         for(int j = 0; j < matrice_size; j++){
  311.             matrice[j][i] = chaine.substr(debut, 1);
  312.             debut++;
  313.         }
  314.         debut++;
  315.     }
  316.  
  317.     // ------------------------------- RECUPERATION DE LA BASE MATRICIELLE -------------------------------
  318.  
  319.  
  320.     for(int i = 0; i < matrice_size; i++){
  321.  
  322.         if(matrice[i][0] == "1" && matrice[i][1] == "0" && matrice[i][2] == "0" && matrice[i][3] == "0"){
  323.             indice_matriciel[0] = i;
  324.         }
  325.  
  326.         else if(matrice[i][0] == "0" && matrice[i][1] == "1" && matrice[i][2] == "0" && matrice[i][3] == "0"){
  327.             indice_matriciel[1] = i;
  328.         }
  329.  
  330.         else if(matrice[i][0] == "0" && matrice[i][1] == "0" && matrice[i][2] == "1" && matrice[i][3] == "0"){
  331.             indice_matriciel[2] = i;
  332.         }
  333.  
  334.         else if(matrice[i][0] == "0" && matrice[i][1] == "0" && matrice[i][2] == "0" && matrice[i][3] == "1"){
  335.             indice_matriciel[3] = i;
  336.         }
  337.  
  338.  
  339.     }
  340.  
  341.     for(int i = 0; i < 4; i++){
  342.  
  343.  
  344.     }
  345.  
  346. }
  347.  
  348. char MaFenetre::decrypt(){
  349.  
  350.     string char1;
  351.     string char2;
  352.     string data;
  353.  
  354.     for(int i = 0; i < 4; i++){
  355.         char1 = char1+intermed1[indice_matriciel[i]];
  356.     }
  357.  
  358.     for(int i = 0; i < 4; i++){
  359.         char2 = char2+intermed2[indice_matriciel[i]];
  360.     }
  361.  
  362.     data = char1+char2;
  363.  
  364.     std::stringstream sstream(data);
  365.  
  366.     std::string output;
  367.     while(sstream.good())
  368.     {
  369.         std::bitset<8> bits;
  370.         sstream >> bits;
  371.         char c = char(bits.to_ulong());
  372.         output += c;
  373.         return c;
  374.     }
  375.  
  376.     return 0;
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement