在我的文本编码器程序中找不到错误
Can't find the error in my text encoder program
只是为了好玩,我一直在研究一个文本编码程序,它会打乱文本。 我已经让它几乎完美无缺地工作,但似乎无法解决一个错误。 基本上,文本在大多数情况下都会正确编码和解码,除了偶尔,程序会解码一个字母不合适的字符串。 代码有点丑陋,我没有尝试清理它,因为我仍在解决错误,所以我对此表示歉意。 它在部分中的复制和粘贴也很差,因此在我使其正常工作后,优化的机会对我来说将是巨大的。 我有一种感觉,我知道错误在哪里,但是进行我认为需要进行的更正会导致无休止的循环。 以下是底部存储在注释中的一些输出的代码:
#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% LZ$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% LZ$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)没有正确转换并丢失了首字母"a"。 我认为错误出在 void FillKey
函数中 - 在 if 语句中if (Key3[j] == RandomNumber)
我创建了一个新的随机数并设置j=0
如果任何新生成的随机数与键中以前的任何随机数完全相同(显然不能有相同的字符两次)。 所以我的想法是,j 被设置为 0,但在 for 循环之后,它被递增并变成 1。 所以程序没有检查Key3[3]
中的重复项,所以我将语句更改为 j=-1
,以预测它并在 for 循环后将其恢复为零,但随后程序卡住了并且永远不存在循环。
任何建议将不胜感激。
这里有错误:
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;
}
然后在调用函数时keysize
传递ASCII_RANGE_SIZE_94
这也可以通过异或运算符完成
#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";
}
相关文章:
- 我在c ++矩阵计算器程序中找不到错误
- 我在代码中找不到错误以找到最大的素因数?
- 在我的应用逻辑中找不到错误
- 在模板类中找不到错误
- 双重释放或损坏错误(找不到错误?
- 在我的 cpp 或头文件中找不到错误,类构建失败
- 在哈希表复制构造函数中找不到错误
- 找不到错误。C++,字符串
- 8谜题代码.我的队列没有填充,我找不到错误的位置
- C++中的预期表达式,但找不到错误
- C++ 将布尔隐式转换为 int 和算术,删除分支,找不到错误
- 找不到错误"invalid conversion from char to const char*"的解决方案
- 在以下代码中找不到错误:- 在 try() 之前预期未限定 id
- 在重载运算符中找不到错误<
- C++ 中的分段错误(核心转储) 找不到错误
- 在使用set_difference算法时找不到错误
- 在 while 循环中找不到错误?
- C++错误LNK2001,找不到错误
- 在我的代码中找不到错误....分段错误
- LNK2005(已定义)找不到错误