【rsa】MD5withRSA和sha256withRSA签名算法

1.MD5withRSA

#include 
#include 
#include 
#include 
#include 
#include 
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 
#include 
#include 
#include 
#include 
#include 
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;
}

你可能感兴趣的:(算法,c语言)