小健triple 2019-03-28 10:39
浏览 219

why undefined reference to system function(such as SHA512_Init) on linux gcc?

我想问一下用以下代码我以及表明了头文件而且也存在这些头文件并且这些函数确
实存在于这些头文件中,为什还显示undefined reference to?
第一次用这个,很希望大家能够帮我看看谢谢

#include <string.h>
#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/sha.h> 
#include <openssl/crypto.h> 



#define PUBLIC_KEY_PATH ("/home/yang_mingkai/rsa_private_key.pem")
#define PRIVATE_KEY_PATH ("/home/yang_mingkai/rsa_public_key.pem")


void printHash(unsigned char *md, int len)
{

    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%02x", md[i]);
    }

    printf("\n");
}

RSA* ReadPrivateKey(char* p_KeyPath)

{   
    FILE *fp = NULL;

    char szKeyPath[1024];

    RSA  *priRsa = NULL, *pubRsa = NULL, *pOut = NULL;



    printf("PrivateKeyPath[%s] \n", p_KeyPath);


    /*  Žò¿ªÃÜÔ¿ÎÄŒþ */

    if(NULL == (fp = fopen(p_KeyPath, "r")))

    {

        printf( "fopen[%s] failed \n", p_KeyPath);

        return NULL;
    }

    /*  »ñÈ¡ËœÃÜÔ¿ */

    priRsa = PEM_read_RSAPrivateKey(fp, NULL, NULL,NULL);

    if(NULL == priRsa)

    {
        ERR_print_errors_fp(stdout);

        printf( "PEM_read_RSAPrivateKey\n");

        fclose(fp);

        return NULL;

    }

    fclose(fp);


pOut = priRsa;

    return pOut;

}


/*¶ÁÈ¡¹«³×*/

RSA* ReadPublicKey(char* p_KeyPath)

{

FILE *fp = NULL;

    char szKeyPath[1024];

    RSA  *priRsa = NULL, *pubRsa = NULL, *pOut = NULL;



printf("PublicKeyPath[%s]\n", p_KeyPath);


    /*  Žò¿ªÃÜÔ¿ÎÄŒþ */

    if(NULL == (fp = fopen(p_KeyPath, "r")))

    {

        printf( "fopen[%s] \n", p_KeyPath);

        return NULL;

    }

    /*  »ñÈ¡ËœÃÜÔ¿ */

    if(NULL == (priRsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL,NULL)))

    {

        printf( "PEM_read_RSAPrivateKey error\n");

        fclose(fp);

        return NULL;

    }

    fclose(fp);



    pOut = priRsa;

    return pOut;

}


int main()

{

    char *ct = "55dsd421fd4df1x21c1d4sd21sd51s5";

    char *buf;   

    char *buf2;

    RSA *pubKey;

    RSA *privKey;

    int len;

//  int nOutLen;



    buf = malloc(520);

    buf2 = malloc(520);





    SHA512_CTX c;

    unsigned char md[SHA512_DIGEST_LENGTH];




    sha512_init(&c);

    SHA512_Update(&c, ct, strlen(ct));


    SHA512_Final(md, &c); 

    OPENSSL_cleanse(&c, sizeof(c));


    printHash(md, SHA512_DIGEST_LENGTH);



              privKey = ReadPrivateKey(PRIVATE_KEY_PATH);

              if (!privKey) 

    {

        ERR_print_errors_fp (stderr);

        exit (1);

    }


    pubKey = ReadPublicKey(PUBLIC_KEY_PATH);

    if(!pubKey)

    {

       RSA_free(privKey);

       printf("Error: can't load public key");

       exit(1);

    }







    len = RSA_private_encrypt(SHA512_DIGEST_LENGTH, md, buf,

        privKey,RSA_PKCS1_PADDING);

    if (len != 256)

    {

        printf("Error: ciphertext should match length of key len = %d \n", len);

        exit(1);

    }



    printf("RSA_private_encrypt:");

    printHash(buf, strlen(buf));



    /*¹«Ô¿œâÃÜ*/

    RSA_public_decrypt(len, (const unsigned char*)buf, (unsigned char*)buf2, 

        pubKey,RSA_PKCS1_PADDING);

    printf("RSA_public_decrypt:");

    printHash(buf2, strlen(buf2));



    rsa_free(privKey);

    rsa_free(pubKey);

    free(buf);

    free(buf2);


     return 0;

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 r语言蛋白组学相关问题
    • ¥15 Python时间序列如何拟合疏系数模型
    • ¥15 求学软件的前人们指明方向🥺
    • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
    • ¥20 双层网络上信息-疾病传播
    • ¥50 paddlepaddle pinn
    • ¥20 idea运行测试代码报错问题
    • ¥15 网络监控:网络故障告警通知
    • ¥15 django项目运行报编码错误
    • ¥15 STM32驱动继电器