XOR加密数据包无法正确解密

时间:2015-05-20 12:37:40

标签: c++ encryption

我做了一个简单的XOR加密来加密一些数据包,但是解密效果不好......因为我是C ++的初学者,请帮助我......

unsigned char cheimagice[] = { 0xCF, 0x10, 0x4E, 0x3A, 0xC2, 0xD8, 0x5F, 0xAD, 0xE4 };
    unsigned char test[] = { 0xC3, 0x18, 0xC7, 0xBE, 0x08, 0x1B, 0x25, 0xFF, 0x81, 0x55, 0xE0, 0xB5 };
    printf("[Criptare]: \n");
    printf("[INAINTE]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test[0], (unsigned char)test[1], (unsigned char)test[2], (unsigned char)test[3], (unsigned char)test[4], (unsigned char)test[5], (unsigned char)test[6], (unsigned char)test[7], (unsigned char)test[8], (unsigned char)test[9], (unsigned char)test[10], (unsigned char)test[11]);
    unsigned char *test1 = new unsigned char[sizeof(test)];
    unsigned char *test2 = new unsigned char[sizeof(test)];
    // criptare :)
    for (unsigned int i = 0; i < sizeof(test); ++i)
    {
        test1[i] ^= test[i - 1] ^ cheimagice[i % 9];
    }
    printf("[DUPA]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test1[0], (unsigned char)test1[1], (unsigned char)test1[2], (unsigned char)test1[3], (unsigned char)test1[4], (unsigned char)test1[5], (unsigned char)test1[6], (unsigned char)test1[7], (unsigned char)test1[8], (unsigned char)test1[9], (unsigned char)test1[10], (unsigned char)test1[11]);
    // decriptare :)
    printf("[Decriptare]: \n");
    printf("[INAINTE]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test1[0], (unsigned char)test1[1], (unsigned char)test1[2], (unsigned char)test1[3], (unsigned char)test1[4], (unsigned char)test1[5], (unsigned char)test1[6], (unsigned char)test1[7], (unsigned char)test1[8], (unsigned char)test1[9], (unsigned char)test1[10], (unsigned char)test1[11]);
    for (unsigned int i = 0; i < sizeof(test1); ++i)
    {
        test2[i] ^= test1[i - 1] ^ cheimagice[i % 9];
    }
    printf("[DUPA]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test2[0], (unsigned char)test2[1], (unsigned char)test2[2], (unsigned char)test2[3], (unsigned char)test2[4], (unsigned char)test2[5], (unsigned char)test2[6], (unsigned char)test2[7], (unsigned char)test2[8], (unsigned char)test2[9], (unsigned char)test2[10], (unsigned char)test2[11]);

输出:

[Criptare]:
[INAINTE]: 0xC3 0x18 0xC7 0xBE 0x08 0x1B 0x25 0xFF 0x81 0x55 0xE0 0xB5
[DUPA]: 0xE3 0xD3 0x6C 0xFD 0x11 0xD0 0x29 0x88 0x21 0x4E 0x36 0xAE
[Decriptare]:
[INAINTE]: 0xE3 0xD3 0x6C 0xFD 0x11 0xD0 0x29 0x88 0x21 0x4E 0x36 0xAE
[DUPA]: 0x6C 0xF3 0x9D 0x56 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

2 个答案:

答案 0 :(得分:3)

您正在使用未初始化的内存 - test1的内容完全是随机的。我猜你应该替换^ = with =来赋予数组而不是xor。

答案 1 :(得分:-1)

我认为这要归功于@AccessViolation和@vsoftco!

unsigned char cheimagice[] = { 0xCF, 0x10, 0x4E, 0x3A, 0xC2, 0xD8, 0x5F, 0xAD, 0xE4 };
    unsigned char test[] = { 0xC3, 0x18, 0xC7, 0xBE, 0x08, 0x1B, 0x25, 0xFF, 0x81, 0x55, 0xE0, 0xB5 };
    printf("[Criptare]: \n");
    printf("[INAINTE]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test[0], (unsigned char)test[1], (unsigned char)test[2], (unsigned char)test[3], (unsigned char)test[4], (unsigned char)test[5], (unsigned char)test[6], (unsigned char)test[7], (unsigned char)test[8], (unsigned char)test[9], (unsigned char)test[10], (unsigned char)test[11]);
    unsigned char *test1 = new unsigned char[sizeof(test)];
    unsigned char *test2 = new unsigned char[sizeof(test)];
    // criptare :)
    for (unsigned int i = 0; i < sizeof(test); ++i)
    {
        test1[i] = test[i] ^ cheimagice[i % 9];
    }
    printf("[DUPA]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test1[0], (unsigned char)test1[1], (unsigned char)test1[2], (unsigned char)test1[3], (unsigned char)test1[4], (unsigned char)test1[5], (unsigned char)test1[6], (unsigned char)test1[7], (unsigned char)test1[8], (unsigned char)test1[9], (unsigned char)test1[10], (unsigned char)test1[11]);
    // decriptare :)
    printf("[Decriptare]: \n");
    printf("[INAINTE]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test1[0], (unsigned char)test1[1], (unsigned char)test1[2], (unsigned char)test1[3], (unsigned char)test1[4], (unsigned char)test1[5], (unsigned char)test1[6], (unsigned char)test1[7], (unsigned char)test1[8], (unsigned char)test1[9], (unsigned char)test1[10], (unsigned char)test1[11]);
    for (unsigned int i = 0; i < sizeof(test); ++i)
    {
        test2[i] = test1[i] ^ cheimagice[i % 9];
    }
    printf("[DUPA]: 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX 0x%02hX \n", (unsigned char)test2[0], (unsigned char)test2[1], (unsigned char)test2[2], (unsigned char)test2[3], (unsigned char)test2[4], (unsigned char)test2[5], (unsigned char)test2[6], (unsigned char)test2[7], (unsigned char)test2[8], (unsigned char)test2[9], (unsigned char)test2[10], (unsigned char)test2[11]);

非常感谢你的配偶!