From 597d48ba0fce30cd7d6de158affb7335afd2fb63 Mon Sep 17 00:00:00 2001 From: xoy Date: Thu, 13 Jun 2024 17:02:39 +0200 Subject: [PATCH] [First commit] Basic caesar encryption. --- .gitignore | 1 + caesar.cpp | 34 ++++++++++++++++++++++++++++++++++ caesar.hpp | 19 +++++++++++++++++++ main.cpp | 25 +++++++++++++++++++++++++ makefile | 21 +++++++++++++++++++++ 5 files changed, 100 insertions(+) create mode 100644 .gitignore create mode 100644 caesar.cpp create mode 100644 caesar.hpp create mode 100644 main.cpp create mode 100644 makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbe9c82 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode/ \ No newline at end of file diff --git a/caesar.cpp b/caesar.cpp new file mode 100644 index 0000000..c9bb5de --- /dev/null +++ b/caesar.cpp @@ -0,0 +1,34 @@ +#include "caesar.hpp" +#include + +Caesar::Caesar(int rotation) { + setRotation(rotation); +} + +void Caesar::setRotation(int rotation) { + this->rotation = rotation; +} + +int Caesar::getRotation() { + return rotation; +} + +char Caesar::encryptChar(char toEncrypt) { + char newChar = toEncrypt + rotation; + + if(newChar > MAX) newChar -= MAX; + + if(newChar < MIN) newChar += MIN; + + return newChar; +} + +std::string Caesar::encryptString(std::string toEncrypt) { + std::string encrypted = ""; + + for(int i = 0; i < toEncrypt.length(); i++) { + encrypted += encryptChar(toEncrypt[i]); + } + + return encrypted; +} diff --git a/caesar.hpp b/caesar.hpp new file mode 100644 index 0000000..16756d8 --- /dev/null +++ b/caesar.hpp @@ -0,0 +1,19 @@ +#ifndef CAESAR_H +#define CAESAR_H + +#include + +class Caesar { + public: + Caesar(int rotation); + void setRotation(int rotation); + int getRotation(); + char encryptChar(char toEncrypt); + std::string encryptString(std::string toEncrypt); + private: + int rotation; + const int MIN = 33; + const int MAX = 126; +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..993a6af --- /dev/null +++ b/main.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "caesar.hpp" + +const std::string usage = "caesar "; + +int main(int argc, char* argv[]) { + if(argc < 3) { + std::cout << usage << std::endl; + return 1; + } + + int rotation = std::stoi(argv[1]); + std::string toEncrypt = ""; + + for(int i = 2; i < argc; i++) { + toEncrypt += argv[i]; + if(i < argc - 1) toEncrypt += " "; + } + + Caesar caesar{rotation}; + std::cout << caesar.encryptString(toEncrypt) << std::endl; + + return 0; +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..335064b --- /dev/null +++ b/makefile @@ -0,0 +1,21 @@ + +CXX = g++ + +CXXFLAGS = -std=c++11 -Wall + +TARGET = caesar + +SRCS = main.cpp caesar.cpp + +OBJS = $(SRCS:.cpp=.o) + +$(TARGET): $(OBJS) + $(CXX) $(CXXFLAGS) $(OBJS) -o $(TARGET) + +%.o: %.cpp + $(CXX) $(CXXFLAGS) -c $< -o $@ + +clean: + rm -f $(OBJS) $(TARGET) + +.PHONY: clean