ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

linux C语言 签名验签 --- 亲测 sha256 sha512

2020-03-21 17:52:18  阅读:360  来源: 互联网

标签:include int linux RSA C语言 char printf 验签 NULL


 

签名:

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

/*
 * 参考https://blog.csdn.net/zjf535214685/article/details/82182241
*/ 

#define PRIVATE_KEY_PATH ("./rsaprivatekey.pem")
#define SHA_WHICH        NID_sha256

void printHex(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; 
    RSA  *priRsa = 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);
    
    return priRsa;
}

int test_RSA_sign(void)
{
    char *data = "china";
    char buf[128] = {0};
    RSA *privKey = NULL;
    int nOutLen = sizeof(buf);
    int nRet = 0;

    //对数据进行sha256算法摘要
    SHA256_CTX c;
    unsigned char md[SHA256_DIGEST_LENGTH];
 
    SHA256((unsigned char *)data, strlen(data), md);
    printHex(md, SHA256_DIGEST_LENGTH);

    privKey = ReadPrivateKey(PRIVATE_KEY_PATH);
    if (!privKey) 
    {  
        ERR_print_errors_fp (stderr);    
        return -1;  
    }
 

    /* 签名 */
    nRet = RSA_sign(SHA_WHICH, md, SHA256_DIGEST_LENGTH, buf, &nOutLen, privKey);
    if(nRet != 1)
    {
        printf("RSA_sign err !!! \n");    
        goto quit;
    }
    printf("RSA_sign len = %d:", nOutLen);
    printHex(buf, nOutLen);


quit:
    RSA_free(privKey);
    
    return 0;
}


int main(int argc, char *argv[])
{
    test_RSA_sign();
    return 0;
}

验签:

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

/*
 * 参考https://blog.csdn.net/zjf535214685/article/details/82182241
*/ 

#define PUBLIC_KEY_PATH  ("./rsapubkey.pem")
#define SHA_WHICH        NID_sha256

void printHex(unsigned char *md, int len)
{
 
    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%02x", md[i]);
    }
 
    printf("\n");
}

/*读取公匙*/
RSA* ReadPublicKey(char* p_KeyPath)
{   
    FILE *fp = NULL; 
    RSA *pubRsa = NULL;
    
    printf("PublicKeyPath[%s]\n", p_KeyPath);
 
    /*  打开密钥文件 */
    if(NULL == (fp = fopen(p_KeyPath, "r")))
    {
        printf( "fopen[%s] \n", p_KeyPath);
        return NULL;
    }
    /*  获取公钥 */
    if(NULL == (pubRsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL,NULL)))
    {
        printf( "PEM_read_RSAPrivateKey error\n");
        fclose(fp);
        return NULL;
    }
    fclose(fp);
 
    return pubRsa;
}

int test_RSA_verify(void)
{
    char *data = "china";
    char buf[128] = {
0x06,0x62,0x0b,0xb4,0x16,0xdf,0x52,0xb9,
0x42,0x53,0x05,0x95,0x12,0xbe,0x3e,0x4f,
0x9e,0x4d,0xed,0x20,0xf8,0x3a,0x07,0xad,
0xc4,0xe0,0x6d,0xb9,0xd5,0x35,0xe8,0xae,
0xf3,0x84,0xdb,0xd5,0x33,0x6f,0x10,0x9b,
0x47,0x8d,0x26,0x7a,0x50,0x9f,0xf9,0x57,
0xec,0xba,0xa3,0xc1,0x50,0xae,0x47,0xbb,
0xcb,0x6c,0x87,0x78,0x19,0xb3,0x1f,0x1f,
0x68,0x9a,0xc2,0x9e,0xde,0x3c,0xdd,0x97,
0x17,0x17,0xaf,0xd1,0xc9,0xfb,0x68,0x58,
0x19,0xbb,0xa4,0xf4,0x18,0x4d,0xe3,0xf3,
0xb0,0x8d,0x30,0xe6,0x5b,0x6d,0x5e,0x2f,
0xf5,0xe7,0x6b,0x30,0xf0,0x70,0xa4,0x69,
0xfa,0xb9,0xa8,0xdd,0xf0,0x71,0x99,0x6c,
0x7a,0xc2,0xce,0xe8,0x13,0x46,0x0c,0x85,
0x8e,0x3f,0x55,0xe3,0xe7,0x30,0xd1,0x7d,    
     };
    RSA *pubKey = NULL;
    int nOutLen = sizeof(buf);
    int nRet = 0;

    //对数据进行sha256算法摘要
    SHA256_CTX c;
    unsigned char md[SHA256_DIGEST_LENGTH];
 
    SHA256((unsigned char *)data, strlen(data), md);
    printHex(md, SHA256_DIGEST_LENGTH);

 
    pubKey = ReadPublicKey(PUBLIC_KEY_PATH);  
    if (!pubKey)
    {
       printf("Error: can't load public key");
       return -1;
    }

    /* 验签 */
    nRet = RSA_verify(SHA_WHICH, md, SHA256_DIGEST_LENGTH, buf, nOutLen, pubKey);
    printf("RSA_verify %s(ret=%d).\r\n", (1 == nRet) ? "Success" : "Failed", nRet);

    RSA_free(pubKey);
    
    return 0;
}

int main(int argc, char *argv[])
{
    test_RSA_verify();
    return 0;
}

 

标签:include,int,linux,RSA,C语言,char,printf,验签,NULL
来源: https://www.cnblogs.com/LiuYanYGZ/p/12540577.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有