具有奇怪行为的比特币库函数

Bitcoin library function with strange behavior

大家好,在此先感谢您的光临。

所以我得到了输出以下内容的 .cpp 文件:

密码 =(一堆奇怪的字符)

解密 = Allo mon coco

所以一切都很好...但是如果我将第三条注释行换到第一条注释行之上,我会得到以下输出:

Cipher = (Bunch of strange characters)Allo mon coco

解密 = Allo mon coco

//Like that everything outputs well
#include "Crypto/MainCrypto.h"

namespace Crypto
    {MainCrypto::MainCrypto()
        {const unsigned char Key[16] = "Bob";
        const unsigned char IV[16] = "Random";
        AES128CBCEncrypt m_AES128CBCEncrypt(Key, IV, 0);
        AES128CBCDecrypt m_AES128CBCDecrypt(Key, IV, 0);

        unsigned char ciptxt[16];                   //1
        unsigned char dcptxt[16];                   //2
        unsigned char Data[16] = "Allo mon coco";   //3

        m_AES128CBCEncrypt.Encrypt(Data, 16, ciptxt);
        m_AES128CBCDecrypt.Decrypt(ciptxt, 16, dcptxt);

        std::cout << "Cipher = " <<  ciptxt << std::endl;
        std::cout << "Decryp = " << dcptxt << std::endl;
        }

    MainCrypto::~MainCrypto()
        {}
    }


//Like that, I'm getting unwanted Allo mon coco
#include "Crypto/MainCrypto.h"

namespace Crypto
    {MainCrypto::MainCrypto()
        {const unsigned char Key[16] = "Bob";
        const unsigned char IV[16] = "Random";
        AES128CBCEncrypt m_AES128CBCEncrypt(Key, IV, 0);
        AES128CBCDecrypt m_AES128CBCDecrypt(Key, IV, 0);

        unsigned char Data[16] = "Allo mon coco";   //3
        unsigned char ciptxt[16];                   //1
        unsigned char dcptxt[16];                   //2

        m_AES128CBCEncrypt.Encrypt(Data, 16, ciptxt);
        m_AES128CBCDecrypt.Decrypt(ciptxt, 16, dcptxt);

        std::cout << "Cipher = " <<  ciptxt << std::endl;
        std::cout << "Decryp = " << dcptxt << std::endl;
        }

    MainCrypto::~MainCrypto()
        {}
    }

weird

not weird

我不知道为什么会这样。明白的请给我解释一下。

我不确定比特币库,但我假设它工作正常,所以可能的罪魁祸首是字符数组。

CC++中,字符数组应该以null结尾,否则它将不知道字符串何时结束,所以它会输出字符,直到找到'\0'字符。所以这里发生的基本上是另一个字符数组的内存泄漏,因为它不是空终止的。

要解决此问题,请添加 Data[15] = '[=12=]'

加密库调用正在加载 ciptxt 中的所有 16 个内存位置。

"std::cout << "Cipher = "<< ciptxt << std::endl;"语句期望 ciptxt 以空值终止,但 Encrypt 将非空值放在第 16 位。 std::cout 语句将继续打印字符,直到达到内存中的空值。它正在 dcptxt 字符数组中找到空终止符。