Advertisement
HappyBlue

ProjectInfo2016

Apr 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7. //*************************************************//
  8. //*****TRANSFORME PIN (nombre) EN KEY (tableau)****//
  9. void pintokey(int pin, int key[6])
  10. {
  11. key[0]=(int)pin/1000;
  12. key[1]=(int)(pin-key[0]*1000)/100;
  13. key[2]=(int)(pin-key[0]*1000-key[1]*100)/10;
  14. key[3]=(int)(pin-key[0]*1000-key[1]*100-key[2]*10);
  15. key[4]=(key[1]*key[3])%75;
  16. key[5]=((key[0]+key[2])*(key[0]+key[2]))%75;
  17. }
  18.  
  19. //***********************************//
  20. //********FONCTION DECRYPTAGE********//
  21. string decrypt(string txt, int key[6])
  22. {
  23. string txtout;
  24. txtout=txt;
  25. for(int i=0; i<6; i++)
  26. key[i]%=95; //pas obligatoire, mais de cette manière on est sûr que c'est fait
  27. // for(int i=0; i<6; i++)
  28. // key[i]=95-key[i]; // le "complementaire de la clef fais le travaille inverse du cryptage avec cette même clef
  29. for(int i=0; i<txt.size(); i++)
  30. {
  31. txtout[i]=(txt[i]-32-key[i%6])%95+32;
  32. }
  33. cout << txtout << endl;
  34. return txtout;
  35. }
  36.  
  37. //*****************************//
  38. //****VERIFICATION DES MOTS****//
  39. double verif(string txt, string dico)
  40. {
  41. int pos_start,pos_end,wordlength,word,wordok,txtlength;
  42. word=0;
  43. wordok=0;
  44. pos_start=0;
  45. pos_end=0;
  46. wordlength=0;
  47. txtlength =txt.length();
  48. string mot;
  49. cout << "Le texte fait " << txtlength << " caracteres." << endl;
  50. txt.insert(txtlength," "); //on rajoute un espace à la fin, car c'est ce qu'on cherche, ce qui sépare les mots
  51. while(pos_end<=txtlength )
  52. {
  53. pos_end=txt.find(" ",pos_start); //on défini la position de fin du mot par un espace
  54. wordlength=pos_end-pos_start; //la longueur du mot qui va nous servir pour insérer le mot trouvé dans un string
  55. //for(wordlength=0; txt[pos_start+wordlength]!=' ' && txt[pos_start+wordlength]!=0; wordlength++);
  56. // mot=txt.substr(pos_start, wordlength);
  57. if(txt[pos_end-1]==',' || txt[pos_end-1]=='.')
  58. wordlength--;
  59. if(txt[pos_start+1]==39)//si apostrophe, aller voir 2 caract. plus loin (début du vrai mot)
  60. pos_start+=2;
  61. if(wordlength>=3)//on ne considère que le smots de 3+ lettres
  62. {
  63. word++;
  64. mot= txt.substr(pos_start,wordlength);
  65. //cout << "Un mot est" << mot << endl;
  66. 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
  67. mot[0]+=32;
  68. if(dico.find(mot,0)!=-1)
  69. wordok++;
  70. //cout << "Il y a " << wordok << " bons mots sur "<< word << " mots." << endl;
  71. }
  72. pos_end++;
  73. pos_start=pos_end; //à la prochaine itération, on commence à cherche à la case d'après
  74. }
  75. double fiab=0;
  76. fiab=(double)wordok/(double)word;
  77. cout << "Fiabilite: " << fiab <<". (il y a " << word << " mot dont "<<wordok << " correct(s)."<< endl;
  78. return fiab;
  79. }
  80.  
  81.  
  82. //***************************//
  83. //*****MAIN DU PROGRAMME*****//
  84. int main()
  85. {
  86. string txtcrypt,txttemp,dico,dicotemp,txtout;
  87. int pin=0;
  88. int key[6]= {0};
  89. double fiab;
  90.  
  91. ifstream txt("message_pin_4962.txt");
  92. if(txt.is_open())
  93. {
  94. cout << "Le fichier crypte a bien ete ouvert." << endl;
  95. while(txt.good())
  96. {
  97. getline(txt,txttemp); //on récupère le message crypé et on l'insère dans un string
  98. txtcrypt+=txttemp;
  99. }
  100. cout << "Le message crypte est :" << endl;
  101. cout << txtcrypt << endl;
  102.  
  103. ifstream dictionnaire("dictionnaire.txt");
  104. if(dictionnaire.is_open()) //chargement du duco dans un string
  105. {
  106. cout << "Le dictionnaire a bien ete ouvert." << endl;
  107. while(dictionnaire.good())
  108. {
  109. getline(dictionnaire,dicotemp);
  110. dico+=dicotemp;
  111. }
  112. dictionnaire.close();
  113. }
  114. else
  115. cout << "Le dictionnaire n'a pas pu etre ouvert." << endl;
  116. }
  117. else
  118. cout << "Erreur d'ouverture du fichier crypte." << endl;
  119.  
  120.  
  121. do
  122. {
  123. pintokey(pin,key);
  124. cout << key[0]<<'\t'<<key[1]<<'\t'<<key[2]<<'\t'<<key[3]<<'\t'<<key[4]<<'\t'<<key[5]<< endl;
  125. txtout = decrypt(txtcrypt,key);
  126. cout << "Decryptage termine." << endl;
  127. fiab = verif(txtout,dico);
  128. cout << "Calcul de fiabilite effectue." << endl;
  129. txtout.clear();
  130. pin++;
  131. }
  132. while(fiab<0.5 && pin<10000);
  133. if(verif(txtcrypt,dico)<0.5)
  134. cout << "Erreur, aucun PIN adéquat n'a pu etre trouve." << endl;
  135. if(verif(txtcrypt,dico)>0.5)
  136. {
  137. cout << "Texte decrypte. Le code pin correspondant est " << pin << " et le texte decrypte est:" << endl;
  138. cout << txt << endl;
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement