I'm trying to perform AES encryption/decryption operation on OpenSSL private.pem file. I'm using polarssl library from here for this operation.
I read private.pem using the following code,
unsigned char * buffer = 0;
long length;
FILE * fp = fopen ("private.pem", "rb");
if (fp)
{
fseek (fp, 0, SEEK_END);
length = ftell (fp);
fseek (fp, 0, SEEK_SET);
buffer = malloc (length);
if (buffer)
{
fread (buffer, 1, length, fp);
}
fclose (fp);
}
printf("buffer is \n %s\n", buffer);
Output:
buffer is
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-128-CBC,440969729B4F3D6AFEF6D2A0ABD1BE50
zRCM0nSfz6hqi/viRhXNT/5X3/i5lZUgePoM+JYzpXSjOrY1DWRKzFfKfOrHUZNx
DqJzcRMll6E/DfFfoQacu+TDyEsQe2kXkByHJKqDEDoG3FZoIMJOssAzjoQ1qseM
cd61c4imktl7MO7wFVb0z+HhqSdc5zOLd9aN25FbQRVXJdROGajQiuLothEfcbrS
OWMNhNKo3FsN1mKhhaJGha5EQnwmcFvc7aslgda0bAMnnvg5o2g3RPXdLlhi7XE6
EdOpqJYVkfUv4zj2/DcMcSqRNixguRez0macMWsYHV00MQrNS7HFILHjb4bhYKUt
ZHeY36dh48/fyIrQAheB+2Tyq4qxWnQTF4XLj6Y4B+NYqvCOP4+s1ERp574ZMvkZ
oK6LmAgcneYobuuzDYsaZczYxQcM9HYIodVO7Si8RmM/XfwS6Xftt3Rg1TFPWXu8
P4LRj8/AHGd6+Tniky9McGpr/7a79+mr97xbG3hjhayzhQc1uq212jgACDy1m7QF
3evChUQINH0uN8URmhlkXs7ORz1nK0EyVGWYQ0+3GVYyQ7AarqgZIm1xemUb6z33
knwpmjLbdglKg2qQLi/yJdRCCaQr8gJ1QE5GrY7GtE+g09RqxmsT7khIdK8TDbqY
+CRQVyHODZaYf21gNUsVgnyzkAqVndNCU14A7DhzXZBMtROjJGeRCsNwHt4aUSW8
ANdaMBNlDRYRXYrxPpqGlK3T4A0xpi/o3Heu/p4K7lCOgX+v7TVvP3dS+dmBSb0P
9XkuMYfmyuEvX63DgaI1K/QLEMyJ0CVzICKaX8vKZ0ervKW+OLfz4VGITp0fsnd3
-----END RSA PRIVATE KEY-----
I'm trying to perform AES encryption operation on buffer. For that, I did the following,
#define AES_BLOCK_SIZE 16
unsigned char private_encrypt[strlen(buffer)];
int j;
for(j=0;j<length/AES_BLOCK_SIZE;++j){
aes_crypt_ecb(&aes,AES_ENCRYPT, buffer+AES_BLOCK_SIZE*j,private_encrypt+AES_BLOCK_SIZE*j);
}
the output of the encryption is:
encrypted private key is ��xvI���8����˷�%�C�Քb��s��&�y�k;.����f���H֕
I don't know any other way to print it. Then I try to decrypt private_encrypt using the following,
unsigned char buf_test[strlen(buffer)];
for(j=0;j<length/AES_BLOCK_SIZE;++j){
aes_crypt_ecb(&aes,AES_DECRYPT, private_encrypt+AES_BLOCK_SIZE*j,buf_test+AES_BLOCK_SIZE*j);
buf_test+AES_BLOCK_SIZE*j);
}
printf("output buffer is %s\n",buf_test);
I get the following output,
output buffer is �Nxړ��f
But shouldn't the output matches with the original private.pem? What I'm doing wrong here?
The same code works if I use plaintext instead of private.pem for encryption/decryption operation using AES.