0

How can I decrypt an encrypted message in AES which is of alphanumeric form? In the code below, encryption gives alphanumeric sequences, but decryption produces the wrong plaintext.

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 lenofPaddedMessage = originalLen;

if (lenofPaddedMessage % 16 != 0)
    lenofPaddedMessage = (lenofPaddedMessage / 16 + 1) * 16;

unsigned char *paddedMessage = new unsigned char[lenofPaddedMessage];
for (int i = 0; i < lenofPaddedMessage; i++) {
    if (i >= originalLen)
        paddedMessage[i] = 0;
    else
        paddedMessage[i] = message[i];
}
for (int i = 0; i < lenofPaddedMessage; i += 16)
    AES_Encrypt(paddedMessage + i, key);

cout << " Encrypted message:" << endl;
for (int i = 0; i < lenofPaddedMessage; i++) {
    cout << (paddedMessage[i]) << endl;
    cout << " ";
}

unsigned char expandedKey[176];

int messageLen = strlen((const char *)paddedMessage);
unsigned char *decryptedMessage = new unsigned char[messageLen];
for (int i = 0; i < messageLen; i += 16) {
    AESDecrypt(paddedMessage + 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];
    cout << endl;
}
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • 1
    If the ciphertext is in "alphanumeric" form, you need to decode it to binary before decrypting it. The manner in which it is decoded is *directly* related to the manner in which it was *encoded*, basic hex, base64, whatever. We don't know that, and if you don't, you need to find out. Unrelated, I find your crypto-library inconsistent usage of underscores (AES_Encrypt vs AESDecrypt) questionable. – WhozCraig Apr 09 '21 at 09:38
  • Can I decrypt it if the encrypted message is in hex format?WhozCraig – Aditya Karthik Apr 09 '21 at 15:04
  • Removed previous comment as it wasn't as nice as it should be. Please do try everything to make your code as readable as possible. One easy thing is to make sure that it is indented correctly. Another way is to split it up into well readable method calls (for instance padding the message could be a separate call). – Maarten Bodewes Apr 11 '21 at 13:37
  • Took some time to find out how, but I reflowed using `clang-format -i -style="{BasedOnStyle: llvm, IndentWidth: 4}" yourcode.cpp`. – Maarten Bodewes Apr 11 '21 at 13:49

1 Answers1

0
int messageLen = strlen((const char *)paddedMessage);

The encrypted padded message consists of randomized bytes, not characters. It may well be that these bytes contain a zero, and then strlen won't work.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • Note to moderator, this is about [this comment on a question of Andrew](https://stackoverflow.com/questions/67013169/can-a-password-be-cracked-with-this-information#comment118491143_67013169). There are deleted comments in the history. – Maarten Bodewes Apr 11 '21 at 13:53