首页 > 解决方案 > 加密后解密的 AES-256 问题

问题描述

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 的每个字节,但是在某个地方出错了,解密没有像加密之前那样给我一个工作字节数组,我不知道为什么有人可以帮助我?谢谢

Before 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 

After 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

Decrypted back : 
4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 
B8 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

我用AESCrypt(rawData, 48); AESDecrypt(rawData,48);

标签: c++encryption

解决方案


推荐阅读