Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6. string chiffre(string cleartext, int cle)
  7. {
  8. string cryptext{""};
  9. int val{0};
  10.  
  11. for(int i{0}; i < cleartext.size(); i++)
  12. {
  13. if((cleartext[i] + cle) > 126)
  14. {
  15. val = ((cleartext[i] + cle) % 127) + 32;
  16. cryptext.push_back(char(val));
  17. }
  18. else
  19. {
  20. cryptext.push_back(char(cleartext[i] + cle));
  21. }
  22. }
  23. return cryptext;
  24. }
  25. string dechiffre(string cryptext, int cle)
  26. {
  27. string cleartext{""};
  28. int val{0};
  29. for(int i{0}; i < cryptext.size(); i++)
  30. {
  31. if((cryptext[i] - cle) < 32)
  32. {
  33. val = (95 + (cryptext[i] - cle));
  34. while(val < 32)
  35. {
  36. val = (95 + (val));
  37. }
  38. if(val < 0)
  39. {
  40. val = val * -1;
  41. cleartext.push_back(char(val));
  42. }
  43. else
  44. {
  45. cleartext.push_back(char(val));
  46. }
  47. }
  48. else
  49. {
  50. cleartext.push_back(char(cryptext[i] - cle));
  51. }
  52. }
  53. return cleartext;
  54. }
  55. void filetociph(string filename, int cle)
  56. {
  57. string ligne;
  58. string temp;
  59. ifstream fin(filename);
  60. while(getline(fin, ligne))
  61. {
  62. ofstream fout("ciph1.txt", ios::app);
  63. temp = chiffre(ligne, cle);
  64. fout << temp << endl;
  65.  
  66. }
  67. }
  68. void ciphfiletotext(string filename, int cle)
  69. {
  70. string ligne;
  71. string temp;
  72. ifstream fin(filename);
  73. while(getline(fin, ligne))
  74. {
  75. ofstream fout("text1.txt", ios::app);
  76. temp = dechiffre(ligne, cle);
  77. fout << temp << endl;
  78.  
  79. }
  80. }
  81. void unknowkey(string filename)
  82. {
  83. string ligne;
  84. string temp;
  85.  
  86. ofstream fout("unknowtext.txt", ios::app);
  87.  
  88. for(int i{1}; i < 95; i++)
  89. {
  90.  
  91. ifstream fin(filename);
  92.  
  93. fout << endl;
  94. fout << i;
  95. fout << endl;
  96. while(getline(fin, ligne))
  97. {
  98. temp = dechiffre(ligne, i);
  99. fout << temp << endl;
  100.  
  101. }
  102. fout << "\n\n\n";
  103. fin.close();
  104. }
  105. }
  106. int main()
  107. {
  108. filetociph("C:\\Users\\Z.GHANMI\\Desktop\\caesreer\\text.txt", 50);
  109. ciphfiletotext("C:\\Users\\Z.GHANMI\\Desktop\\caesreer\\ciph1.txt", 50);
  110. unknowkey("C:\\Users\\Z.GHANMI\\Desktop\\caesreer\\ciph1.txt");
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement