Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C printf显示器"-858993460“;或╠╠╠╠╠╠╠╠&“;;这些是什么?_C_Printf_Display - Fatal编程技术网

C printf显示器"-858993460“;或╠╠╠╠╠╠╠╠&“;;这些是什么?

C printf显示器"-858993460“;或╠╠╠╠╠╠╠╠&“;;这些是什么?,c,printf,display,C,Printf,Display,我试图建立一个程序,加密密码,然后做相反的过程(告诉正确的密码从一个加密的) 为此,我: 密码(char[]type) 一种加密密钥(vector-int[]type),其长度与密码的char长度相同 两个步骤(也放在int step[2]类型的向量中) 要求是必须使用以下两个步骤构建加密过程: 第一个(步骤[0]中的值)用于将从密码char的第一个位置开始的值(ASCII)添加到加密密钥向量的第一个位置,步骤数等于第一步步骤[0] 例如,将char-password[0]添加到int-

我试图建立一个程序,加密密码,然后做相反的过程(告诉正确的密码从一个加密的)

为此,我:

  • 密码(
    char[]
    type)

  • 一种加密密钥(vector-
    int[]
    type),其长度与密码的
    char
    长度相同

  • 两个步骤(也放在
    int step[2]
    类型的向量中)

要求是必须使用以下两个步骤构建加密过程:

  • 第一个(步骤[0]中的值)用于将从密码
    char
    的第一个位置开始的值(ASCII)添加到加密密钥向量的第一个位置,步骤数等于第一步
    步骤[0]
例如,将
char-password[0]
添加到
int-key[0]
,然后将
char-password[1]
添加到
int-key[1]
,以此类推,步骤数等于
步骤[0]
中的值

  • 第二步(
    步骤[1]
    )从密码
    字符的ASCII值的相应位置减去加密密钥的值,步骤数等于第二步(
    步骤[1]
例如,从
int-key[5]
中减去
char-password[5]
,然后从
int-key[6]
中减去
char-password[6]
,以此类推,步骤数等于
步骤[1]
中的值

然后,该过程重复,直到密码字符长度结束

我构建了一个函数,如下所示,可以实现这一点(对多个步骤进行加法,然后对多个其他步骤进行减法,并重复该过程,直到密码字符结束-加密密钥向量的长度与密码字符的长度相同)

结果被放入一个新的向量(用于加密)或一个新的
char
(用于查找密码的反向过程)

为什么显示“-858993460”?


此外,如果使用以下代码尝试
printf
在反向过程中显示密码的
char
(将加密密码转换为可读密码)

void decriptareFinal(int encryptedPass5[255], 
                     int longKey2[255], 
                     int b5, 
                     int steps3[2]) {   
    char Password1[255];
    int a = steps3[0], 
        b = steps3[1], 
        n = b5, 
        i2, 
        ap = a, 
        bp = b;

    for (i2 = 0; i2 < n; i2++) {
        if (ap > 0) {
            Password1[i2] = encryptedPass5[i2] - longKey2[i2];
            ap--;
        }
        else if (bp > 0) {
            Password1[i2] = longKey2[i2] - encryptedPass5[i2];
            bp--;
        }
        else if (ap == 0 && bp == 0) {
            ap = a;
            bp = b;
        }
    }

    int j2;
    printf("\n");
    for (j2 = 0; j2 < b5; j2++)
        printf("%c", Password1[j2]);
}
什么是“╠"?

这个“╠╠╠╠╠╠╠╠“是一个不正确的显示(其余的“cő~ypaXOF”是正确的)。


此外,我尝试手动将添加和减去表单
[0]
到字符串/向量的末尾,如果这样做,则不会出现
-858993460类型或
类型的错误消息╠╠╠╠╠╠╠╠出现。
它工作正常,这告诉我
char
(密码)或
int
(键)中的值是正确的

如果这很重要的话,我使用的是64位Windows,但我使用的是Visual Studio。
还有,我有一台Mac电脑。我也有同样的问题,但不是
-858993460
╠╠╠╠╠╠╠╠我收到其他消息(例如
0
而不是
-858993460
)。

我对您的代码做了很多更改。以下是列表:

  • 为标准库添加了更多的
    #包括
    标记(我猜您已经有了一些,但没有发布它们)
  • 将函数参数更改为
    char*
    而不是
    char[]
    ,在函数参数中使用
    char
    数组不是常规做法,我甚至不确定它们在代码中的工作方式
  • 去掉了不必要的/额外的变量:
    ap
    bp
    和其他一些变量。因为C是传递值,所以
    步骤
    变量不需要临时变量
  • 修正了变量名。你的很多变量名都不直观/怪异,我觉得我必须把它们改成更准确的名称
  • 使用
    malloc
    (动态内存分配),而不是将
    char
    数组放在堆栈上。这更灵活,减少了发生这些问题的机会,但也增加了发生SEGCHARS故障的可能性
  • 修复了您的加密/解密算法,因为如果刚刚完成这两个步骤(
    ap==0&&bp==0
    ),它会使
    char
    保持不变
  • 修复了一些语法错误
  • 添加了一些调试
    fprintf
    语句,以便您可以看到发生了什么
至于你那古怪的╠ 字符,我不明白为什么你的代码会打印出来,除了可能你有一些内存错误,或者你的环境在Unicode和ASCII之间切换。坦率地说,我不想弄明白,因为你的代码有点乱,我不认为你发布了所有的代码,因为我第一次运行它时它有编译错误

我还向
malloc
'd字符串的末尾添加了一些空终止字符(
'\0'
),以便在调试时正确打印字符串。
总之,够了。下面是代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *criptareFinal(char *plaintext, int *key, int len, int *steps) {
    char *ciphertext = malloc(len + 1);
    int a = steps[0], b = steps[1], n = len;

    for (int i = 0; i < n; i++) {
        if (a == 0 && b == 0) {
            a = steps[0];
            b = steps[1];
        }

        if (a > 0) {
            ciphertext[i] = key[i] + plaintext[i];
            a--;
        }
        else if (b > 0) {
            ciphertext[i] = key[i] - plaintext[i];
            b--;
        }
    }

    ciphertext[len] = '\0';

    fprintf(stderr, "\n");
    fprintf(stderr, "ENCRYPTION: ");
    for (int i = 0; i < n; i++)
        fprintf(stderr, "%d ", ciphertext[i]);

    return ciphertext;
}

