c - OpenSSL C RSA 库解密

标签 c encryption rsa

我正在使用这些函数使用 RSA_public_encrypt 和 RSA_private_decrypt 将文本文件加密和解密为输出文本文件

在启动命令行程序时,将公钥文件名或私钥文件名作为输入,加密过程工作正常,但解密总是失败。

下面是我调用的加密文件函数,它调用 readRSAKeyFromFile 返回 RSA 数据类型,以便稍后处理。

如果我在这里遗漏了什么,请告诉我。

我对 C 有点陌生,我想尝试编写一些东西作为测试来理解 C 基础知识。

非常感谢您的帮助

void enc_file(char *pub_key_name, char *file_name){

    RSA *rsa = readRSAKeyFromFile(pub_key_name, 1);

    char *data_read_from_file;
    long fileSize = 0;

    unsigned char *encrypted_data = (unsigned char*)malloc( RSA_size(rsa) ) ;

    FILE * stream = fopen (file_name, "rb");
    //Seek to the end of the file to determine the file size
    fseek(stream, 0L, SEEK_END);
    fileSize = ftell(stream);
    fseek(stream, 0L, SEEK_SET);

    //Allocate enough memory (add 1 for the \0, since fread won't add it)
    data_read_from_file = malloc(fileSize+1);

    //Read the file
    size_t size=fread(data_read_from_file,1,fileSize,stream);
    data_read_from_file[size]= 0; // Add terminating zero.
    fclose(stream);

    int result = public_key_encryption(data_read_from_file, encrypted_data, rsa);

    free(data_read_from_file);

    FILE * file = fopen("encrypted_data.txt","w+");
    fputs((const char *)encrypted_data,file);
    fclose(file);

    printf(" %s \n", encrypted_data );

    if( result == -1 ){
        perror("Couldn't encrypt file");
    }else{
        printf("[*] Successfully encrypted data \n" );
    }

}


void dec_file(char *priv_key_name, char *file_name){

    RSA *rsa = readRSAKeyFromFile(priv_key_name, 0);

    char *data_read_from_file;
    long fileSize = 0;

    unsigned char *decrypted_data = (unsigned char*)malloc( RSA_size(rsa) ) ;

    FILE * stream = fopen (file_name, "rb");
    //Seek to the end of the file to determine the file size
    fseek(stream, 0L, SEEK_END);
    fileSize = ftell(stream);
    fseek(stream, 0L, SEEK_SET);

    //Allocate enough memory (add 1 for the \0, since fread won't add it)
    data_read_from_file = malloc(fileSize+1);

    //Read the file
    size_t size=fread(data_read_from_file,1,fileSize,stream);
    data_read_from_file[size]= 0; // Add terminating zero.
    fclose(stream);

    int result = private_key_decryption(data_read_from_file, decrypted_data, rsa);

    free(data_read_from_file);

    FILE * file = fopen("encrypted_data.txt","w+");
    fputs((const char *)decrypted_data,file);
    fclose(file);

    printf(" %s \n", decrypted_data );

    if( result == -1 ){
        perror("Couldn't encrypt file");
    }else{
        printf("[*] Successfully decrypted data \n" );
    }

}

RSA * readRSAKeyFromFile(char * filename,int is_public){


    FILE * rsa_pkey_file = fopen(filename,"r");

    if(rsa_pkey_file == NULL){
        printf("ERROR opening file :: %s \n",filename);
        return NULL;
    }

//    RSA * rsa_key=  RSA_new();
    RSA *rsa_pkey = NULL;

    if(is_public == 1 ){
        PEM_read_RSA_PUBKEY(rsa_pkey_file, &rsa_pkey, NULL, NULL);
    }else{
        PEM_read_RSAPrivateKey(rsa_pkey_file, &rsa_pkey, NULL, NULL);
    }

    return rsa_pkey;
}

int public_key_encryption( char *data,  unsigned char *encrypted, RSA *rsa_key){

    int result = RSA_public_encrypt( (int)strlen(data), (const unsigned char*)data, encrypted, rsa_key, RSA_PKCS1_PADDING ) ;
    return result;
}

int private_key_decryption(char * data, unsigned char *decrypted, RSA *rsa_key){

    int result = RSA_private_decrypt((int)strlen(data),(const unsigned char *)data,(unsigned char*)decrypted,rsa_key,RSA_PKCS1_PADDING);
    return result;
}

最佳答案

fputs((const char *)encrypted_data,file);

问题就出在这里。加密数据不是 C 风格字符串,仅将其转换为 const char * 并将其传递给采用 C 风格字符串的函数是行不通的。

关于c - OpenSSL C RSA 库解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38165408/

相关文章:

c - 内联 asm 代码组织

C++/RSA转换 key

c - 使用 fwrite() 将标准输入写入文件

c - 将 'char *(char *)' 传递给类型为 'VALUE (*)()' 的参数的不兼容指针类型

python - 在 Python 3 中删除字符串文字前面的 'b' 字符 do

php - 如何正确存储 PBKDF2 密码哈希

javascript - 有人可以解密这个 javascript 吗?

java - 在 Java 上验证 AWS id token

node.js - Node.js 中的 XML 到 PEM

c - 使用O_CREAT时open的执行