加密后解密的AES-256问题

时间:2018-09-15 08:39:27

标签: c++ encryption

256个加密类

VirtualAES.cpp位于https://pastebin.com/EWrn1GJ3中 VirtualAES.h位于https://pastebin.com/tbC8mR0K

好,现在我需要的是这个,我有此代码

char rawData[36] = {
    0x15, 0x7D, 0x90, 0x5C, 0xA2, 0x15, 0x61, 0xD5, 0xBA, 0x0F, 0x46, 0x18,
    0x53, 0x47, 0xA9, 0x7A, 0xD2, 0x19, 0x69, 0x50, 0x3A, 0x6F, 0xF0, 0x50,
    0xA8, 0x19, 0x9C, 0x0C, 0xE4, 0xC3, 0xB8, 0x37, 0x01, 0xC3, 0x0A, 0x23 
}

我将其用于加密

void AESCrypt(char *rawData0,int size) {

    unsigned char key[KEY_256] = "S#q-}=6{)BuEV[GDeZy>~M5D/P&Q}7>";

    unsigned char plaintext[BLOCK_SIZE];
    unsigned char ciphertext[BLOCK_SIZE];

    aes_ctx_t* ctx;
    virtualAES::initialize();
    ctx = virtualAES::allocatectx(key, sizeof(key));

    int count = 0;
    int index = size / 16; //Outer loop range
    int innerCount = 0;
    int innerIndex = 16; //We encrypt&copy 16 Bytes for once.
    int dataIndex = 0; //Non resetting @rawData index for encryption
    int copyIndex = 0; //Non resetting @rawData index for copying encrypted data.



    for (count; count < index; count++)
    {
        for (innerCount = 0; innerCount < innerIndex; innerCount++)
        {
            plaintext[innerCount] = rawData[dataIndex];
            dataIndex++;
        }

        virtualAES::encrypt(ctx, plaintext, ciphertext);

        for (innerCount = 0; innerCount < innerIndex; innerCount++)
        {
            rawData[copyIndex] = ciphertext[innerCount];
            copyIndex++;
        }
    }

    delete ctx;
}

好吧,它工作得很好,只有当我尝试使用:解密时,

void AESDecrypt(char* toDecrypt, int size)
{
    //Explanation exist in Builder
    unsigned char key[KEY_256] = "S#q-}=6{)BuEV[GDeZy>~M5D/P&Q}7>";

    unsigned char ciphertext[BLOCK_SIZE];
    unsigned char decrypted[BLOCK_SIZE];

    aes_ctx_t* ctx;
    virtualAES::initialize();
    ctx = virtualAES::allocatectx(key, sizeof(key));

    int count = 0;
    int index = size / 16;
    int innerCount = 0;
    int innerIndex = 16;
    int dataCount = 0;
    int copyCount = 0;
    for (count; count < index; count++)
    {
        for (innerCount = 0; innerCount < innerIndex; innerCount++)
        {
            ciphertext[innerCount] = toDecrypt[dataCount];
            dataCount++;
        }

        virtualAES::decrypt(ctx, ciphertext, decrypted);

        for (innerCount = 0; innerCount < innerIndex; innerCount++)
        {
            toDecrypt[copyCount] = decrypted[innerCount];
            copyCount++;
        }
    }

    delete ctx;
}

我需要对rawData中的每个字节进行加密,但是在某个地方出错了,解密不给我工作字节数组,就像加密之前一样,我不知道为什么有人可以帮助我吗?

在enc之前:15 7D 90 5C A2 15 61 D5 BA 0F 46 18 53 47 A9 7A D2 19 69 50 3A 6F F0 50 A8 19 9C 0C E4 C3 B8 37 01 C3 0A 23 04 36 80 F6 42 CF 91 38 C1 C3 56 16 enc之后:84 80 78 4A E5 49 E5 C3 64 E1 09 00 62 28 93 06 43 4E A5 3A 81 37 33 DA 6E E2 CD 70 2C 9B 91 74 81 75 8B 69 C7 42 69 13 99 C4 FF 82 6E 1D 08 42并解密回去:4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 B8 00 00 00 00 00 00 00 00 40 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00我使用AESCrypt(rawData,48); AESDecrypt(rawData,48);

0 个答案:

没有答案