c - 使用字符串格式的公钥来加密 C 中的消息

标签 c encryption rsa

这里我的主要目的是在客户端和服务器交换公钥。然后使用它们通过加密方式向对方发送消息,对方使用其私钥进行解密。 现在我已经在客户端和服务器上用 c 生成了 rsa key 对,并获得了字符串格式的公钥和私钥,交换了它们,但我不知道如何使用它们来加密消息。 请帮忙。 这是我的客户端代码:

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>

#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>

#define KEY_LENGTH  2048
#define PUB_EXP     3
#define PRINT_KEYS
#define WRITE_TO_FILE

int main(int argc, char *argv[])
{
struct sockaddr_in serv;
int sockfd,newsockfd,portno=3000,test_v;
char buffer_c[4096],publickey[4096];

size_t pri_len;            // Length of private key
size_t pub_len;            // Length of public key
char   *pri_key;           // Private key
char   *pub_key;           // Public key
char   msg[KEY_LENGTH/8];  // Message to encrypt
char   *encrypt = NULL;    // Encrypted message
char   *decrypt = NULL;    // Decrypted message
char   *err;               // Buffer for any error messages
char   *pub2;   

// Generate key pair
printf("Generating RSA (%d bits) keypair...", KEY_LENGTH);
fflush(stdout);
RSA *keypair = RSA_generate_key(KEY_LENGTH, PUB_EXP, NULL, NULL);

// To get the C-string PEM form:
BIO *pri = BIO_new(BIO_s_mem());
BIO *pub = BIO_new(BIO_s_mem());

PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL);
PEM_write_bio_RSAPublicKey(pub, keypair);

pri_len = BIO_pending(pri);
pub_len = BIO_pending(pub);

pri_key = malloc(pri_len + 1);
pub_key = malloc(pub_len + 1);

BIO_read(pri, pri_key, pri_len);
BIO_read(pub, pub_key, pub_len);

pri_key[pri_len] = '\0';
pub_key[pub_len] = '\0';

#ifdef PRINT_KEYS
    printf("\nCLIENTPRIVATEKEY:\N%s\n", pri_key);
#endif
printf("done.\n");

if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
    printf("socket not created\n");
    exit(0);
}
else
    printf("socket created\n");

memset(&serv,'0',sizeof(serv));
memset(&buffer_c,'0',sizeof(buffer_c));

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


if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0)
{
    printf("not connected.\n");
    exit(0);
}
else
    printf("connected\n");

//send the pub-key
strcpy(buffer_c,pub_key);
test_v=send(sockfd,buffer_c,strlen(buffer_c),0);
if(test_v<0)
{
    printf("error sending.\n");
    exit(0);
}
else
{
    printf("client sent pub-key\n");
}

test_v=recv(sockfd,publickey,4095,0);
if(test_v<0)
{
    printf("error recving.\n");
    exit(0);
}
else
{
    publickey[test_v]='\0';
    printf("server pub-key:\n%s",publickey);

    * pub2 = publickey; 
}

 // Get the message to encrypt
    printf("Message to encrypt: ");
    fgets(msg, KEY_LENGTH-1, stdin);
    msg[strlen(msg)-1] = '\0';

// Encrypt the message
    encrypt = malloc(RSA_size(keypair));
    int encrypt_len;
    err = malloc(130);
    if((encrypt_len = RSA_public_encrypt(strlen(msg), (unsigned char*)msg, (unsigned char*)encrypt,keypair,RSA_PKCS1_OAEP_PADDING))==-1)        {
    ERR_load_crypto_strings();
    ERR_error_string(ERR_get_error(), err);
    fprintf(stderr, "Error encrypting message: %s\n", err);
    goto free_stuff;
    }   

close(sockfd);

    free_stuff:
    RSA_free(keypair);
    BIO_free_all(pub);
    BIO_free_all(pri);
    free(pri_key);
    free(pub_key);
    free(encrypt);
    free(decrypt);
    free(err);
return 0;

}

最佳答案

实际上,您并不使用公钥来加密消息,而是使用新的随 secret 钥来加密新的随 secret 钥,该随 secret 钥将用于使用对称 key 算法来加密消息,例如 AES ,由于使用非对称 key 加密算法的复杂性很高。当然,如果你愿意的话,你可以做到......

为了对其进行加密,您必须执行一些模块算术计算:

enc_message = message^public_key mod product_of_2_chosen_primes

并对其进行解码:

message = enc_message^private_key mod product_of_2_chosen_primes

看看RSA 。顺便说一句,由于您正在处理非常大的数字,因此您必须看看 some Big Number library

关于c - 使用字符串格式的公钥来加密 C 中的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19865327/

相关文章:

JAVA 将 AES key 、初始化 vector 和数据从加密文件中分离出来,并在加密过程中使用这 3 种组合

c++ - C/C++ 中的静态作用域

c++ - 尝试让 while 循环工作,当输入符号而不是字符时,该循环会中断

c - 为什么要在结构体之前添加标识符?我不明白为什么,如何解决这个问题?

c# - 将 VB6 随机化转换为 C#

java - 在 Java 中为 Web Push API 加密消息

java - PHP 如何处理三元组加密的 32 字节 key

c# - BTLE 4.0 负载

java.security.NoSuchAlgorithmException : RSA Signature not available

java - 使用 Java 内置库的简单 RSA 加密