C++ Can';在我的文本编码器程序中找不到错误

C++ Can';在我的文本编码器程序中找不到错误,c++,debugging,text,encoder,C++,Debugging,Text,Encoder,只是为了好玩,我一直在研究一个文本编码程序,可以对文本进行置乱。我已经让它几乎完美地工作,但似乎无法解决一个bug。基本上,文本的大部分编码和解码都是正确的,除了偶尔,程序会解码一个字母不正确的字符串。代码有点难看,我还没有尝试清理它,因为我还在解决bug,所以我对此表示歉意。它也是拙劣的复制和粘贴在部分,所以优化的机会将是巨大的为我以后下来后,我得到正确的工作。我有一种感觉,我知道错误在哪里,但我认为需要进行的纠正会导致一个无休止的循环。下面是一些底部输出存储在注释中的代码: #include

只是为了好玩,我一直在研究一个文本编码程序,可以对文本进行置乱。我已经让它几乎完美地工作,但似乎无法解决一个bug。基本上,文本的大部分编码和解码都是正确的,除了偶尔,程序会解码一个字母不正确的字符串。代码有点难看,我还没有尝试清理它,因为我还在解决bug,所以我对此表示歉意。它也是拙劣的复制和粘贴在部分,所以优化的机会将是巨大的为我以后下来后,我得到正确的工作。我有一种感觉,我知道错误在哪里,但我认为需要进行的纠正会导致一个无休止的循环。下面是一些底部输出存储在注释中的代码:

#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:

*/
#包括
使用名称空间std;
void-FillKey(char-Key2[]);
void Encrypt();
void EncryptMessage(char-Key3[],char-Input2[],char-MessageEncrypted2[]);
void Decrypt();
无效解密消息(char-Key4[],char-CodedInput2[],char-NormalOutput2[]);
char ReturnDecodedCharacter(char键5[],char字符);
const int STRING_SIZE=500;
const int ASCII_RANGE_SIZE_94=94//从ASCII值32到126=94的范围
常量int SHIFT_SET_32=32//将范围向上移动32个值,从(0-93)移动到(32到126)
int main()
{
智力选择;
做
{
cout此处有错误:

for (KeySpot=0; KeySpot<=ASCII_RANGE_SIZE_94; ++KeySpot)
为了使代码更易于阅读,您可以编写如下代码:

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;
}

哇,这就解决了!好吧,再加上将j=0更改为j=-1。之前,将j=0更改为j=-1会导致程序陷入循环,但在看到您的修复并实现了j=-1之后,现在一切都正常了。至少在我的测试中似乎是这样。我还将看看您发送的代码清理。非常感谢!我怀疑我是否能以编码为职业,尤其是如果我不能抓住自己的逻辑错误的话!顺便问一下,这是干什么用的。它看起来很奇怪,不应该用于任何实际应用程序。你可以使用XOR运算符(
^
)将消息与密码密钥相结合,只要密码密钥比消息长,并且密码密钥只使用一次,该方法将是安全的。该方法将产生二进制结果,但有方法将其转换为文本。这不是家庭作业问题。我只是想做。我在学校和大学以外学习编程在我的家庭作业中,我用我自己的程序来补充这些。我只是想制作一个程序并尝试制作它。只是为了不断地使用我们学到的编程信息。我意识到这可能是一个糟糕的方法,但我的目标是制作一般的编码程序,就像我过去无法做到的那样。这看起来像是一个修改caesar或vigenere cipher的n。我添加了一个现代密码学中使用的XOR方法。虽然根本不应该使用
rand
。这是我第一次处理加密,所以我对这一领域不熟悉。我们为什么要使用rand()?太明显了吗?
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;
}
#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";
}