Не могу найти ошибку в моей программе кодирования текста

Просто ради интереса, я работал над программой кодирования текста, которая будет шифровать текст. У меня это работает почти безупречно, но, похоже, не удалось решить одну ошибку. По сути, текст по большей части будет правильно кодироваться и декодироваться, за исключением того, что время от времени программа будет декодировать строку с одной буквой не на своем месте. Код довольно уродливый, я не пытался его очистить, так как я до сих пор работаю над ошибками, поэтому я извиняюсь за это. Кроме того, он плохо копируется и вставляется в разделы, так что вероятность оптимизации будет для меня огромной позже, после того, как я все заработаю правильно. У меня такое чувство, что я знаю, где ошибка, но внесение исправлений, которые, я думаю, необходимо сделать, вызывает бесконечный цикл. Вот код с некоторым выводом внизу, сохраненным в комментарии:

#include <iostream>
using namespace std;

void FillKey(char Key2[]);

void Encrypt();
void EncryptMessage(char Key3[], char Input2[], char MessageEncrypted2[]);

void Decrypt();
void DecryptMessage(char Key4[], char CodedInput2[], char NormalOutput2[]);
char ReturnDecodedCharacter(char Key5[], char Character);

const int STRING_SIZE=500;
const int ASCII_RANGE_SIZE_94=94;//Range from ASCII value 32 to 126 = 94
const int SHIFT_SET_32=32;//Move range up 32 values form (0-93) to (32 to 126)

int main()
{
int Choice;

do
{
cout << "Press 1 to encrypt"      << endl;
cout << "Press 2 to decrypt"      << endl;
cout << "Press other key to quit" << endl << endl;
cout << "Choice: ";

cin >> Choice;

if (Choice == 1)
Encrypt();

else if (Choice == 2)
Decrypt();
}

while (Choice==1 || Choice == 2);
}

void Encrypt()
{
int  SecretNumber;

char Key[ASCII_RANGE_SIZE_94]={0};//One larger 0 - 93, not using 0
char Input[STRING_SIZE]={0};
char MessageEncrypted[STRING_SIZE]={0};

cout << "\nEnter Secret Number: ";
cin >> SecretNumber;

cin.ignore(1);//removes newline from input buffer

srand(SecretNumber);//Seed number for my random number generator

FillKey(Key);

cout << "\nInput message: ";

cin.getline(Input, STRING_SIZE);

EncryptMessage(Key, Input, MessageEncrypted);

cout << "\nEncrypted Message(copy inside of parenthesis): " << "(" << MessageEncrypted << ")" << endl << endl;
}

void FillKey(char Key3[])
{
int RandomNumber;
int KeySpot, j;

for (KeySpot=0; KeySpot<=ASCII_RANGE_SIZE_94; ++KeySpot)
{
RandomNumber = SHIFT_SET_32+rand()%ASCII_RANGE_SIZE_94;//0-93, then shifts up to 32-126 ASCII range

for (j=0; j<=KeySpot; ++j)//this bit of code checks to make sure new rand number isn't already used before
{
if (Key3[j] == RandomNumber)
{
RandomNumber = SHIFT_SET_32+rand()%ASCII_RANGE_SIZE_94;//0-93, then shifts up to 32-126
j=0;
}
}

Key3[KeySpot] = (char) RandomNumber;
}
}

void EncryptMessage(char Key3[], char Input2[], char MessageEncrypted2[])
{
for (int i=0; Input2[i]; ++i)
{
MessageEncrypted2[i]= Key3[((int) Input2[i])-SHIFT_SET_32];//-32 to get range back into 1-95 (range of Key)
}
}

void Decrypt()
{
int SecretNumber;

char Key[ASCII_RANGE_SIZE_94]={0};
char EncryptedMessage[STRING_SIZE]={0};
char DecodedMessage[STRING_SIZE]={0};

cout << "\nEnter Secret Number: ";
cin >> SecretNumber;

cin.ignore(1);//removes newline from input buffer

srand(SecretNumber);//Seed number for random number generator

FillKey(Key);

cout << "\nInput message to decode: ";

cin.getline(EncryptedMessage, STRING_SIZE);

DecryptMessage(Key, EncryptedMessage, DecodedMessage);

cout << "\nDecoded Message: " << DecodedMessage << endl << endl;
}

void DecryptMessage(char Key4[], char EncryptedMessage2[], char DecodedMessage2[])
{
for (int i=0; EncryptedMessage2[i]; ++i)
{
DecodedMessage2[i] = ReturnDecodedCharacter(Key4, EncryptedMessage2[i]);
}
}

char ReturnDecodedCharacter(char Key5[], char Character)
{
for (int Count=0; Count<=ASCII_RANGE_SIZE_94; ++Count)
{
if ((int) Character == Key5[Count])
{
return (char) Count+SHIFT_SET_32;//+32 to get back into 32-126 ASCII range
}
}

return 0;
}