char *decriptareFinal(char *ciphertext, int *key, int len, int *steps) {   
    char *plaintext = malloc(len + 1);
    int a = steps[0], b = steps[1], n = len;

    for (int i = 0; i < n; i++) {
        if (a == 0 && b == 0) {
            a = steps[0];
            b = steps[1];
        }

        if (a > 0) {
            plaintext[i] = ciphertext[i] - key[i];
            a--;
        }
        else if (b > 0) {
            plaintext[i] = key[i] - ciphertext[i];
            b--;
        }
    }

    plaintext[len] = '\0';

    fprintf(stderr, "\n");
    fprintf(stderr, "DECRYPTION: ");
    for (int i = 0; i < n; i++)
        fprintf(stderr, "%d ", plaintext[i]);

    return plaintext;
}

int main() {
    char *toEncrypt = "Hello World!";
    int steps[] = {2, 3};
    int key[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};

    int len = strlen(toEncrypt);

    fprintf(stderr, "INITIAL: ");
    for (int i = 0; i < len; i++)
        fprintf(stderr, "%d ", toEncrypt[i]);

    fprintf(stderr, "\n");
    char *encrypted = criptareFinal(toEncrypt, key, len, steps);
    fprintf(stderr, "\n");
    fprintf(stderr, "ENCRYPTED: %s\n", encrypted);
    char *decrypted = decriptareFinal(encrypted, key, len, steps);
    fprintf(stderr, "\n");
    fprintf(stderr, "DECRYPTED: %s\n", decrypted);

}

如果您有任何问题,请告诉我。

在第一个函数中,您将
i
0
迭代到
n
一次一步。但是,并非通过循环体的所有路径都将任何内容分配给
encryptedPass[i]
-对于最后一个
else if
和所有
if
条件都不匹配的情况都是如此。对于第二个函数也是如此,但是对于
密码1[i]

