School-Coding-Cpp/sfusi/encrypt_vigenèreCipher.cpp

55 lines
1.1 KiB
C++

/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 08/03/2025
*/
#include <iostream>
#include <cctype>
#define SIZE 100+1
using namespace std;
void encryptVingenereCipher(char *str, int offset, char *wormOffset);
int main(void) {
char str[SIZE]; //ABCDEFGHIJKLMNOPQRSTUVWXYZ
char wormOffset[SIZE];
int offset;
cout << "Inserisci una frase: ";
cin.getline(str, SIZE);
cout << "Inserisci la chiave: ";
cin.getline(wormOffset, SIZE);
encryptVingenereCipher(str, offset, wormOffset);
return 0;
}
void encryptVingenereCipher(char *str, int offset, char *wormOffset) {
int j = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
if (isupper(wormOffset[j % strlen(wormOffset)])) {
offset = wormOffset[j % strlen(wormOffset)] - 'A';
} else {
offset = wormOffset[j % strlen(wormOffset)] - 'a';
}
if (isupper(str[i])) {
str[i] = ((str[i] - 'A' + offset + 26) % 26) + 'A';
} else if (islower(str[i])) {
str[i] = ((str[i] - 'a' + offset + 26) % 26) + 'a';
}
j++;
}
}
cout << "Frase codificata: " << str << endl;
}