static int des_setkey_dec_wrap( void *ctx, const unsigned char *key, unsigned int key_length ) { ((void) key_length); return des_setkey_dec( (des_context *) ctx, key ); }
int decrypt_doc(unsigned char* data, int size) { data += 0x10; // Skip dummy PGD header. size -= 0x10; // Adjust size. unsigned char *out = new unsigned char[size]; // Perform DES CBC decryption. des_context ctx; des_setkey_dec(&ctx, des_key); des_crypt_cbc(&ctx, DES_DECRYPT, size, des_iv, data, out); // Check for "DOC" header in the decrypted data. if ((out[0] == 0x44) && (out[1] == 0x4F) && (out[2] == 0x43) && (out[3] == 0x20)) { // Copy back the decrypted data. memcpy(data - 0x10, out, size); delete[] out; return 0; } else { delete[] out; return -1; } }
static mrb_value mrb_des_decrypt(mrb_state *mrb, mrb_value self) { mrb_value mode, key, source, dest, iv; unsigned char output[100]; des_context ctx; mrb_int len=8; memset(output, 0, sizeof(output)); mrb_get_args(mrb, "SSSS", &mode, &key, &source, &iv); des_init(&ctx); des_setkey_dec(&ctx, RSTRING_PTR(key)); if (mrb_str_cmp(mrb, mode, mrb_str_new(mrb, "CBC", 3)) == 0) { des_crypt_cbc(&ctx, DES_DECRYPT, RSTRING_LEN(source), RSTRING_PTR(iv), RSTRING_PTR(source), output); len = RSTRING_LEN(source); } else if (mrb_str_cmp(mrb, mode, mrb_str_new(mrb, "ECB", 3)) == 0) { des_crypt_ecb(&ctx, RSTRING_PTR(source), output); } else { des_free(&ctx); return mrb_nil_value(); } des_free(&ctx); return mrb_str_new(mrb, output, len); }
void eid2_decrypt_block(u8 *block, u32 length) { u8 tmp[0x08], iv[0x08]; int block_size = 8; int num_blocks = (int)(length / block_size); memcpy(iv, eid2_des_iv, 0x08); if(num_blocks > 0) { int i, j; des_context ctx; des_setkey_dec(&ctx, eid2_des_key); for(i = 0; i < num_blocks - 1; ++i) { u8 *ptr = block + i * block_size; memcpy(tmp, ptr, block_size); des_crypt_ecb(&ctx, ptr, ptr); if(i > 0) { for(j = 0; j < block_size; ++j) ptr[j] = ptr[j] ^ iv[j]; } memcpy(iv, tmp, block_size); } } }
void test_suite_des_decrypt_ecb( char *hex_key_string, char *hex_src_string, char *hex_dst_string ) { unsigned char key_str[100]; unsigned char src_str[100]; unsigned char dst_str[100]; des_context ctx; memset(key_str, 0x00, 100); memset(src_str, 0x00, 100); memset(dst_str, 0x00, 100); memcpy(key_str, hex_key_string, strlen(hex_key_string) + 1); memcpy(src_str, hex_src_string, strlen(hex_src_string) + 1); des_setkey_dec( &ctx, key_str); TEST_ASSERT( des_crypt_ecb( &ctx, src_str, dst_str ) == 0 ); memcpy(hex_dst_string, dst_str, strlen(dst_str) + 1); }