但是,在这些函数的末尾,您可以无条件地打印这些数组中的所有值,这意味着它们是未初始化的值。(您可以迭代到未知值
b6
b7
,而您可能应该迭代到在
void decriptareFinal(int encryptedPass5[255], 
                     int longKey2[255], 
                     int b5, 
                     int steps3[2]) {   
    char Password1[255];
    int a = steps3[0], 
        b = steps3[1], 
        n = b5, 
        i2, 
        ap = a, 
        bp = b;

    for (i2 = 0; i2 < n; i2++) {
        if (ap > 0) {
            Password1[i2] = encryptedPass5[i2] - longKey2[i2];
            ap--;
        }
        else if (bp > 0) {
            Password1[i2] = longKey2[i2] - encryptedPass5[i2];
            bp--;
        }
        else if (ap == 0 && bp == 0) {
            ap = a;
            bp = b;
        }
    }

    int j2;
    printf("\n");
    for (j2 = 0; j2 < b5; j2++)
        printf("%c", Password1[j2]);
}
côő~ypaXOF╠╠╠╠╠╠╠╠
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *criptareFinal(char *plaintext, int *key, int len, int *steps) {
    char *ciphertext = malloc(len + 1);
    int a = steps[0], b = steps[1], n = len;

    for (int i = 0; i < n; i++) {
        if (a == 0 && b == 0) {
            a = steps[0];
            b = steps[1];
        }

        if (a > 0) {
            ciphertext[i] = key[i] + plaintext[i];
            a--;
        }
        else if (b > 0) {
            ciphertext[i] = key[i] - plaintext[i];
            b--;
        }
    }

    ciphertext[len] = '\0';

    fprintf(stderr, "\n");
    fprintf(stderr, "ENCRYPTION: ");
    for (int i = 0; i < n; i++)
        fprintf(stderr, "%d ", ciphertext[i]);

    return ciphertext;
}

char *decriptareFinal(char *ciphertext, int *key, int len, int *steps) {   
    char *plaintext = malloc(len + 1);
    int a = steps[0], b = steps[1], n = len;

    for (int i = 0; i < n; i++) {
        if (a == 0 && b == 0) {
            a = steps[0];
            b = steps[1];
        }

        if (a > 0) {
            plaintext[i] = ciphertext[i] - key[i];
            a--;
        }
        else if (b > 0) {
            plaintext[i] = key[i] - ciphertext[i];
            b--;
        }
    }

    plaintext[len] = '\0';

    fprintf(stderr, "\n");
    fprintf(stderr, "DECRYPTION: ");
    for (int i = 0; i < n; i++)
        fprintf(stderr, "%d ", plaintext[i]);

    return plaintext;
}

int main() {
    char *toEncrypt = "Hello World!";
    int steps[] = {2, 3};
    int key[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17};

    int len = strlen(toEncrypt);

    fprintf(stderr, "INITIAL: ");
    for (int i = 0; i < len; i++)
        fprintf(stderr, "%d ", toEncrypt[i]);

    fprintf(stderr, "\n");
    char *encrypted = criptareFinal(toEncrypt, key, len, steps);
    fprintf(stderr, "\n");
    fprintf(stderr, "ENCRYPTED: %s\n", encrypted);
    char *decrypted = decriptareFinal(encrypted, key, len, steps);
    fprintf(stderr, "\n");
    fprintf(stderr, "DECRYPTED: %s\n", decrypted);

}
INITIAL: 72 101 108 108 111 32 87 111 114 108 100 33

ENCRYPTION: 73 103 -105 -104 -106 38 94 -103 -105 -98 111 45
ENCRYPTED: Igùÿû&^Öù₧o-

DECRYPTION: 72 101 108 108 111 32 87 111 114 108 100 33
DECRYPTED: Hello World!
for (i2 = 0; i2 < n; i2++) {
if (ap > 0) {
    Password1[i2] = encryptedPass5[i2] - longKey2[i2];
    ap--;
}
else if (bp > 0) {
    Password1[i2] = longKey2[i2] - encryptedPass5[i2];
    bp--;
}
else if (ap == 0 && bp == 0) {
    ap = a;
    bp = b;
    i = i - 1;
}
for (i = 0; i < n; i++) {
if (ap > 0) {
    encryptedPass[i] = longKey1[i] + password4[i];
    ap--;
}
else if (bp > 0) {
    encryptedPass[i] = longKey1[i] - password4[i];
    bp--;
}
else if (ap == 0 && bp == 0) {
    ap = a;
    bp = b;
    i = i - 1;
}