/*

Press 1 to encrypt
Press 2 to decrypt
Press other key to quit

Choice: 2

Enter Secret Number: 1

Input message to decode: abcdefghijklmnopqrstuvqwyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Decoded Message: Yj.63;nQb_ d"*!yTuc'8rTWi~1/|^ta5:?s+ODJwge42X,%q$SEvNZ9-h=kFL

Press 1 to encrypt
Press 2 to decrypt
Press other key to quit

Choice: 1

Enter Secret Number: 2

Input message: abcdefghijklmnopqrstuvqwyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Encrypted Message(copy inside of parenthesis): (oZs?=LtFPSk<B +j]&xf!}]1"yq;zQm8\:>|T7_56VRH{9D-/*aOdbGCgAnJuv)

Press 1 to encrypt
Press 2 to decrypt
Press other key to quit

Choice: 2

Enter Secret Number: 2

Input message to decode: oZs?=LtFPSk<B +j]&xf!}]1"yq;zQm8\:>|T7_56VRH{9D-/*aOdbGCgAnJuv

Decoded Message: abcdefghijklmnopqrstuvqwyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Press 1 to encrypt
Press 2 to decrypt
Press other key to quit

Choice: 1

Enter Secret Number: 3

Input message: abcdefghijklmnopqrstuvqwyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Encrypted Message(copy inside of parenthesis): (w?oGY`hn5Ty% L\Z$qrDHR$"!F=WX}IV-Nf|g&+(Ps.pM*S)1t/e2@9v067{KC)

Press 1 to encrypt
Press 2 to decrypt
Press other key to quit

Choice: 2

Enter Secret Number: 3

Input message to decode: w?oGY`hn5Ty% L\Z$qrDHR$"!F=WX}IV-Nf|g&+(Ps.pM*S)1t/e2@9v067{KC

Decoded Message: abcdefghijklmnopqrstuvqwyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Press 1 to encrypt
Press 2 to decrypt
Press other key to quit

Choice: 1

Enter Secret Number: 4

Input message: abcdefghijklmnopqrstuvqwyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Encrypted Message(copy inside of parenthesis): (2RQ%?u*"_yNXs!=t;VWb9);]:+#>dqv$wU7(EiP[JgGI&pxmh4\ eafk}0^1ln)

Press 1 to encrypt
Press 2 to decrypt
Press other key to quit

Choice: 2

Enter Secret Number: 4

Input message to decode: 2RQ%?u*"_yNXs!=t;VWb9);]:+#>dqv$wU7(EiP[JgGI&pxmh4\ eafk}0^1ln

Decoded Message:  bcdefghijklmnopqrstuvqwyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890

Press 1 to encrypt
Press 2 to decrypt
Press other key to quit

Choice:

*/

Если вы заметили, последняя строка (с секретным номером 4) не была преобразована правильно и потеряла начальное «а». Я думаю, что ошибка в void FillKey функция — внутри оператора if if (Key3[j] == RandomNumber) Я создаю новое случайное число и устанавливаю j=0 если любое новое сгенерированное случайное число точно такое же, как любое из предыдущих в ключе (очевидно, не может иметь один и тот же символ дважды, очевидно). Поэтому я думаю, что j устанавливается в 0, но после цикла for он увеличивается и становится равным 1. Так что программа не проверяет наличие дубликатов в Key3[3]поэтому я изменил утверждение на j=-1, чтобы предвидеть это и вернуть его к нулю после цикла for, но тогда программа застревает и никогда не существует цикла.

Любые предложения будут ценны.

1

Решение

Здесь есть ошибка:

for (KeySpot=0; KeySpot<=ASCII_RANGE_SIZE_94; ++KeySpot)

Так должно быть:

for (KeySpot=0; KeySpot < ASCII_RANGE_SIZE_94; ++KeySpot)

Потому что размер ключа ASCII_RANGE_SIZE_94, который включает в себя нулевой индекс. Та же ошибка в ReturnDecodedCharacter, так должно быть:

for (int Count=0; Count < ASCII_RANGE_SIZE_94; ++Count)

Чтобы сделать код более простым для чтения, вы можете написать следующее:

char ReturnDecodedCharacter(const char *key, int keysize, char Character)
{
for (int Count = 0; Count < keysize; ++Count)
{
if (Character == key[Count])
{
return (char)(Count + SHIFT_SET_32);//+32 to get back into 32-126 ASCII range
}
}
return 0;
}

Тогда пройдите ASCII_RANGE_SIZE_94 как keysize при вызове функции


Это также можно сделать с помощью оператора XOR

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void xor_with_cipher(const string &cipher, const string &src, string &dst)
{
for (unsigned int i = 0; i < src.length(); i++)
{
char c = src[i] ^ cipher[i];
dst.push_back(c);
}
}

int main()
{
string cipher;

//create a simple cipher key with rand()
//Note, rand **** should not **** be used in real cryptography applications
int secretKey = 123;
srand(secretKey);
for (int i = 0; i < 100; i++)
{
char c = rand() % 0xff;
cipher.push_back(c);
}

string input = "plain text";
string encoded;
string decoded;

//XOR cipher with input, create encoded
xor_with_cipher(cipher, input, encoded);

//XOR cipher with encoded, create decoded (decoded = input)
xor_with_cipher(cipher, encoded, decoded);

//encoded string is in binary, it may contain zero and non-printable characters
//we can print the integer value of individual characters in encoded
cout << "encoded: ";
cout << hex << setfill('0');
for (unsigned int i = 0; i < encoded.length(); i++)
cout << setw(2) << (encoded[i] & 0xff);
cout << "\n";

cout << "decoded: " << decoded << "\n";
}
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector