Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. // beaufort cipher, http://practicalcryptography.com/ciphers/classical-era/beaufort/
  2.  
  3. #include<iostream>
  4. using std::cout; using std::cin; using std::endl;
  5. #include<string>
  6. using std::string;
  7. #include<algorithm>
  8. using std::rotate;
  9.  
  10.  
  11. const string alphabet = "abcdefghijklmnopqrstuvwxyz";
  12.  
  13.  
  14. string rotate_left(int move, string init_string){
  15. for (int i=0; i < move; i++){
  16. rotate(init_string.begin(), init_string.begin()+1,init_string.end());
  17. }//End of For loop
  18. return init_string;
  19. }//End of rotate_left function
  20.  
  21.  
  22. char beaufort_letter(char p_text, char key){
  23. if(isalpha(p_text) and isalpha(key)){
  24. if(islower(p_text) and islower(key)){
  25. int x = alphabet.find(p_text);
  26. string string_alpha = alphabet;
  27. string rotated_alpha = rotate_left(x,string_alpha);
  28. int y = rotated_alpha.find(key);
  29. return
  30. alphabet[y];
  31. }
  32. else
  33. return p_text;
  34. }
  35. else
  36. return p_text;
  37. }//End of beaufort_letter function
  38.  
  39. char vigenere_letter(char p_text, char key){
  40. if(isalpha(p_text) and isalpha(key)){
  41. if(islower(p_text) and islower(key)){
  42. int x = alphabet.find(p_text);
  43. string string_alpha = alphabet;
  44. string rotated_alpha = rotate_left(x,string_alpha);
  45. int y = alphabet.find(key);
  46. return
  47. rotated_alpha[y];
  48. }
  49. else
  50. return p_text;
  51. }
  52. else
  53. return p_text;
  54. }//End of vigenere_letter function
  55.  
  56. string encode_beaufort(string p_text, string key_s){
  57. string cipher_text = "";
  58. for (int i=0; i<p_text.length(); i++){
  59. if (isalpha(p_text[i]))
  60. cipher_text += p_text[i];
  61. }
  62. int length = cipher_text.length();
  63. int count_i = key_s.length();
  64. int count_loop = 0;
  65. while(key_s.length() != length){
  66. if (count_loop == count_i)
  67. count_loop = 0;
  68. else{
  69. key_s = key_s + key_s[count_loop];
  70. count_loop++;
  71. }
  72. }
  73. for (int i=0; i< length; i++){
  74. if (isalpha(cipher_text[i]) and islower(cipher_text[i])){
  75. cipher_text[i] = beaufort_letter(cipher_text[i],key_s[i]);
  76. }
  77. else
  78. break;
  79. }
  80. return cipher_text;
  81. }//End of encode_beaufort function
  82.  
  83. string encode_vigenere(string p_text, string key_s){
  84. string cipher_text = "";
  85. for (int i=0; i<p_text.length(); i++){
  86. if (isalpha(p_text[i]))
  87. cipher_text += p_text[i];
  88. }
  89. int length = cipher_text.length();
  90. int count_i = key_s.length();
  91. int count_loop = 0;
  92. while(key_s.length() != length){
  93. if (count_loop == count_i)
  94. count_loop = 0;
  95. else{
  96. key_s = key_s + key_s[count_loop];
  97. count_loop++;
  98. }
  99. }
  100. for (int i=0; i< length; i++){
  101. if (isalpha(cipher_text[i]) and islower(cipher_text[i])){
  102. cipher_text[i] = vigenere_letter(cipher_text[i],key_s[i]);
  103. }
  104. else
  105. break;
  106. }
  107. return cipher_text;
  108. }//End of encode_vigenere function
  109.  
  110.  
  111. int main (){
  112. int case_number;
  113. cin >> case_number;
  114.  
  115. switch (case_number){
  116.  
  117. case 1: {
  118. int rotations;
  119. cin >> rotations;
  120. string s;
  121. cin >> s;
  122. cout << rotate_left(rotations, s) << endl;
  123. break;
  124. }
  125.  
  126. case 2: {
  127. char plain_letter;
  128. char key_letter;
  129. cin >> plain_letter >> key_letter;
  130. cout << beaufort_letter(plain_letter, key_letter) << endl;
  131. break;
  132. }
  133.  
  134. case 3:{
  135. char plain_letter;
  136. char key_letter;
  137. cin >> plain_letter >> key_letter;
  138. cout << vigenere_letter(plain_letter, key_letter) << endl;
  139. break;
  140. }
  141.  
  142. case 4:{
  143. string plain_text;
  144. string key;
  145. cin.ignore(100, '\n');
  146. getline(cin, plain_text);
  147. getline(cin, key);
  148. cout << encode_beaufort(plain_text, key) << endl;
  149. break;
  150. }
  151.  
  152. case 5:{
  153. string plain_text;
  154. string key;
  155. cin.ignore(100, '\n');
  156. getline(cin, plain_text);
  157. getline(cin, key);
  158. cout << encode_vigenere(plain_text, key) << endl;
  159. break;
  160. }
  161.  
  162. } // of switch
  163. } // of main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement