#include #include #include #include #define BUFF_SIZE 32 // Vérification que la clé est bien composée de lettres majuscules uniquement bool check_key(char* key) { for (size_t i=0; i < strlen(key); i++) { if ('A' > key[i] || key[i] > 'Z') return false; } return true; } // Chiffre/ Déchiffre uniquement les lettres, incrémente k_uses si la clé est utilisée char enc(char in, char k, int action, int* k_uses) { if ('a' <= in && in <= 'z') in += 'A'-'a'; if ('A' > in || in > 'Z') return in; (*k_uses)++; return ((in - 'A' + (action * (k-'A')) +('Z'-'A'+1))%('Z'-'A'+1))+'A'; } int vigenere(char* src, char* key, int action) { FILE* fp = fopen(src, "r"); char buffer[BUFF_SIZE]; int k_uses = 0; int klength = strlen(key); while (fgets(buffer, klength, fp)) { for (int i=0; i < BUFF_SIZE; i++) { if (buffer[i] == '\0') { printf("%s", buffer); break; } buffer[i] = enc(buffer[i], key[k_uses%klength], action, &k_uses); } } fclose(fp); return 0; } int main(int argc, char* argv[]) { char* src = NULL; char* key = NULL; int action = 0; for (int i=1; i < argc; i++) { if (!strcmp(argv[i], "-e")) { action = 1; src = argv[i+1]; i++; } else if (!strcmp(argv[i], "-d")) { action = -1; src = argv[i+1]; i++; } else if (!strcmp(argv[i], "-k")) { key = argv[i+1]; i++; } else { printf("Unknown option %s\n", argv[i]); } } if (!src || !key || !action) { printf("Usage: %s -[e|d] -k \n", argv[0]); return 1; } if (!check_key(key)) { printf("Clé de chiffrement invalide. Utilisez seulement des lettres majuscules.\n"); return 1; } return vigenere(src, key, action); }