【rsa】MD5withRSA和sha256withRSA签名算法

1.MD5withRSA

#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#include <openssl/md5.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 MD5_WHICH        NID_md5
#define WHICH_DIGEST_LENGTH    MD5_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("");
}
void printHex2(unsigned char *md, int len)
{

    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%x", 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];

    MD5((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(MD5_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 = "authorizationCode=8783a8eca4ca971c6c9679eb30906c5c&mobile=17343448975&portalCode=1449930122433007616&userId=1000926576";
    test_RSA_sign(plaintext,data);
    printf("buf2:%s\n",plaintext);
    return 0;
}

2.sha256withRSA

#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("");
}
void printHex2(unsigned char *md, int len)
{

    int i = 0;
    for (i = 0; i < len; i++)
    {
        printf("%x", 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 = "authorizationCode=8783a8eca4ca971c6c9679eb30906c5c&mobile=17343448975&portalCode=1449930122433007616&userId=1000926576";
    test_RSA_sign(plaintext,data);
    printf("buf2:%s\n",plaintext);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值