1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include <iostream>
#include <string>
enum class Mode{Encrypt,Decrypt,KeyReset};
class DataObject
{
public:
DataObject(const std::string& data,const std::string& encdata = {}, const bool encrypted = false);
std::string getKey();
std::string EncryptDecrypt(const Mode& _execMode = Mode::Encrypt);
private:
bool execute(const std::string& key, const Mode& _execMode = Mode::Encrypt);
std::string _OrigData;
std::string _EncrData;
bool _Encrypted;
};
int main()
{
std::string data{},key;
char choice;
Mode runMode;
std::cout<<"Enter your Data: ";
std::getline(std::cin,data,'\n');
DataObject myData(data);
while(true)
{
std::cout<<"============================================\n";
std::cout<<"\nwhat would you like to do?\n 1] Encrypt\n 2] Decrypted\n 3] change key\n 4] Quit\n\t : ";
std::cin>>choice;
std::cout<<"\n*******************************************\n";
if(choice == '1')
runMode = Mode::Encrypt;
else if(choice == '2')
runMode = Mode::Decrypt;
else if(choice == '3')
runMode = Mode::KeyReset;
else
break;
std::cout<<myData.EncryptDecrypt(runMode)<<"\n";;
std::cout<<"\n============================================\n";
}
std::cout<<"\n\nGoodbye...";
}
DataObject::DataObject(const std::string& data,const std::string& encdata, const bool encrypted)
:_OrigData(data), _EncrData(encdata), _Encrypted(encrypted) {}
std::string DataObject::getKey()
{
std::string key;
std::cout<<"\nEnter your key (arbitrary length): ";
std::cin>>key;
return key;
}
bool DataObject::execute(const std::string& key, const Mode& _execMode)
{
std::string _runData{};
int _origDataIndex = 0, _keyIndex = 0;
int _dataSize = _OrigData.size(), _keySize = key.size();
char _processedChar{};
if(_execMode == Mode::Decrypt && !_Encrypted)
return false;
while(_origDataIndex < _dataSize)
{
if(_execMode == Mode::Encrypt)
{
if(!_Encrypted)
_processedChar = (char)(_OrigData[_origDataIndex] ^ key[_keyIndex]);
else
return false;
}
else if(_execMode == Mode::Decrypt)
_processedChar = (char)(_EncrData[_origDataIndex] ^ key[_keyIndex]);
_runData += _processedChar;
++_origDataIndex;
++_keyIndex;
if(_keyIndex == _keySize)
_keyIndex = 0;
}
if(_execMode == Mode::Encrypt)
{
_EncrData = _runData;
_Encrypted = true;
}
if(_execMode == Mode::Decrypt && _runData != _OrigData)
return false;///decryption failed: bad key
return true; ///operation completed successfully
}
std::string DataObject::EncryptDecrypt(const Mode& _execMode)
{
std::string key{};
if(_execMode != Mode::KeyReset)
{
key = getKey();
bool status = execute(key,_execMode);
if(_execMode == Mode::Encrypt)
{
if(status == false) ///failed
return "\n<Already Encrypted>";
else
return "\n<successfully Encrypted>";
}
else if(_execMode == Mode::Decrypt)
{
if(status == false && !_Encrypted)
return "\n<Can't Decrypt unencrypted data>";
else if(status == false)
return "\n<Decryption failed>: bad key <"+key+">";
else
return "\n<successfully Decrypted>: "+_OrigData;
}
}
else
{
if(!_Encrypted)
return "\n<FAILED: No previous key found>";
int attempts = 1;
bool correct = false;
while(attempts < 4 && !correct)
{
std::cout<<"\nAttempt "<<attempts++<<"] Enter your original Key: ";
std::cin>>key;
if(execute(key,Mode::Decrypt) == true)
correct = true;
}
if(correct)
{
std::cout<<"\nEnter your new key: ";
std::cin>>key;
_EncrData = {};
_Encrypted = false;
if(execute(key,Mode::Encrypt) == true)
return "\n<Key changed successfully>";
}
return "\n<FAILED: you entered the wrong key for 3 times>";
}
}
|