cencryptioncryptographypolarssl

Is Encryption-Decryption irreversable


i am trying to handle a problem using encryption and decryption algorithms, i used below program to test my requirements and i realised an odd problem.

i am using polarssl for my encryption and decryption needs.

As you might see first i encrypted "plain" variable with my "iv" and "key" and find a result. second i decrypted an array with those "iv" and "key".

When i decrypted "encryptedValue" i found "plain" array, but when i encrypted "plain" i cannot find "encryptedValue".

What could be the problem here?

#include <stdio.h>
#include <polarssl/aes.h>

const int ENCRIPTION = 1;
const int DECRIPTION = 0;

void printByteArray(unsigned char * array, int length) {
    int i = 0;
    for (i = 0; i < length; i++) {
        printf("%x ", array[i]);
    }
    printf("\n");
}

void encrypt() {
    unsigned char o[32];


    unsigned char key[16] = { 0x03, 0xC9, 0xD8, 0xE6, 0x01, 0xA5, 0x05,
            0x9F, 0x11, 0xBF, 0x44, 0x9D, 0xF9, 0x55, 0x18, 0xED

    };

    unsigned char iv[16] = { 0x03, 0xB5, 0x62, 0x57, 0xC8, 0x69, 0x22, 0x89,
            0xF4, 0x96, 0x2B, 0x05, 0x44, 0x2B, 0xD0, 0xA7

    };

    unsigned char plain[32]=
    {
      0xA2, 0xD8, 0x69, 0x9C, 0x77, 0x73, 0xC7, 0x5E, 0x1D, 0x3B, 0x83, 0x26, 0x6E, 0x2F, 0x35, 0x30,
      0x9D, 0xF0, 0xF2, 0xE5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    aes_context aesCtx;

    aes_setkey_dec(&aesCtx, key, 128);
    aes_crypt_cbc(&aesCtx, ENCRIPTION, 32, iv, plain, o);

    printf("Encryption result, encrypted value:\n");
    printByteArray(o, 32);
}

decrypt() {

    unsigned char o[32];

    unsigned char key[16] = { 0x03, 0xC9, 0xD8, 0xE6, 0x01, 0xA5, 0x05,
            0x9F, 0x11, 0xBF, 0x44, 0x9D, 0xF9, 0x55, 0x18, 0xED

    };

    unsigned char iv[16] = { 0x03, 0xB5, 0x62, 0x57, 0xC8, 0x69, 0x22, 0x89,
            0xF4, 0x96, 0x2B, 0x05, 0x44, 0x2B, 0xD0, 0xA7

    };

    unsigned char encryptedValue[32] = { 0x11, 0x22, 0x07, 0x86,
            0xA2, 0xD3, 0xED, 0x95, 0xB9, 0x14, 0xC0, 0x57, 0xF7, 0xAF, 0x5F,
            0xDC, 0x93, 0x66, 0x77, 0x68, 0x44, 0x12, 0x9F, 0x1B, 0x72, 0x6A,
            0xEA, 0x51, 0xB8, 0xF7, 0x1D, 0xA4 };

    aes_context aesCtx;

    aes_setkey_dec(&aesCtx, key, 128);
    aes_crypt_cbc(&aesCtx, DECRIPTION, 32, iv, encryptedValue,
            o);

    printf("Decryption result, plain array:\n");
    printByteArray(o, 32);

}

int main() {

    encrypt();
    decrypt();
}

Solution

  • There is a mistake in this part of the encrypt() function:

    aes_setkey_dec(&aesCtx, key, 128);
    aes_crypt_cbc(&aesCtx, ENCRIPTION, 32, iv, plain, o);
    

    If you check the API documentation for AES, you'll see that there are two different functions for setting the key for encryption and decryption.

    You should use aes_setkey_enc() instead of aes_setkey_dec() when you want to set the encryption key.

    Then the result in o is the same as what you are decrypting.