Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
README.md and Comments in source code
Browse files Browse the repository at this point in the history
  • Loading branch information
seigtm committed Apr 18, 2021
1 parent 5b847b5 commit 4760739
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 39 deletions.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Text files encryption using strategy design pattern in C++.

This source code is a template of using the **strategy** design pattern for text files encryption in C++.
This project is a homework from my college programming teacher.

## Problem statement:

> Develop the program for encrypting text documents.
> The user enters a string containing the path to the text file ("C:/example.txt").
> After that, he enters a number from 1 to 3 to clarify the text encryption method.
> After the selected algorithm works, the encrypted text is saved to another file ("C:/example_ciphered.txt").
> Develop the console application that implements the described functionality and contains a hierarchy of encryption classes.
> Justify the selected class hierarchy and the selected design pattern.
## Implementation:

A basic virtual class for std::string encryption strategies:

```cpp
class EncryptionStrategy
{
public:
virtual std::string encrypt(const std::string &text, const std::string &key = "") = 0;
virtual std::string decrypt(const std::string &text, const std::string &key = "") = 0;
};
```

Concrete encryption strategy using XOR:

```cpp
class XOREncryptionStrategy : public EncryptionStrategy ...
```
Concrete encryption strategy using Caesar:
```cpp
class CaesarEncryptionStrategy : public EncryptionStrategy ...
```

Concrete encryption strategy using Binary code:

```cpp
class BinaryEncryptionStrategy : public EncryptionStrategy ...
```
Interface for file encryption using encryption strategies:
```cpp
class IFileEncryptor ...
```
106 changes: 67 additions & 39 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,81 +5,100 @@
#include <bitset>
#include <sstream>

class EncryptionStrategy {
// A basic virtual class for std::string encryption strategies.
class EncryptionStrategy
{

public:
virtual std::string encrypt(const std::string& text, const std::string& key = "") = 0;
virtual std::string decrypt(const std::string& text, const std::string& key = "") = 0;
virtual std::string encrypt(const std::string &text, const std::string &key = "") = 0;
virtual std::string decrypt(const std::string &text, const std::string &key = "") = 0;
};


class XOREncryptionStrategy : public EncryptionStrategy {
// Concrete encryption strategy using XOR.
class XOREncryptionStrategy : public EncryptionStrategy
{

public:
std::string encrypt(const std::string& text, const std::string& key) override {
if (key.empty()) {
std::string encrypt(const std::string &text, const std::string &key) override
{
if (key.empty())
{
return text;
}

std::string output = text;

for (size_t i = 0; i < text.size(); i++)
{
output[i] = text[i] ^ key[i % key.size()];
}

return output;
}

std::string decrypt(const std::string& text, const std::string& key) override {
std::string decrypt(const std::string &text, const std::string &key) override
{
return encrypt(text, key);
}
};

class CaesarEncryptionStrategy : public EncryptionStrategy {

// Concrete encryption strategy using Caesar.
class CaesarEncryptionStrategy : public EncryptionStrategy
{
const size_t ASCIISize = 255;

public:
std::string encrypt(const std::string& text, const std::string& key) override {
std::string temp{ text };
std::string encrypt(const std::string &text, const std::string &key) override
{
std::string temp{text};
auto shift = std::stoull(key);

for (auto& ch : temp) {
for (auto &ch : temp)
{
ch += char(shift % ASCIISize);
}

return temp;
}

std::string decrypt(const std::string& text, const std::string& key) override {
std::string temp{ text };
std::string decrypt(const std::string &text, const std::string &key) override
{
std::string temp{text};
auto shift = std::stoull(key);

for (auto& ch : temp) {
for (auto &ch : temp)
{
ch -= char(shift % ASCIISize);
}

return temp;
}
};

class BinaryEncryptionStrategy : public EncryptionStrategy {
// Concrete encryption strategy using Binary code.
class BinaryEncryptionStrategy : public EncryptionStrategy
{

public:
std::string encrypt(const std::string& text, const std::string&) override {
std::string encrypt(const std::string &text, const std::string &) override
{
std::stringstream temp;

for (const auto &ch : text) {
for (const auto &ch : text)
{
std::bitset<8> bs(static_cast<unsigned long long>(ch));
temp << bs.to_string();
}

return temp.str();
}

std::string decrypt(const std::string& text, const std::string&) override {
std::string decrypt(const std::string &text, const std::string &) override
{
std::stringstream decoded;

for (auto segmentIterator{ text.begin() }; segmentIterator != text.end(); segmentIterator += 8) {
for (auto segmentIterator{text.begin()}; segmentIterator != text.end(); segmentIterator += 8)
{
std::string segment(segmentIterator, segmentIterator + 8);
auto ASCII = std::stoull(segment, nullptr, 2);
decoded << char(ASCII);
Expand All @@ -89,47 +108,56 @@ class BinaryEncryptionStrategy : public EncryptionStrategy {
}
};


class FileCryptingInterface {
// Interface for file encryption using encryption strategies.
class IFileEncryptor
{

public:
// FileCryptingInterface() = default;
// IFileEncryptor() = default;

void setStrategy(EncryptionStrategy* strat) {
void setStrategy(EncryptionStrategy *strat)
{
if (strat)
{
strategy = strat;
}
}

std::string encrypt(const std::string& text, const std::string& key = "") {
std::string encrypt(const std::string &text, const std::string &key = "")
{
return strategy ? strategy->encrypt(text, key) : text;
}

std::string decrypt(const std::string& text, const std::string& key = "") {
std::string decrypt(const std::string &text, const std::string &key = "")
{
return strategy ? strategy->decrypt(text, key) : text;
}

private:
EncryptionStrategy* strategy;
EncryptionStrategy *strategy;
};

int main() {
int main()
{
std::string text = "abc";
std::string key = "4";

FileCryptingInterface fileCrypter;
IFileEncryptor fileCryptor;

std::cout << "XOR:" << std::endl;
fileCrypter.setStrategy(new XOREncryptionStrategy);
std::cout << fileCrypter.encrypt(text, key) << std::endl;
std::cout << fileCrypter.decrypt("UVW", key) << std::endl << std::endl;
fileCryptor.setStrategy(new XOREncryptionStrategy);
std::cout << fileCryptor.encrypt(text, key) << std::endl;
std::cout << fileCryptor.decrypt("UVW", key) << std::endl
<< std::endl;

std::cout << "Caesar:" << std::endl;
fileCrypter.setStrategy(new CaesarEncryptionStrategy);
std::cout << fileCrypter.encrypt(text, key) << std::endl;
std::cout << fileCrypter.decrypt("efg", key) << std::endl << std::endl;
fileCryptor.setStrategy(new CaesarEncryptionStrategy);
std::cout << fileCryptor.encrypt(text, key) << std::endl;
std::cout << fileCryptor.decrypt("efg", key) << std::endl
<< std::endl;

std::cout << "Binary:" << std::endl;
fileCrypter.setStrategy(new BinaryEncryptionStrategy);
std::cout << fileCrypter.encrypt(text, "") << std::endl;
std::cout << fileCrypter.decrypt("011000010110001001100011", "") << std::endl;
fileCryptor.setStrategy(new BinaryEncryptionStrategy);
std::cout << fileCryptor.encrypt(text, "") << std::endl;
std::cout << fileCryptor.decrypt("011000010110001001100011", "") << std::endl;
}

0 comments on commit 4760739

Please sign in to comment.