Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cctype>
  6.  
  7. using namespace std;
  8.  
  9. class Crypto {
  10. public:
  11. void DisplayFile(string filename);
  12. void EncryptFile(string cipher_key, string filename_from, string filename_to);
  13. void DecryptFile(string cipher_key, string filename_from, string filename_to);
  14. string EncryptString(string &cipher_key, string string_to_be_encrypted);
  15. string DecryptString(string &cipher_key, string string_to_be_decrypted);
  16. private:
  17. string cipher_key;
  18. void RotateCipherKey();
  19. char substitution_cipher(char char_to_encrypt);
  20. char reverse_substitution_cipher(char char_to_encrypt);
  21. };
  22.  
  23. using namespace std;
  24.  
  25. char Crypto::substitution_cipher(char char_to_encrypt)
  26. {
  27. int cipherLength=0, i=0;
  28.  
  29. if(!islower(char_to_encrypt))
  30. char_to_encrypt = tolower(char_to_encrypt);
  31.  
  32. if(!isalpha(char_to_encrypt)){
  33. cout << "Character to encrypt must be alphabetical!";
  34. return char_to_encrypt;
  35. }
  36.  
  37. cipherLength = cipher_key.length();
  38.  
  39. for(i=0; i<cipherLength; i++){
  40. RotateCipherKey();
  41. char_to_encrypt = cipher_key[i];
  42. }
  43.  
  44. return char_to_encrypt;
  45. }
  46.  
  47.  
  48. // Perform a "reverse" substitution cipher on a single character
  49. // using the specified cipher key
  50. // See reference document for implementation details.
  51. // You reverse the operation described above
  52.  
  53. char Crypto::reverse_substitution_cipher(char char_to_decrypt)
  54. {
  55. int i=0, cipherLength=0;
  56. cipherLength = cipher_key.length();
  57.  
  58. for(i=0; i<cipherLength; i++){
  59. RotateCipherKey();
  60. char_to_decrypt = cipher_key[i];
  61. }
  62.  
  63. return char_to_decrypt;
  64. }
  65.  
  66. // Encrypt String and return it
  67. // You will use the substitution_cipher() function to encrypt the
  68. // individual characters
  69. //
  70. // Think for loop
  71. string Crypto::EncryptString(string &cipher_key,
  72. string string_to_be_encrypted)
  73. {
  74. int i=0, loopBound=0;
  75. char tempChar;
  76. if(string_to_be_encrypted.length()>0)
  77. loopBound=string_to_be_encrypted.length();
  78.  
  79. for(i=0; i < loopBound; i++){
  80. tempChar = substitution_cipher(string_to_be_encrypted[i]);
  81. RotateCipherKey();
  82. }
  83.  
  84. string encrypted_string = string_to_be_encrypted;
  85. cout << " " << string_to_be_encrypted;
  86. return encrypted_string;
  87. }
  88.  
  89. // Decrypt String and return it
  90. // You will use the reverse_subsitution_cipher() function to
  91. // decrypt the individual characters
  92. //
  93. string Crypto::DecryptString(string &cipher_key, string string_to_be_decrypted)
  94. {
  95. string decrypted_string = string_to_be_decrypted;
  96.  
  97. return decrypted_string;
  98. }
  99.  
  100. //
  101. // Need to finish this one
  102. //
  103. // Display file specified by the filname parameter
  104. // See EncryptFile/DecryptFile for details on how to read and
  105. // display a file. Use the ifstream infile section to read a file
  106. // and cout to display it to the screen.
  107. void Crypto::DisplayFile(string filename)
  108. {
  109. string tempString;
  110. ifstream infile;
  111. infile.open(filename);
  112.  
  113. infile >> tempString;
  114. while(infile){
  115. cout << " " << tempString;
  116. infile >> tempString;
  117. }
  118.  
  119. cout << endl;
  120. }
  121.  
  122. // Encrypt the specified file using the specified cipher key and
  123. // write the output to a different file
  124. // This function is complete
  125. void Crypto::EncryptFile(string cipher_key,
  126. string filename_from, string filename_to)
  127. {
  128. string input;
  129. ifstream infile;
  130. ofstream outfile;
  131.  
  132. infile.open (filename_from.c_str());
  133. outfile.open (filename_to.c_str());
  134.  
  135. if(!infile)
  136. {
  137. cout << "Can not open input file " + filename_from << endl;
  138. exit(0);
  139. }
  140.  
  141. if(!outfile)
  142. {
  143. cout << "Can not open Output file " + filename_to << endl;
  144. exit(0);
  145. }
  146.  
  147.  
  148. while( getline(infile, input))
  149. {
  150. outfile << EncryptString(cipher_key, input) << endl;
  151. }
  152. infile.close();
  153. outfile.close();
  154. }
  155.  
  156. // Decrypt the specified file using the specified cipher key and
  157. // write the output to a different file
  158. // This function is complete
  159. void Crypto::DecryptFile(string cipher_key,
  160. string filename_from, string filename_to)
  161. {
  162. string input;
  163. ifstream infile;
  164. ofstream outfile;
  165.  
  166. infile.open (filename_from.c_str());
  167. outfile.open (filename_to.c_str());
  168.  
  169. if(!infile)
  170. {
  171. cout << "Can not open input file " + filename_from << endl;
  172. exit(0);
  173. }
  174.  
  175. if(!outfile)
  176. {
  177. cout << "Can not open Output file " + filename_to << endl;
  178. exit(0);
  179. }
  180.  
  181.  
  182. while( getline(infile, input))
  183. {
  184. outfile << DecryptString(cipher_key, input) << endl;
  185. }
  186. infile.close();
  187. outfile.close();
  188. }
  189.  
  190. int main()
  191. {
  192. Crypto Cipher1;
  193. Crypto Cipher2;
  194. string cipher_key = "qwertyuiopasdfghjklzxcvbnm";
  195. string cipher_key2 = "wertyuiopasdgfhjqklzxcvbnm";
  196.  
  197. Cipher1.EncryptFile(cipher_key, "test.txt",
  198. "test-encrypted.txt");
  199. Cipher1.DecryptFile(cipher_key, "test-encrypted.txt",
  200. "test-encrypt-decrypt-test.txt");
  201. Cipher1.DisplayFile("test.txt");
  202. Cipher1.DisplayFile("test-encrypted.txt");
  203. Cipher1.DisplayFile("test-encrypt-decrypt-test.txt");
  204.  
  205. string cipher_key3 = cipher_key2;
  206. cout << Cipher2.EncryptString(cipher_key3,
  207. "C++ Programming Is Fun") << endl;
  208.  
  209. cipher_key3 = cipher_key2;
  210. string cipher_key4 = cipher_key2;
  211.  
  212. cout << Cipher2.DecryptString(cipher_key2,
  213. Cipher2.EncryptString(cipher_key3,"C++ Programming Is Fun") )
  214. << endl;
  215.  
  216. return 0;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement