On Sun, May 1, 2011 at 5:28 PM, derleader mail <[email protected]> wrote:
> >What protocol are you using?
> What I mean is application layer protocol. But since in your example,
> you're using your own protocol,
> why not send both length and data.
> Example.
> <4 byte len field><0..2^32-1 data field>
>
> Then in you receiving end, do recv 4 bytes, get length, and recv until
> received data equals to length.
>
> And decrypt.
>
> - re
>
> You mean furst to send the encryped string and next the length of the
> string as value?
>
> Example for server:
>
> send(sock, encrypted_string, 25, 0);
> send(sock, encrypted_string_length, 25, 0);
>
Example only:
Assume, encrypted_data and encrypted_data_len are the
the output of you encryption operation.
For simplicity, copy to a separate buffer
.
memcpy(buff, &encrypted_data_len, sizeof(unsigned int)); // need to convert
to network byte
memcpy(buff+sizeof(unsigned int), encrypted_data, encrypted_data_len);
total = sizeof(unsigned int) + encrypted_data_len;
sent = 0;
do {
ret = send(sock, buff+sent, total-sent);
sent += ret;
} while (sent < total);
>
> For client
> recv(sock, encrypted_string, 25, 0);
> recv(sock, encrypted_string_length, 25, 0);
>
In your client side, first, received the first four bytes.
Now that you have the encrypted length, received the data.
Perform decryption.
On Sun, May 1, 2011 at 4:34 PM, derleader mail <[email protected]> wrote:
>
>> 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.
>
>
> I'm going to use stream protocol - TCP/IP. Here is the template source code
> of the server without the encryption part
>
> #include <stdio.h>
> #include <sys/ioctl.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <netinet/in.h>
> #include <pthread.h>
>
> void* thread_proc(void *arg);
>
> int main(int argc, char *argv[])
> {
> struct sockaddr_in sAddr;
> int listensock;
> int result;
> int nchildren = 1;
> pthread_t thread_id;
> int x;
> int val;
>
> if (argc > 1) {
> nchildren = atoi(argv[1]);
> }
>
> listensock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
>
> val = 1;
> result = setsockopt(listensock, SOL_SOCKET, SO_REUSEADDR, &val,
> sizeof(val));
> if (result < 0) {
> perror("server5");
> return 0;
> }
>
> sAddr.sin_family = AF_INET;
> sAddr.sin_port = htons(1972);
> sAddr.sin_addr.s_addr = INADDR_ANY;
>
> result = bind(listensock, (struct sockaddr *) &sAddr, sizeof(sAddr));
> if (result < 0) {
> perror("exserver5");
> return 0;
> }
>
> result = listen(listensock, 5);
> if (result < 0) {
> perror("exserver5");
> return 0;
> }
>
> for (x = 0; x < nchildren; x++) {
> result = pthread_create(&thread_id, NULL, thread_proc, (void *)
> listensock);
> if (result != 0) {
> printf("Could not create thread.\n");
> return 0;
> }
> sched_yield();
> }
>
> pthread_join (thread_id, NULL);
> }
>
> void* thread_proc(void *arg)
> {
> int listensock, sock;
> char buffer[25];
> int nread;
>
> listensock = (int) arg;
>
> while (1) {
> sock = accept(listensock, NULL, NULL);
> printf("client connected to child thread %i with pid %i.\n",
> pthread_self(), getpid());
> nread = recv(sock, buffer, 25, 0);
> buffer[nread] = '\0';
> printf("%s\n", buffer);
> send(sock, buffer, nread, 0);
> close(sock);
> printf("client disconnected from child thread %i with pid %i.\n",
> pthread_self(), getpid());
> }
> }
>