Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////////////////////////////////////////////////////////////////
- //Class JumbledCipher //
- // //
- //Will encrypt a file's data based on a key and output it to an //
- //output file //
- // //
- //////////////////////////////////////////////////////////////////
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <map>
- #include "JumbledCipher.h"
- using namespace std;
- JumbledCipher::JumbledCipher(void)
- {
- //key = "abcdefghijklmnopqrstuvwxyz";
- key = "adec";
- }
- JumbledCipher::~JumbledCipher(void)
- {
- //does nothing
- }
- void JumbledCipher::setPhrase(string somePhrase)
- {
- phrase = somePhrase;
- }//end function setPhrase
- void JumbledCipher::jumbleEncrypt()
- {
- int phraseLength = phrase.length();//passes the length of the phrase to length
- /*Used for setting the parameters for comparison checking*/
- int capA = (int)'A';
- int capZ = (int)'Z';
- int lowA = (int)'a';
- int lowZ = (int)'z';
- int spaceChar = (int)' ';
- int keyOffset,charTemp;
- int keyLength = key.length();
- int tempCount=0;
- for(int j=0; j<phraseLength; j++)
- {
- bool capital = false;//bools for capital and lowercase lettering
- bool lower = false;
- charTemp = (int)phrase[j];//holds the ASCII value of each character in the string
- if((charTemp >= capA) && (charTemp <= capZ))
- {
- capital = true;
- }
- else if((charTemp >=lowA) && (charTemp <=lowZ))
- {
- lower = true;
- }
- if(tempCount>=keyLength)
- {
- tempCount=1;
- }
- else
- {
- if(isspace(phrase[j]))
- {
- charTemp = (int)charTemp;
- tempCount-1;
- }
- else
- {
- keyOffset = (int)key[tempCount];
- if((keyOffset<=lowZ)&&(keyOffset>=lowA))
- {
- keyOffset = keyOffset-lowA;
- }
- else if((keyOffset<=capZ)&&(keyOffset>=capA))
- {
- keyOffset = keyOffset-capA;
- }
- ++tempCount;
- charTemp=charTemp+keyOffset;
- if(capital && (charTemp >capZ))
- {
- charTemp=(charTemp-wrapAround)-capZ+capA;
- }
- else if(lower &&(charTemp >lowZ))
- {
- charTemp=(charTemp-wrapAround)-lowZ+lowA;
- }
- }
- phrase[j]=(int)charTemp;
- }
- }
- }//end function jumbleEncrypt
- string JumbledCipher::getPhrase()
- {
- outFile.open("encryptedJumble.txt",fstream::app);//opens the file encryptedJumble.txt, if it doesn't exist it is created
- jumbleEncrypt();//handles the encryption of the phrase
- outFile<< phrase<< endl;//sends the phrase to the file line by line
- outFile.close();//closes the file
- return phrase;
- }//end function getPhrase
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement