【问题标题】:OpenSSL C RSA library decryptionOpenSSL C RSA 库解密
【发布时间】:2016-07-02 23:41:34
【问题描述】:

我正在使用这些函数将文本文件加密和解密为使用 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;
}

【问题讨论】:

  • RSA 不是加密文件的好解决方案,因为可加密数据的大小限制仅限于密钥长度。数据加密一般采用AES等对称算法。

标签: c encryption rsa


【解决方案1】:
fputs((const char *)encrypted_data,file);

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

【讨论】:

  • 什么解决方案可以作为替代方案?
  • 提示:public_key_encryption() 返回“加密”数据的长度(由 RSA_public_encrypt() 返回)...
  • @f0unix 你注意到你的代码忽略了result吗?
  • 是的,问题是它既不加密也不解密原型
  • @f0unix 你真的不应该使用 AES 进行加密和解密。它是用作系统组件的原语,您尝试将其用作整个系统。
猜你喜欢
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 2020-10-14
  • 1970-01-01
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
相关资源
最近更新 更多