在 C 中解码密文时出现大小问题

Size problem while decoding ciphered text in C

[编辑]

这是需要解码的密文:

bURCUE}__V|UBBQVT

我的解码器可以成功获取密文但将其转换为某个点。编码消息的其余部分是乱码。我检查了缓冲区的大小和字符指针,似乎都是正确的,我找不到缺陷

我希望看到的消息:

SecretLongMessage

屏幕上的解密消息如下所示:

SecretLong|drs`fe

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUZZ_SIZE 1024


char* encryptDecrypt(const char* toEncrypt, int length)
{
    char key[] = "1011011011";
    char* output = malloc(length + 1);
    output[length] = '[=11=]'; //buffer

    for (int i = 0; i < length; i++)
    {
        output[i] = toEncrypt[i] ^ key[i % (sizeof(key)/sizeof(char))];
    }
    return output;
}

int main(int argc, char* argv[])
{
    char buff[BUZZ_SIZE];
    FILE *f;
    f = fopen("C:\Users\Dell\source\repos\XOR\XOR\bin\Debug\cipher.txt", "r"); // read mode
    fgets(buff, BUZZ_SIZE, f);
    printf("Ciphered text: %s, size = %d\n", buff,sizeof(buff));
    fclose(f);

    char* sourceString = buff;

    //Decrypt
    size_t size = strlen(sourceString);

    char* decrypted = encryptDecrypt(buff, size);
    //printf("\nsize = %d\n",size);
    printf("\nDecrypted is: ");
    printf(decrypted);
    // Free the allocated buffers

    return 0;
}

这是我给出密码的 C# 代码

String szEncryptionKey = "1011011011";
        public Form1()
        {
            InitializeComponent();
        }
        string EncryptOrDecrypt(string text, string key)
        {
            var result = new StringBuilder();

            for (int c = 0; c < text.Length; c++)
            {
                // take next character from string
                char character = text[c];

                // cast to a uint
                uint charCode = (uint)character;

                // figure out which character to take from the key
                int keyPosition = c % key.Length; // use modulo to "wrap round"

                // take the key character
                char keyChar = key[keyPosition];

                // cast it to a uint also
                uint keyCode = (uint)keyChar;

                // perform XOR on the two character codes
                uint combinedCode = charCode ^ keyCode;

                // cast back to a char
                char combinedChar = (char)combinedCode;

                // add to the result
                result.Append(combinedChar);
            }

            return result.ToString();
        }


        private void Button1_Click(object sender, EventArgs e)
        {
            String str = textBox1.Text;

            var cipher = EncryptOrDecrypt(str, szEncryptionKey);
            System.IO.File.WriteAllText(@"C:\Users\Dell\source\repos\XOR\XOR\bin\Debug\cipher.txt", cipher);

        }

您想使用

中的所有字符
char key[] = "1011011011";

用于加密。但是数组 key 包含一个终止符 '[=14=]',当您使用

时,它包含在计算中
key[i % (sizeof(key)/sizeof(char))]

因为 sizeof(key) 包括终止 '[=14=]'.

您可以使用 strlen 来计算字符串长度或使用 key[i % (sizeof(key)/sizeof(char))-1] 或将数组初始化为

char key[] = {'1', '0', '1', '1', '0', '1', '1', '0', '1', '1' };

省略终止符 '[=14=]'。在后一种情况下,您可以使用 sizeof 来计算原始代码中的密钥索引

将 C# 代码添加到问题后,很明显加密在密钥中不包含 '[=14=]'key.Length 相当于 C 中的 strlen(key),而不是 sizeof(key)

顺便说一句:C# 中的变量名 String szEncryptionKey = "1011011011"; 具有误导性,因为它不是 C 中的零终止字符串。

注意:strlen(key) 与您的情况下的 sizeof(key)-1 相同,因为您没有指定数组大小并将数组初始化为字符串。在其他情况下可能不一样。