Skip to content

Instantly share code, notes, and snippets.

@AliBasicCoder
Created March 14, 2024 19:11
Show Gist options
  • Select an option

  • Save AliBasicCoder/8b9dc5d7713da90571969cffa925feeb to your computer and use it in GitHub Desktop.

Select an option

Save AliBasicCoder/8b9dc5d7713da90571969cffa925feeb to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
using namespace std;
string getKeyword() {
cout << "please enter your keyword: ";
while (true) {
// get input from user
string user_input;
cin.clear();
getline(cin, user_input);
string keyword;
// loop through the user input
for (int i = 0; i < user_input.length(); i++) {
char c = user_input[i];
// if the character is a capital letter add it to the keyword
if (c >= 65 && c <= 90)
keyword.push_back(c);
// if the character is a small letter convert it to a capital and add it
if (c >= 97 && c <= 122)
keyword.push_back(char((int) c - 32));
// ignore all other characters
}
if (keyword.length() > 8) {
cout << "Invalid input! keyword can't be longer than 8 characters! please enter a valid keyword: ";
continue;
}
if (keyword.empty()) {
cout << "Invalid input! keyword can't be empty please enter a valid keyword: ";
continue;
}
return keyword;
}
}
string getMessage() {
cout << "please enter your message: ";
while (true) {
// get user inout
string user_input;
cin.clear();
getline(cin, user_input);
string message;
// loop through user input
for (int i = 0; i < user_input.length(); i++) {
char c = user_input[i];
// if the character is lower case letter convert it to upper case and add it
if (c >= 97 && c <= 122)
message.push_back(char((int) c - 32));
// add all other characters (including upper case letters)
else
message.push_back(c);
}
if (message.length() > 80) {
cout << "Invalid input! message can't be longer than 80 characters! please enter a valid message: ";
continue;
}
if (message.empty()) {
cout << "Invalid input! message can't be empty please enter a valid message: ";
continue;
}
return message;
}
}
void vignereCipher() {
string keyword = getKeyword();
string message = getMessage();
int keywordLen = (int) keyword.length();
// loop through the message
for (int i = 0; i < message.length(); i++) {
// get current character from message
char c = message[i];
// get current repeated character from keyword
// if for example keywordLen=8, and i = 0. (i % keywordLen) = 1
// same for i = 8, i = 16, and so on...
// so the characters from keyword will repeat
char c2 = keyword[i % keywordLen];
// if the character is NOT an upper case letter print it as is
// note: we already converted all lower case letters to upper case
if (!(c >= 65 && c <= 90)) {
cout << c;
continue;
}
cout << char(((c + c2) % 26) + 65);
}
}
void vignereDecipher() {
string keyword = getKeyword();
string message = getMessage();
int keywordLen = (int) keyword.length();
for (int i = 0; i < message.length(); i++) {
char c = message[i];
char c2 = keyword[i % keywordLen];
if (!(c >= 65 && c <= 90)) {
cout << c;
continue;
}
// to decipher a character, subtract the keyword character ASCII value
// from the ciphered ASCII value
// then add a big multiple of 26 to the result (like 260 in this case)
// to ensure the result is not negative
// the divide the result by 26 and take the remainder
// and add 65
cout << char(((c - c2 + 260) % 26) + 65);
}
}
int main() {
vignereDecipher();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment