Why my program shows additional weird characters in decryption?

  aes, c++, cryptography, encryption

I am trying to implement AES in C++. There is no problem in encryption part, decryption is also successful, bu there is some additional weird characters at the end of the decrypted data "aes islemi". It looks like: aes islemi↕U but it should be aes islemi How can I solve this problem? Thanks in advance for your help.

The original codes from: https://github.com/ceceww/aes/blob/master/decrypt.cpp

The related codes:

int main() {
    unsigned char message[] = "aes islemi";
    unsigned char key[16] =
    {
        1, 2, 3, 4,
        5, 6,7, 8,
        9, 10, 11, 12,
        13, 14, 15, 16,
    };
    int originalLen = strlen((const char*)message);
    int lenofsendedmessage = originalLen;

    if (lenofsendedmessage % 16 != 0) //byte 16 değilse yuvarlanacak
        lenofsendedmessage = (lenofsendedmessage / 16 + 1) * 16;

    unsigned char* sendedmessage = new unsigned char[lenofsendedmessage];
    for (int i = 0; i < lenofsendedmessage; i++)
    {
        if (i >= originalLen)sendedmessage[i] = 0;
        else sendedmessage[i] = message[i];

    }
    for (int i = 0; i < lenofsendedmessage; i += 16)//enc
        AES_Encrypt(sendedmessage + i, key);

    cout << "n Result:" << endl;
    for (int i = 0; i < lenofsendedmessage; i++)
    {
        PrintHex(sendedmessage[i]);
        cout << " ";


    }
    cout << endl;
    unsigned char expandedKey[176];
    KeyExpansion(key, expandedKey);

    int messageLen = strlen((const char*)sendedmessage);
    unsigned char* decryptedMessage = new unsigned char[messageLen];
    for (int i = 0; i < messageLen; i += 16) {
        AES_decrypt(sendedmessage + i, expandedKey, decryptedMessage + i);
    }
    cout << "Decrypted message in hex:" << endl;
    for (int i = 0; i < messageLen; i++) {
        cout << hex << (int)decryptedMessage[i];
        cout << " ";
    }
    cout << endl;
    cout << "Decrypted message: ";
    for (int i = 0; i < messageLen; i++) {
        cout << decryptedMessage[i];
   
    }
    _getch();


    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT