I began to learn the pros and can not cope with the task to the Vigenère cipher. The function works, but encrypts not as it should be. Ispolzrvanie string and vector are prohibited
# include & lt; iostream & gt; // For Input and Output
#include & lt; fstream & gt; // For file input and output
#Include & lt; String.h & gt;
Using Namespace STD;
const int wordLength = 81;
void encodeText (char text [wordLength], char key [wordLength]) {
int textLen = strlen (text);
int keyLen = strlen (key);
int i, j;
char newKey [textLen];
char encryptedText [textLen];
// new key
for (i = 0, j = 0; i & lt; textLen; ++ i, ++ j) {
if (j == keyLen) {
j = 0;
}
newKey [i] = key [j];
}
newKey [i] = '\ 0';
// encryption
for (i = 0; i & lt; textLen; i ++) {
if (text [i]! = '') {
encryptedText [i] = ((text [i] + newKey [i])% 26);
encryptedText [i] + = 'a';
}
ELSE {
encryptedText [i] = '';
}
}
encryptedText [i] = '\ 0';
COUT & LT; & LT; newKey & lt; & lt; Endl;
COUT & LT; & LT; text & lt; & lt; Endl;
COUT & LT; & LT; encryptedText & lt; & lt; Endl;
}
INT MAIN () {
COUT & LT; & LT; "Enter the text to encrypt";
char textToEncode [wordLength];
cin.getline (textToEncode, wordLength);
COUT & LT; & LT; "Enter key";
char keyword [wordLength];
cin.getline (keyword, wordLength);
COUT & LT; & LT; "The key, and cipher text: \ n";
encodeText (textToEncode, keyword);
Return 0;
}
For example code displays:
Enter the text to encrypt: all generalizations are false
Enter key: secret
Key and cipher text:
secretsecretsecretsecretsecre
all generalizations are false
ebz wjrufdbndqhlesw ouu jqzvu
The code should display:
secretsecretsecretsecretsecre
all generalizations are false
spn kxfitrpbrevzsgk cii xenji
Answer 1
Yuri Kozlov Answer:
is necessary is encryptedText [i] = ((text [i] + newKey [i])% 26)
is replaced by this encryptedText [i] = (((text [i] - 'a') + (newKey [i] - 'a'))% 26)
.