On Sun, May 1, 2011 at 1:48 AM, derleader mail <derlea...@abv.bg> wrote:
> > Hi, > > The encrypted output is not a NULL terminated string so strlen will not > work. > > >> EVP_DecryptUpdate(&ctx, (unsigned char *)plaintextz, &out_len, (unsigned > char *)ciphertext, strlen(ciphertext)); > > Use the length output from the encryption part. > > Thank you very much for the reply. The problem is that the encryption and > decryption must be on separate machines. I need a way to take the size of > the encrypted message using language function like strlen(). Is there > other solution? > Hi, What protocol are you using? If you cannot send the "length" of the encrypted data, then you cannot decrypt it properly. > > Regards > > - re > > On Sun, May 1, 2011 at 12:27 AM, derleader mail <derlea...@abv.bg> wrote: > >> Hi, >> I'm trying to code a C program that can convert very big number of >> characters. The problem is that there is an error in decryption. >> >> This is the code: >> >> //gcc test_Blowfish.c -L/usr/local/ssl/lib/ -lssl -lcrypto -Wall >> >> #include <stdio.h> >> #include <stdlib.h> >> #include <strings.h> >> #include <openssl/blowfish.h> >> #include <openssl/evp.h> >> >> int main(void) { >> >> char plaintext[1024] = "{aaX{aaX57 : {223 : >> 2323}}{}{}{}{}{}{3535:42424}242424242242424243r23r23r23r23r23r23r3r{}pppa57 >> : {223 : >> 2323}}{}{}{}{}{}{3535:42424}242424242242424243r23r23r23r23r23r23r3r{}pppa{aaX57 >> : {223 : >> 2323}}{}{}{}{}{}{3535:42424}242424242242424243r23r23r23r23r23r23r3r{}pppa"; >> char plaintextz[1024]; >> char ciphertext[1024]= {0,}; >> char mykey[EVP_MAX_KEY_LENGTH] = "blowfish_key"; >> char iv[EVP_MAX_IV_LENGTH] = "blowfish"; >> int tmp_len = 0, in_len, out_len=0; >> EVP_CIPHER_CTX ctx; >> >> //memset(mykey,0,sizeof(mykey)); >> //memset(iv,0,sizeof(iv)); >> >> >> >> printf("No encrypt: %s\n", plaintext); >> printf("No encrypt size: %d\n", strlen(plaintext)); >> >> //Encrypt >> EVP_EncryptInit(&ctx, EVP_bf_cfb(), (unsigned char *)mykey, (unsigned >> char *)iv); >> EVP_EncryptUpdate(&ctx, (unsigned char *)ciphertext, &out_len, >> (unsigned char *)plaintext, strlen(plaintext)); //Block through the mem >> to be encrypted >> tmp_len += out_len; >> EVP_EncryptFinal(&ctx, (unsigned char *) &ciphertext[out_len], >> &out_len); //Finish any remaining encryption and throw a pad on >> tmp_len += out_len; >> printf("Encrypted: %s\n", ciphertext); >> printf("Encrypted size: %d\n", tmp_len); >> >> //Reset memory for Decryption >> // memset(plaintext,0,sizeof(plaintext)); >> in_len = tmp_len; >> out_len = tmp_len = 0; >> >> //decrypt >> EVP_DecryptInit(&ctx, EVP_bf_cfb(), (unsigned char *)mykey, (unsigned >> char *)iv); >> EVP_DecryptUpdate(&ctx, (unsigned char *)plaintextz, &out_len, >> (unsigned char *)ciphertext, strlen(ciphertext)); >> tmp_len += out_len; >> EVP_DecryptFinal(&ctx, (unsigned char *)&plaintextz[out_len], >> &out_len); >> tmp_len += out_len; >> >> //Zero out the pad >> memset(&plaintext[tmp_len],0,(int)(sizeof(plaintext)) - tmp_len); >> >> printf("Decrypted : %s\n", plaintextz); >> printf("Decrypted size: %d\n", tmp_len); >> >> printf("Block Size: %d\n",EVP_CIPHER_CTX_block_size(&ctx)); >> >> return 0; >> } >> >> >> >> This is the output: >> >> [root@localhost test]# ./a.out >> No encrypt: {aaX{aaX57 : {223 : >> 2323}}{}{}{}{}{}{3535:42424}242424242242424243r23r23r23r23r23r23r3r{}pppa57 >> : {223 : >> 2323}}{}{}{}{}{}{3535:42424}242424242242424243r23r23r23r23r23r23r3r{}pppa{aaX57 >> : {223 : >> 2323}}{}{}{}{}{}{3535:42424}242424242242424243r23r23r23r23r23r23r3r{}pppa >> No encrypt size: 267 >> Encrypted: �A-�� W =?:�$�i �_�8:�F�wo#�5 � @D�mo��-I ���F�Q�J�#��F�0b� >> ;�`� C䦱�~6�)ހ�YG �ed�Ӕ�Z%�9!mdvϋ���\���QB��}�N ����@_�W�F�e"� >> Encrypted size: 267 >> Decrypted : {aaX{aaX57 : {223 : >> 2323}}{}{}{}{}{}{3535:42424}242424242242424243r23r23r23r23r23r23r3r{}pppa57 >> : {223 : 2323}}{}{}{}{}{}{3535:4242 >> Decrypted size: 131 >> Block Size: 1 >> >> As youy see the decrypted size number is less that the original. >> Any idea where is the problem? >> >> > >