2024-06-13 15:02:39 +00:00
|
|
|
#include "caesar.hpp"
|
|
|
|
#include <string>
|
2024-06-13 16:37:44 +00:00
|
|
|
#include <iostream>
|
2024-06-13 15:02:39 +00:00
|
|
|
|
|
|
|
Caesar::Caesar(int rotation) {
|
|
|
|
setRotation(rotation);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Caesar::setRotation(int rotation) {
|
2024-06-13 22:20:44 +00:00
|
|
|
this->rotation = rotation - MAX * (int)(rotation / MAX);
|
2024-06-13 15:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Caesar::getRotation() {
|
|
|
|
return rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
char Caesar::encryptChar(char toEncrypt) {
|
2024-06-13 16:37:44 +00:00
|
|
|
int newChar = toEncrypt + rotation;
|
2024-06-13 15:02:39 +00:00
|
|
|
|
2024-06-13 22:20:44 +00:00
|
|
|
bool substractTwo = newChar > MAX;
|
2024-06-13 15:02:39 +00:00
|
|
|
|
2024-06-13 22:20:44 +00:00
|
|
|
while(newChar > MAX) {
|
|
|
|
newChar -= MAX;
|
|
|
|
newChar += MIN - 1;
|
|
|
|
}
|
2024-06-13 16:37:44 +00:00
|
|
|
|
|
|
|
return (char)newChar;
|
2024-06-13 15:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Caesar::encryptString(std::string toEncrypt) {
|
|
|
|
std::string encrypted = "";
|
|
|
|
|
|
|
|
for(int i = 0; i < toEncrypt.length(); i++) {
|
|
|
|
encrypted += encryptChar(toEncrypt[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return encrypted;
|
|
|
|
}
|