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
|
#include <string>
#include <fstream>
#include <iostream>
#include <cstdio>
#include <conio.h>
#include "cinhelper.cpp"
using namespace std;
const string cipher = "aaaaaaaa";
bool encipher(const string &pass, const string & input, string &output)
{
const int size_s = input.size(), size_c = pass.size();
int index_c = 0;
for (int i = 0; i < size_s; ++i, ++index_c)
{
if (index_c == size_c)
index_c = 0;
output[i] = (input[i] + pass[i]) % 256; //was 256
}
return true;
}
bool decipher(const string & pass, const string & input, string & output)
{
const int size_s = input.size(), size_c = cipher.size();
int index_c = 0;
for (int i = 0; i < size_s; ++i, ++index_c)
{
if (index_c == size_c)
index_c = 0;
output[i] = (input[i] - cipher[i]) % 256;
}
return true;
}
int main()
{
string plain, result = "damnit";
cout << "Type a string. \n>" << flush;
getline(cin, plain);
result.resize(plain.size());
cout << "You typed: " << plain << ".\n" << flush;
cout << "Encipher or decipher? 1 or 2?\n>" << flush;
int xyz = 0;
if (cin.fail())
{
cin.clear();
clearcin(cin);
}
cin >> xyz;
if (xyz == 1)
{
if (encipher(cipher, plain, result))
cout << "Your result: " << result << ".\n" << endl;
}
else if (xyz == 2)
{
if (decipher(cipher, plain, result))
{
cout << "Your result: " << result << ".\n" << endl;
}
}
else return 123;
return 0;
}
|