dear all, i am using openssl 0.9.8.e in M$ and compile in MSYS. i just want to encrypt a string and then decrypt later. for the encryption, it is always ok. but for decryption, it failed randomly. the word "randomly" means that for some cases, the decryption is ok, but for other cases, it failed and always gave the error information "Error in Decrypt final". for the failure situation, there are two cases: one is that the decrypted string is correct compared with the source string; the other is that the decrypted string is wrong since the final block is lost.
i really feel frustrated due to the random failure and i can't find where the error is. below are the encryption and decryption function. any help would be very appreciated. ===============>encryption function unsigned char *encode_string (char *data, unsigned char key[], unsigned char iv[], int *output_len) { unsigned char *encrypt; int buff_size, input_len, final_part; EVP_CIPHER_CTX ctx; #ifdef REG_DEBUG FILE *fp; #endif input_len = strlen(data) + 1; buff_size = (input_len / BUFFER + 1) * BUFFER + EVP_MAX_BLOCK_LENGTH; /* if(input_len + IV_LEN > BUFFER) buff_size = input_len + IV_LEN + EVP_MAX_BLOCK_LENGTH; else buff_size = BUFFER + EVP_MAX_BLOCK_LENGTH; */ encrypt = (unsigned char *)malloc(buff_size * sizeof(char)); memset(encrypt, 0, buff_size); EVP_CIPHER_CTX_init(&ctx); EVP_EncryptInit(&ctx, EVP_bf_cbc(), key, iv); /* EVP_CIPHER_CTX_set_padding(&ctx, 0); */ if(!EVP_EncryptUpdate(&ctx, encrypt, output_len, data, input_len)) { #ifdef REG_DEBUG if((fp = open_file_to_do (CRYPTO_LOG, "w"))) { fprintf(fp, "Error in encrypt update."); fclose(fp); } #endif } else { if (!EVP_EncryptFinal(&ctx, encrypt + (*output_len), &final_part)) { #ifdef REG_DEBUG if((fp = open_file_to_do (CRYPTO_LOG, "w"))) { fprintf(fp, "Error in encrypt final."); fclose(fp); } #endif } else *output_len += final_part; } EVP_CIPHER_CTX_cleanup(&ctx); return encrypt; } ===============>decryption function `data' is from "read" function which reads from a file and `input_len' is the return value of "read" function unsigned char *decode_string (char *data, int input_len, unsigned char key[], unsigned char iv[], int *output_len) { unsigned char *decrypt; int buff_size, final_part; EVP_CIPHER_CTX ctx; #ifdef REG_DEBUG FILE *fp; #endif buff_size = (input_len / BUFFER + 1) * BUFFER + EVP_MAX_BLOCK_LENGTH; /* if(input_len + IV_LEN > BUFFER) buff_size = input_len + IV_LEN + EVP_MAX_BLOCK_LENGTH; else buff_size = BUFFER + EVP_MAX_BLOCK_LENGTH; */ decrypt = (unsigned char *)malloc(buff_size * sizeof(char)); memset(decrypt, 0, buff_size); EVP_CIPHER_CTX_init(&ctx); EVP_DecryptInit(&ctx, EVP_bf_cbc(), key, iv); /* EVP_CIPHER_CTX_set_padding(&ctx, 0); */ if(!EVP_DecryptUpdate(&ctx, decrypt, output_len, data, input_len)) { #ifdef REG_DEBUG if((fp = open_file_to_do (CRYPTO_LOG, "w"))) { fprintf(fp, "Error in Decrypt update."); fclose(fp); } #endif } else { if (!EVP_DecryptFinal(&ctx, decrypt + (*output_len), &final_part)) { #ifdef REG_DEBUG if((fp = open_file_to_do (CRYPTO_LOG, "w"))) { fprintf(fp, "Error in Decrypt final."); fclose(fp); } #endif } else *output_len += final_part; } EVP_CIPHER_CTX_cleanup(&ctx); return decrypt; } _________________________________________________________________ Hotmail is redefining busy with tools for the New Busy. Get more from your inbox. http://www.windowslive.com/campaign/thenewbusy?ocid=PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_2