J'essaye d'implémenter le Chiffre de Vigenere en C mais le problème est que quand j'essaye de répéter la clef utilisée dans le tableau il est dedans, il casse après la 4ème lettre. Donc, si la clé est ABC et que le texte en clair est HELLO, il renvoie HFNLO au lieu de HFNLP. Quand je regarde mon code, ça a du sens, mais ça ne marche pas. Quelqu'un peut-il voir le problème?Comment répéter les caractères dans le tableau
Voici le code:
int main(int argc, string argv[])
{
if(argc != 2)
{
printf("usage: ./vigenere k\n");
return 1;
}
//asks for plain text
printf("plaintext: ");
string text = get_string();
string k = argv[1];
printf("ciphertext: ");
//checks to see if length of key is shorter than length of plaintext and duplicates it.
int count = 0;
while(strlen(k) <= strlen(text))
{
k[strlen(k + count)] = k[count];
count++;
}
//changes key to be within 0 - 25 and encrypts plaintext
for(int i = 0; i < strlen(text); i++)
{
if(k[i] >= 'A' && k[i] <= 'Z')
{
k[i] = k[i] - 65;
}
else if (k[i] >= 'a' && k[i] <= 'z')
{
k[i] = k[i] - 97;
}
//if statement for plaintext capital letters
if(text[i] >= 'A' && text[i] <= 'Z')
{
text[i] = text[i] - 64;
text[i] = ((text[i] + k[i]) % 26) + 64;
}
//if statement for plaintext lowercase letters
else if(text[i] >= 'a' && text[i] <= 'z')
{
text[i] = text[i] - 96;
text[i] = ((text[i] + k[i]) % 26) + 96;
}
//prints final cipher
printf("%c", text[i]);
}
printf("\n");
return 0;
}
Ce qui est 'K'? et 'strlen (k + count)' semble définitivement louche. –
Utilisez l'opérateur de module sur l'index de clé, au lieu de ce malarky. Tels que 'k [i% strlen (k)]' (à des fins d'illustration: faire quelque chose de plus efficace). –