1. #include <iostream>
  2. #include <strstream>
  3. #include <fstream> //header file for file i/o, contains data types
  4. // ifstream, ofstream, open, close
  5. #include <string> //header file for string manipulation operations
  6. using namespace std;
  7.  
  8.  
  9.  
  10. //The CharacterShift Function takes in the Key
  11. //and the Cipher value (both chars)
  12. //and decrypts them as per this projects
  13. //request and it will also return a blank space
  14. //if the cipher is a blank space
  15. char CharacterShift(char Key, char Cipher);
  16. string Decryption(string CipherArray, string KeyArray);
  17. void IOFunction();
  18. int main(){
  19.  
  20.  
  21. IOFunction();
  22.  
  23.  
  24. return 0;
  25. }
  26.  
  27. char CharacterShift(char Key, char Cipher)
  28. {
  29. char output;
  30. if(Cipher==' ')
  31. {
  32. return Cipher;
  33. }
  34.  
  35. Key = Key - 65;
  36. Cipher = Cipher - 65;
  37.  
  38. if( Key > Cipher)
  39. {
  40. output = 91+Cipher-Key;
  41. }
  42. else
  43. {
  44. output = 65 + (Cipher-Key);
  45. }
  46.  
  47. return output;
  48. }
  49. string Decryption(string KeyArray, string CipherArray){
  50. int TemporaryCount=0;
  51. int i=0;
  52. string New;
  53. int CipherArrayLength=CipherArray.length();
  54. int KeyArrayLength=KeyArray.length();
  55.  
  56. for(i;i<CipherArrayLength;i++){
  57. if(CipherArray[i]=='?'){
  58. New+='?';
  59. }
  60. if(CipherArray[i]=='\''){
  61. New+='\'';
  62. }
  63. if(CipherArray[i]=='%'){
  64. New+='\n';
  65. New+='\n';
  66. }
  67. if(CipherArray[i]=='.'){
  68. New+='.';
  69. }
  70. if(TemporaryCount>=KeyArrayLength){
  71. TemporaryCount=0;
  72. }
  73. if(CipherArray[i]==' '){
  74. New+=' ';
  75. }
  76. if(TemporaryCount<KeyArrayLength && CharacterShift(KeyArray[TemporaryCount],CipherArray[i])!=' '&&CipherArray[i]!='.' ){
  77. New+=CharacterShift(KeyArray[TemporaryCount],CipherArray[i]);
  78. TemporaryCount++;
  79. }
  80. }
  81. return New;
  82. }
  83. void IOFunction(){
  84. string junk,Key1,Cipher1,Key2,Cipher2;
  85. string TotalText;
  86. string myFileName; //variable to hold external file name
  87. ifstream inFile; //internal file (stream) name
  88. cout <<"Please enter your input file name:";
  89. cin >> myFileName;
  90. inFile.open(myFileName.c_str()); //convert to c_string and opens file
  91. // if open does not work, inFile is in a “fail” state
  92. while (!inFile) // therefore !inFile will be ”true”
  93. {
  94. inFile.clear(); //puts the file states back to “good” from bad open
  95. cout << endl << "You entered an invalid file name. Please try again:";
  96. cin >> myFileName;
  97. inFile.open(myFileName.c_str());
  98. }
  99.  
  100. while (inFile) //”true” if the open command executed properly
  101. {
  102. getline(inFile,junk,'@');
  103. getline(inFile,Key1,'#');
  104. getline(inFile,Cipher1,'@');
  105. getline(inFile,Key2,'#');
  106. getline(inFile,Cipher2,'@');
  107. }
  108. if(junk.length()>1){
  109. cout<<"*** Error Code 2 -- No key given ***"<<endl;
  110. }
  111. inFile.close();
  112. cout<< Decryption(Key1, Cipher1);
  113. cout<< Decryption(Key2, Cipher2);
  114.  
  115.  
  116. }