为了好玩,我一直致力于一个文本编码程序,它会加扰文本。我已经接近完美地工作,但似乎无法解决一个错误。基本上,文本将在大多数情况下正确编码和解码,除了偶尔,程序将使用一个字母解码字符串。代码有点难看,我还没有尝试清理它,因为我还在处理这些错误,所以我为此道歉。它也很难复制并粘贴在各个部分,因此在我使其正常工作后,优化的机会对我来说将是巨大的。我有一种感觉,我知道错误在哪里,但我认为需要进行修正会导致无限循环。以下是存储在注释中的底部有一些输出的代码:
#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)没有正确转换并丢失了最初的&#39; a&#39;。我认为错误在void FillKey
函数中 - 在if语句if (Key3[j] == RandomNumber)
内部我创建一个新的随机数并设置j=0
如果任何新生成的随机数与之前的任何一个完全相同密钥中的密钥(显然不能有两次相同的字符)。所以我的想法是,j被设置为0,但在for循环之后,它会增加并变为1.因此程序不会检查Key3[3]
中的重复项,所以我将语句更改为{ {1}},预测它并在for循环后将其恢复为零,但随后程序卡住并且从不存在循环。
任何建议都将不胜感激。
答案 0 :(得分:1)
这里有错误:
for (KeySpot=0; KeySpot<=ASCII_RANGE_SIZE_94; ++KeySpot)
应该是:
for (KeySpot=0; KeySpot < ASCII_RANGE_SIZE_94; ++KeySpot)
因为keysize是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";
}