C++ examples for Operator:Arithmetic Operator
Enforcing Privacy with Cryptography for integer
#include <iostream> // w w w. j ava 2 s.c o m class Converter { private: int plainPassCode = 0; int encPassCode = 0; // SETTERS bool setPlainPass(int); void setEncPass(int); bool validatePlainPass(int); int swapDigits(int); void clearPlainPass(); void clearEncPass(); public: Converter(); ~Converter(); // GETTERS int getPlainPass(); int getEncPass(); bool encrypt(int); int decrypt(int = 0); }; Converter::Converter() {} Converter::~Converter() { clearPlainPass(); clearEncPass(); } // SETTERS bool Converter::setPlainPass(int plain) { if (validatePlainPass(plain)) { plainPassCode = plain; return true; } return false; } void Converter::setEncPass(int enc) { encPassCode = enc; } // GETTERS int Converter::getPlainPass() { return plainPassCode; } int Converter::getEncPass() { return encPassCode; } // validate plain pass code is 4 digits in length bool Converter::validatePlainPass(int plain) { int counter = 0; while (plain > 0) { plain /= 10; counter++; } return (counter == 4) ? true : false; } bool Converter::encrypt(int plain) { if (setPlainPass(plain)) { int digits = 0; for (int i = 3; i >= 0; i--) { digits = (digits * 10) + ((plain % 10) + 7) % 10; plain /= 10; } swapDigits(digits); setEncPass(digits); clearPlainPass(); return true; } std::cout << "ERROR: Invalid length. A 4 digit pass code is required" << std::endl; return false; } int Converter::decrypt(int enc) { int digits = 0; if (enc == 0) enc = getEncPass(); swapDigits(enc); for (int i = 3; i >= 0; i--) { digits = (digits * 10) + (((enc % 10) + 10) - 7) % 10; enc /= 10; } clearEncPass(); setPlainPass(digits); return digits; } int Converter::swapDigits(int digits) { int a, b, c, d, tmp, swapped = 0; d = digits % 10; digits /= 10; c = digits % 10; digits /= 10; b = digits % 10; digits /= 10; tmp = digits; a = c; c = tmp; tmp = c; b = d; d = tmp; swapped = (swapped * 10) + a; swapped = (swapped * 10) + b; swapped = (swapped * 10) + c; swapped = (swapped * 10) + d; return swapped; } void Converter::clearPlainPass() { plainPassCode = 0; } void Converter::clearEncPass() { encPassCode = 0; } int main(int argc, const char *argv[]) { Converter es; int code = 1234; es.encrypt(code); std::cout << "\nEncrypted pass code: " << es.getEncPass() << "\n" << std::endl; std::cout << "Decrypted pass code: " << es.decrypt() << std::endl; return 0; }