Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////
  2. //Class JumbledCipher                                           //
  3. //                                                              //
  4. //Will encrypt a file's data based on a key and output it to an //
  5. //output file                                                   //
  6. //                                                              //
  7. //////////////////////////////////////////////////////////////////
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <string>
  12. #include <map>
  13.  
  14. #include "JumbledCipher.h"
  15.  
  16. using namespace std;
  17.  
  18.  
  19. JumbledCipher::JumbledCipher(void)
  20. {
  21.     //key = "abcdefghijklmnopqrstuvwxyz";
  22.     key = "adec";
  23. }
  24.  
  25. JumbledCipher::~JumbledCipher(void)
  26. {
  27.     //does nothing
  28. }
  29.  
  30. void JumbledCipher::setPhrase(string somePhrase)
  31. {
  32.     phrase = somePhrase;
  33. }//end function setPhrase
  34.  
  35. void JumbledCipher::jumbleEncrypt()
  36. {
  37.     int phraseLength = phrase.length();//passes the length of the phrase to length
  38.     /*Used for setting the parameters for comparison checking*/
  39.     int capA = (int)'A';
  40.     int capZ = (int)'Z';
  41.     int lowA = (int)'a';
  42.     int lowZ = (int)'z';
  43.     int spaceChar = (int)' ';
  44.     int keyOffset,charTemp;
  45.     int keyLength = key.length();
  46.  
  47.     int tempCount=0;
  48.  
  49.     for(int j=0; j<phraseLength; j++)
  50.     {
  51.  
  52.         bool capital = false;//bools for capital and lowercase lettering
  53.         bool lower = false;
  54.  
  55.         charTemp = (int)phrase[j];//holds the ASCII value of each character in the string
  56.  
  57.         if((charTemp >= capA) && (charTemp <= capZ))
  58.         {
  59.             capital = true;
  60.         }
  61.         else if((charTemp >=lowA) && (charTemp <=lowZ))
  62.         {
  63.             lower = true;
  64.         }
  65.        
  66.         if(tempCount>=keyLength)
  67.         {
  68.             tempCount=1;
  69.         }
  70.         else
  71.         {
  72.             if(isspace(phrase[j]))
  73.             {
  74.                 charTemp = (int)charTemp;
  75.                 tempCount-1;
  76.             }
  77.             else
  78.             {
  79.                 keyOffset = (int)key[tempCount];
  80.  
  81.                 if((keyOffset<=lowZ)&&(keyOffset>=lowA))
  82.                 {
  83.                     keyOffset = keyOffset-lowA;
  84.                 }
  85.                 else if((keyOffset<=capZ)&&(keyOffset>=capA))
  86.                 {
  87.                     keyOffset = keyOffset-capA;
  88.                 }
  89.  
  90.                 ++tempCount;
  91.  
  92.                 charTemp=charTemp+keyOffset;
  93.  
  94.                 if(capital && (charTemp >capZ))
  95.                 {
  96.                     charTemp=(charTemp-wrapAround)-capZ+capA;
  97.                 }
  98.                 else if(lower &&(charTemp >lowZ))
  99.                 {
  100.                     charTemp=(charTemp-wrapAround)-lowZ+lowA;
  101.                 }
  102.             }
  103.             phrase[j]=(int)charTemp;
  104.         }  
  105.     }
  106. }//end function jumbleEncrypt
  107.  
  108. string JumbledCipher::getPhrase()
  109. {
  110.     outFile.open("encryptedJumble.txt",fstream::app);//opens the file encryptedJumble.txt, if it doesn't exist it is created
  111.     jumbleEncrypt();//handles the encryption of the phrase
  112.     outFile<< phrase<< endl;//sends the phrase to the file line by line
  113.     outFile.close();//closes the file
  114.  
  115.     return phrase;
  116. }//end function getPhrase
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement