Dear all,
I would like to encrypt the transmitting data on the server side before the data is send to a socket. And then, decrypt the data from the socket on the client side. I am using the following code, which is from Internet. #include "encryption.h" byte_t* BufferEncryption(const byte_t* apBuffer, size_t aNumBytes) { int ret; EVP_CIPHER_CTX ctx; const EVP_CIPHER* cipher; byte_t key[24], iv[8], out[500]; int i, len, inl, outl, total = 0; for (i = 0; i < 24; i ++) { memset(&key[i], i, 1); } for (i = 0; i < 8; i ++) { memset(&iv[i], i, 1); } EVP_CIPHER_CTX_init(&ctx); cipher = EVP_des_ede3_ofb(); //cipher = EVP_des_ede3_cbc(); ret = EVP_EncryptInit_ex(&ctx, cipher, NULL, key, iv); if (ret != 1) { printf("EVP_EncryptInit_ex err!\n"); return (byte_t*)"error"; } inl = 50; len = 0; int j = aNumBytes / inl; for (int k = 0; k < j; k ++) { EVP_EncryptUpdate(&ctx, out + len, &outl, apBuffer + inl, inl); len += outl; } EVP_EncryptFinal_ex(&ctx, out+len, &outl); len += outl; return (byte_t*)out; } apBuffer is the pointer to the buffer storage the unencrypted data. aNumBytes is the length of the apBuffer. There are two questions. (1)The apBuffer is always less than inl=50. Should I use EVP_EncryptUpdate for one time to encrypt my data, or should I use EVP_encryptFinal_ex directly? (2)I don't think the code works. The execution is terminated without encryption on ret = EVP_EncryptInit_ex(&ctx, cipher, NULL, key, iv); I don't know why. Could anyone please help me to answer the two questions? Any help will be really appreciated. Thank you so much. Best Regards, Xiang _________________________________________________________________ 约会说不清地方?来试试微软地图最新msn互动功能! http://ditu.live.com/?form=TL&swm=1