无法通过c中的tcp套接字发送加密数据

Not able to send encrypted data through tcp socket in c

我正在 client.c 中加密数据并将其发送到 server.c。但是,当我在 client.c 中打印加密数据的大小时,它是 256,而在 server.c 中,它打印的是 1 到 256 之间的某个数字。因此我无法通过套接字发送加密数据。如何解决这个问题?

client.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include <openssl/aes.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

char pub[100000],pri[100000];

void read_file(char *fnm,char *data)
{
    int length;
    FILE *f=fopen(fnm,"rb");

    if(f)
    {
        fseek(f,0,SEEK_END);
        length=ftell(f);
        fseek(f,0,SEEK_SET);
        fread(data,1,length,f);
        fclose(f);
    }
}

int padding = RSA_PKCS1_PADDING;

RSA * createRSA(unsigned char * key,int public) {
    RSA *rsa= NULL;
    BIO *keybio;
    keybio = BIO_new_mem_buf(key, -1);
    if (keybio==NULL) {
        printf( "Failed to create key BIO");
        return 0;
    }
    if(public) {
        rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL);
    }
    else {
        rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL);
    }
    if(rsa == NULL) {
        printf("Could not create RSA\n");
    }
    return rsa;
}

int main(int argc,char *argv[])
{
    int sockfd,portno,n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[100000],arg3[100000],encrypt[100000],decrypt[100000];

    //file name,IP,port number,it's private key,other client public key
    //use public key to encrypt
    //private key to decrypt
    //so we can make char arrays and read who file data into them

    read_file(argv[3],pri);
    read_file(argv[4],pub);

    RSA *rpub = createRSA((unsigned char*)pub,1);
    RSA *rpri = createRSA((unsigned char*)pri,0);

    if(argc<5)
    {
        error("Enter all details");
    }

    portno = atoi(argv[2]);

    sockfd = socket(AF_INET,SOCK_STREAM,0);

    if(sockfd<0)
        error("Error opening socket");

    server = gethostbyname(argv[1]);

    if(server==NULL)
    {
        error("No such host");
    }

    bzero((char*) &serv_addr,sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;

    bcopy((char*)server->h_addr,(char*)&serv_addr.sin_addr.s_addr,server->h_length);

    serv_addr.sin_port=htons(portno);

    if(connect(sockfd,&serv_addr,sizeof(serv_addr))<0)
    {
        printf("Error connecting\n");
    }

    //first send it's own message and then receive message from server
    // Do the encryption of own message before sending
    //decrypt the message received from server 


    printf("Please enter the message\n");
    bzero(buffer,100000);
    fgets(buffer,100000-1,stdin);


    //encrypt the buffer data

    int encrypted_len = RSA_public_encrypt(strlen(buffer),buffer,encrypt,rpub,padding);

    //send the encrypted data

    printf("%d\n", encrypted_len);

    if(encrypted_len==-1)
    {
        printf("Encryption failed\n");
        return 0;
    }

    //encrypt = buffer;

    n=write(sockfd,encrypt,encrypted_len);

    if(n<0)
    {
            printf("Error writing to socket\n");
            return 0;
    }


    return 0;
}

server.c

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>


int main(int argc,char *argv[])
{
    int sockfd,newsockfd[2],portno,clilen;
    char buffer[2][100000];
    struct sockaddr_in serv_addr,cli_addr;
    int n;

    if(argc < 2)
    {
        printf("Error, no port provided\n");
        exit(1);
    }

    sockfd=socket(AF_INET,SOCK_STREAM,0);

    if(sockfd < 0)
    {
        error("Error opening socket\n");
    }

    bzero((char*) &serv_addr, sizeof(serv_addr));

    portno=atoi(argv[1]);

    serv_addr.sin_family=AF_INET;
    serv_addr.sin_addr.s_addr=INADDR_ANY;
    serv_addr.sin_port=htons(portno);

    if( bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("Error on binding\n");
        return 0;
    }

    listen(sockfd,5);
    clilen=sizeof(cli_addr);

    
    newsockfd[0]=accept(sockfd,(struct sockaddr *) &cli_addr, &clilen);

    bzero(buffer,100000);

    n=read(newsockfd[0],buffer[0],100000-1);


    printf("%d\n", strlen(buffer[0]));

    //server part if completed

    return 0;
}

注意:对于运行 client.c,您需要提供本地主机、端口号、私钥、public 密钥作为命令行参数。而 运行 server.c 只需要端口号。

However when i am printing the size of the encrypted data in client.c it's 256 whereas on server.c it's printing some number between 1 and 256.

服务器打印的不是加密数据的大小,而是strlen(buffer[0]);您忽略了加密数据可以包含 空字符 ,因此应用 strlen.

是不合适的

正如这里所建议的,Jeremy Friesner 的评论也很有帮助:

无法保证(在 TCP 或 OpenSSL 中)您将通过对 read() 的一次调用接收所有发送的字节。你需要一直调用read()直到你收到所有你期望得到的字节(或者直到read()returns0,表示连接已经关闭)