sha256withrsa 的C语言实现

#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <openssl/crypto.h>
typedef unsigned char           uint8_t;
#define ERROR_SM_SUCCESS 0           // 成功
#define ERROR_SM_INVALID_ARGUMENT -1 // 无效参数
#define ERROR_SM_OPENSSL_FAIL -2     // 失败
#define ERROR_SM_OPEN_FILE_FAIL -3   // 文件打开失败

#define PRIVATE_KEY_PATH ("/root/cxm/pri222.pem")

#define SHA_WHICH        NID_sha256
#define WHICH_DIGEST_LENGTH    SHA256_DIGEST_LENGTH

int Base64Encode(const void *plaintext, size_t plainlen, char *ciphertext,
                 size_t *cipherlen)
{
    if (!ciphertext)
    {
        *cipherlen = (plainlen + 2) / 3 * 4;
        return ERROR_SM_SUCCESS;
    }
    int nLen = EVP_EncodeBlock(ciphertext, (const unsigned char *)plaintext,
                               (int)plainlen);
    if (nLen < 0)
    {
        return ERROR_SM_OPENSSL_FAIL;
    }
    *cipherlen = nLen;
    return ERROR_SM_SUCCESS;
}
void printHex(unsigned char *md, int len)
{

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

/*读取私钥*/
RSA* ReadPrivateKey(char* p_KeyPath)
{
    FILE *fp = NULL;
    RSA  *priRsa = NULL;

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

    /*  打开密钥文件 */
    if(NULL == (fp = fopen(p_KeyPath, "r")))
    {
        printf( "fopen[%s] failed ", p_KeyPath);
        return NULL;
    }
    /*  获取私钥 */
    priRsa = PEM_read_RSAPrivateKey(fp, NULL, NULL,NULL);
    if(NULL == priRsa)
    {
        ERR_print_errors_fp(stdout);
        printf( "PEM_read_RSAPrivateKey");
        fclose(fp);
        return NULL;
    }
    fclose(fp);

    return priRsa;
}

int test_RSA_sign(uint8_t plaintext[2048],char *data)
{
    char buf[128] = {0};
    RSA *privKey = NULL;
    int nOutLen = sizeof(buf);
    int nRet = 0;

    //对数据进行sha256算法摘要
    unsigned char md[WHICH_DIGEST_LENGTH];

    SHA256((unsigned char *)data, strlen(data), md);
    //printHex(md, WHICH_DIGEST_LENGTH);

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

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

    size_t plainlen = 0;
    Base64Encode(buf, nOutLen, plaintext, &plainlen);
    //printf(" buf2:%s\n",plaintext);


quit:
    RSA_free(privKey);

    return 0;
}


int main(int argc, char *argv[])
{
    uint8_t plaintext[2048];
    char *data = "aaaaa";
    test_RSA_sign(plaintext,data);
    printf("buf2:%s\n",plaintext);
    return 0;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值