HI,
Currently I am working on a embedded device project . It requires Triple DES ECB decryption using 24 byte key.
I am using the code from mbedtls library for the decryption . the problem is for some crypt text i am getting corrupted blocks on decryption.
this is the TEST key i am using
char des3_test_keys1[] = "KEYBS87654321KEY01112013";
and the crypt text is
char des3_test_buf[] = "hZ+JjLErMq4UcVboAGWc0w==";
and the code for the decryption is the following
int Decrypt_Pin(char* value,char* out)
{
int i, j, u, v, ret = 0,part=0,enclen=0;
mbedtls_des_context ctx;
mbedtls_des3_context ctx3;
unsigned char buf[60];// = { "400111996242" };
unsigned char prv[8];
unsigned char iv[8];
unsigned char * base64_out=NULL;
unsigned char dec_block[40];
unsigned int len = 0;
mbedtls_des_init(&ctx);
mbedtls_des3_init(&ctx3);
/*
* ECB mode
*/
std::string vl{ value };
std::string output = base64_decode1(vl);
len = output.length();
part = len / 8;
enclen = len % 8;
if (enclen > 0)
part = part + 1;
memset(dec_block,0x04, len + 1);
dec_block[len + 1] = '\0';
strncpy((char*)dec_block,(char*)output.c_str(),len);
mbedtls_des3_set3key_dec(&ctx3, des3_test_keys);
for (i = 0; i < part; i++)
{
mbedtls_des3_crypt_ecb(&ctx3, (unsigned char*)&dec_block[i*8], &buf[i*8 ]);
}
strcpy(out, (const char*)buf);
out[len] = '\0';
printf("decrypted string :%s", out);
mbedtls_des_free(&ctx);
mbedtls_des3_free(&ctx3);
return(ret);
}