IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> sha256withrsa 的C语言实现 -> 正文阅读

[C++知识库]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;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-17 15:11:24  更:2021-08-17 15:15:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 10:12:23-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码