Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <cmath>
- using namespace std;
- //*************************************************//
- //*****TRANSFORME PIN (nombre) EN KEY (tableau)****//
- void pintokey(int pin, int key[6])
- {
- key[0]=(int)pin/1000;
- key[1]=(int)(pin-key[0]*1000)/100;
- key[2]=(int)(pin-key[0]*1000-key[1]*100)/10;
- key[3]=(int)(pin-key[0]*1000-key[1]*100-key[2]*10);
- key[4]=(key[1]*key[3])%75;
- key[5]=((key[0]+key[2])*(key[0]+key[2]))%75;
- }
- //***********************************//
- //********FONCTION DECRYPTAGE********//
- string decrypt(string txt, int key[6])
- {
- string txtout;
- txtout=txt;
- for(int i=0; i<6; i++)
- key[i]%=95; //pas obligatoire, mais de cette manière on est sûr que c'est fait
- // for(int i=0; i<6; i++)
- // key[i]=95-key[i]; // le "complementaire de la clef fais le travaille inverse du cryptage avec cette même clef
- for(int i=0; i<txt.size(); i++)
- {
- txtout[i]=(txt[i]-32-key[i%6])%95+32;
- }
- cout << txtout << endl;
- return txtout;
- }
- //*****************************//
- //****VERIFICATION DES MOTS****//
- double verif(string txt, string dico)
- {
- int pos_start,pos_end,wordlength,word,wordok,txtlength;
- word=0;
- wordok=0;
- pos_start=0;
- pos_end=0;
- wordlength=0;
- txtlength =txt.length();
- string mot;
- cout << "Le texte fait " << txtlength << " caracteres." << endl;
- txt.insert(txtlength," "); //on rajoute un espace à la fin, car c'est ce qu'on cherche, ce qui sépare les mots
- while(pos_end<=txtlength )
- {
- pos_end=txt.find(" ",pos_start); //on défini la position de fin du mot par un espace
- wordlength=pos_end-pos_start; //la longueur du mot qui va nous servir pour insérer le mot trouvé dans un string
- //for(wordlength=0; txt[pos_start+wordlength]!=' ' && txt[pos_start+wordlength]!=0; wordlength++);
- // mot=txt.substr(pos_start, wordlength);
- if(txt[pos_end-1]==',' || txt[pos_end-1]=='.')
- wordlength--;
- if(txt[pos_start+1]==39)//si apostrophe, aller voir 2 caract. plus loin (début du vrai mot)
- pos_start+=2;
- if(wordlength>=3)//on ne considère que le smots de 3+ lettres
- {
- word++;
- mot= txt.substr(pos_start,wordlength);
- //cout << "Un mot est" << mot << endl;
- if(mot[0]<=90 && mot[0]>=65) //on transforme le premier mot trouvé en miniscule (car le dico n'est composé que de mot en minuscules
- mot[0]+=32;
- if(dico.find(mot,0)!=-1)
- wordok++;
- //cout << "Il y a " << wordok << " bons mots sur "<< word << " mots." << endl;
- }
- pos_end++;
- pos_start=pos_end; //à la prochaine itération, on commence à cherche à la case d'après
- }
- double fiab=0;
- fiab=(double)wordok/(double)word;
- cout << "Fiabilite: " << fiab <<". (il y a " << word << " mot dont "<<wordok << " correct(s)."<< endl;
- return fiab;
- }
- //***************************//
- //*****MAIN DU PROGRAMME*****//
- int main()
- {
- string txtcrypt,txttemp,dico,dicotemp,txtout;
- int pin=0;
- int key[6]= {0};
- double fiab;
- ifstream txt("message_pin_4962.txt");
- if(txt.is_open())
- {
- cout << "Le fichier crypte a bien ete ouvert." << endl;
- while(txt.good())
- {
- getline(txt,txttemp); //on récupère le message crypé et on l'insère dans un string
- txtcrypt+=txttemp;
- }
- cout << "Le message crypte est :" << endl;
- cout << txtcrypt << endl;
- ifstream dictionnaire("dictionnaire.txt");
- if(dictionnaire.is_open()) //chargement du duco dans un string
- {
- cout << "Le dictionnaire a bien ete ouvert." << endl;
- while(dictionnaire.good())
- {
- getline(dictionnaire,dicotemp);
- dico+=dicotemp;
- }
- dictionnaire.close();
- }
- else
- cout << "Le dictionnaire n'a pas pu etre ouvert." << endl;
- }
- else
- cout << "Erreur d'ouverture du fichier crypte." << endl;
- do
- {
- pintokey(pin,key);
- cout << key[0]<<'\t'<<key[1]<<'\t'<<key[2]<<'\t'<<key[3]<<'\t'<<key[4]<<'\t'<<key[5]<< endl;
- txtout = decrypt(txtcrypt,key);
- cout << "Decryptage termine." << endl;
- fiab = verif(txtout,dico);
- cout << "Calcul de fiabilite effectue." << endl;
- txtout.clear();
- pin++;
- }
- while(fiab<0.5 && pin<10000);
- if(verif(txtcrypt,dico)<0.5)
- cout << "Erreur, aucun PIN adéquat n'a pu etre trouve." << endl;
- if(verif(txtcrypt,dico)>0.5)
- {
- cout << "Texte decrypte. Le code pin correspondant est " << pin << " et le texte decrypte est:" << endl;
- cout << txt << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement