使用套接字编程通过本地网络通过RSA发送加密消息

时间:2018-03-13 10:57:28

标签: openssl rsa public-key-encryption tcp-ip private-key

我尝试使用tcp / ip通信在网络上发送加密消息,但是在最后一步发生错误,即正在发送消息,并且在私下解密后发生错误。

此代码在生成后第一次使用密钥时完全正常运行但在第一次使用后每次都会发生错误:

  

0407109F:rsa例程:RSA_padding_check_PKCS1_type_2:pkcs解码错误

我正在分享代码:

server side-


#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
//openssl headers
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
//int padding = RSA_SSLV23_PADDING;
//int padding = RSA_PKCS1_OAEP_PADDING;

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( "Failed to create RSA");
    }

    return rsa;
}

int public_encrypt(unsigned char * data,int data_len,unsigned char * key, unsigned char *encrypted)
{
    RSA * rsa = createRSA(key,1);
    int result = RSA_public_encrypt(data_len,data,encrypted,rsa,padding);               //fun for encrypting
    return result;
}



void printLastError(char *msg)
{
    char * err = malloc(130);;
//    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    printf("%s ERROR: %s\n",msg, err);
    free(err);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
int  main()
{
        int eof=0;
        char plainText[2048/8] = "Hello this is AMIT"; //key length : 2048
        char publicKey[4098];
        char privatekey[4098];

        FILE *fd=fopen("public.pem","r");
        while(!feof(fd))
        {
                eof=fread(&publicKey,sizeof(publicKey),1,fd);
//              perror("public key fread");
                if(eof==0)
                {
                        fclose(fd);
                        break;
                }
        }
//printf("public key---%s\n",publicKey);

        unsigned char  encrypted[4098]={};

        int encrypted_length= public_encrypt(plainText,strlen(plainText),publicKey,encrypted);
//      perror("public encryption");
        if(encrypted_length == -1)
        {
                printLastError("Public Encrypt failed ");
                exit(0);
        }
        printf("Encrypted length =%d\n",encrypted_length);
//      printf("Encrypted DATA =%s\n",encrypted);

/*------------------SOCKET---------------------------*/
        struct sockaddr_in server , client;
        char buf[10];
        memset(buf,'\0',sizeof(buf));

        int socket_id = socket(AF_INET, SOCK_STREAM, 0);
        server.sin_family=AF_INET;
        server.sin_port=htons(5000);
        server.sin_addr.s_addr=inet_addr("127.0.0.1");

        bind(socket_id, (struct sockaddr *)&server,sizeof(server));
        perror("bind");
        listen(socket_id,5);

        int addrlen=sizeof(struct sockaddr);
        int client_sock=accept(socket_id,(struct sockaddr *)&client,(socklen_t*)&addrlen);
        perror("accept");
/*      while(9)
        {
                printf("write data for client\n");
                scanf("%s",buf);
                write(client_sock,&buf,strlen(buf));
                perror("write");
        }
*/
        printf("sending encryptd data\n");
        write(client_sock,&encrypted_length,sizeof(encrypted_length));
        perror("write");
        sleep(1);
        write(client_sock,&encrypted,strlen(encrypted));
        perror("write");
/*-----------------END-----------------------------*/
}

客户端 -

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
//openssl headers
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/bio.h>
#include <openssl/err.h>
int padding = RSA_PKCS1_PADDING;
//int padding = RSA_SSLV23_PADDING;
//int padding = RSA_PKCS1_OAEP_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( "Failed to create RSA");
    }

    return rsa;
}


int private_decrypt(unsigned char * enc_data,int data_len,unsigned char * key, unsigned char *decrypted)
{
printf("len = %d\n",data_len);
    RSA * rsa = createRSA(key,0);
    int  result = RSA_private_decrypt(data_len,enc_data,decrypted,rsa,padding);         //fun for decrypting
perror("RSA");
printf("Resut = %d\n",result);
    return result;
}


void printLastError(char *msg)
{
    char * err = malloc(130);;
//    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    printf("\n%s ERROR: %s\n",msg, err);
    free(err);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
int  main()
{
        int eof=0;
//      char plainText[2048/8] = "Hello this is AMIT"; //key length : 2048
        char publicKey[4098];
        char privateKey[4098];
        unsigned char decrypted[4099]={};
        unsigned char encrypted[4098]={};
        int encrypted_length=0;

        FILE *fd=fopen("private.pem","r");
        while(!feof(fd))
        {
                eof=fread(&privateKey,sizeof(privateKey),1,fd);
                perror("private key fread");
                if(eof==0)
                {
                        fclose(fd);
                        break;
                }
        }

//      printf("private key--->\n%s\n",privateKey);

/*------------------SOCKET---------------------------*/

        struct sockaddr_in server , client;
//      char buf[10];
        memset(encrypted,'\0',sizeof(encrypted));
        int socket_id = socket(AF_INET, SOCK_STREAM, 0);

        client.sin_family=AF_INET;
        client.sin_port=htons(5000);
        client.sin_addr.s_addr=inet_addr("127.0.0.1");


        connect(socket_id,(struct sockaddr *)&client,sizeof(client));
        perror("connect");
        printf("\nWAITING TO RECEIVE DATA LENGTH...\n");
        read(socket_id,&encrypted_length,sizeof(encrypted_length));
        //printf("\nENCRYPTED DATA= %s\n",encrypted);

        printf("WAITING TO RECEIVE DATA...\n");
        read(socket_id,&encrypted,sizeof(encrypted));
perror("read");
        printf("ENCRYPTED LENGTH= %d\n",encrypted_length);
/*----------------------------END-----------------------------*/

        int decrypted_length = private_decrypt(encrypted,encrypted_length,privateKey, decrypted);
        //int decrypted_length = private_decrypt(encrypted,512,privateKey, decrypted);
        printf("\nDecrypted Length =%d\n",decrypted_length);
        if(decrypted_length == -1)
        {
                printLastError("Private Decrypt failed ");
                //exit(0);
        }
        printf("\nDecrypted Text =%s\n",decrypted);
}

0 个答案:

没有答案