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) {
|
|
|
|
this->rotation = rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Caesar::getRotation() {
|
|
|
|
return rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
char Caesar::encryptChar(char toEncrypt) {
|
2024-06-13 16:37:44 +00:00
|
|
|
int newChar = toEncrypt + rotation;
|
|
|
|
bool substractTwo = false;
|
2024-06-13 15:02:39 +00:00
|
|
|
|
2024-06-13 16:37:44 +00:00
|
|
|
while(newChar > MAX) newChar -= MAX;
|
2024-06-13 15:02:39 +00:00
|
|
|
|
2024-06-13 16:37:44 +00:00
|
|
|
substractTwo = newChar < MIN;
|
|
|
|
while(newChar < MIN) newChar += MIN;
|
2024-06-13 15:02:39 +00:00
|
|
|
|
2024-06-13 16:37:44 +00:00
|
|
|
if(substractTwo) newChar -= 2;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|