调用OpenSSL实现数字签名功能例程(一)

本文提供了一个使用OpenSSL实现数字签名功能的C++代码示例,详细介绍了初始化OpenSSL、获取签名、验证签名的过程,并通过一个简单的主函数展示了整个流程的应用。

// sign.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <stdio.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/objects.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>

#pragma comment(lib, "libeay32.lib")  
#pragma comment(lib, "ssleay32.lib")  

/*
PKCS7Sign.cpp
Auth:Kagula
功能:调用OpenSSL实现数字签名功能例程(一)
环境:VS2008+SP1,OpenSSL1.0.1
*/

void InitOpenSSL()
{
 ERR_load_crypto_strings();
}

unsigned char * GetSign(char* keyFile,char* plainText,unsigned char* cipherText,unsigned int *cipherTextLen)

 FILE* fp = fopen (keyFile, "r");
 if (fp == NULL)
  return NULL;

 /* Read private key */
 EVP_PKEY* pkey = PEM_read_PrivateKey(fp, NULL, NULL, NULL);
 fclose (fp);

 if (pkey == NULL) {
  ERR_print_errors_fp (stderr);
  return NULL;
 }

 /* Do the signature */
 EVP_MD_CTX     md_ctx;
 EVP_SignInit   (&md_ctx, EVP_sha1());
 EVP_SignUpdate (&md_ctx, plainText, strlen(plainText));
 int err = EVP_SignFinal (&md_ctx, cipherText, cipherTextLen, pkey);

 if (err != 1) {
  ERR_print_errors_fp(stderr);
  return NULL;
 }

 EVP_PKEY_free(pkey);

 return cipherText;
}

bool VerifySign(char* certFile,unsigned char* cipherText,unsigned int cipherTextLen,char* plainText)
{
 /* Get X509 */
 FILE* fp = fopen (certFile, "r");
 if (fp == NULL)
  return false;
 X509* x509 = PEM_read_X509(fp, NULL, NULL, NULL);
 fclose (fp);

 if (x509 == NULL) {
  ERR_print_errors_fp (stderr);
  return false;
 }

 /* Get public key - eay */
 EVP_PKEY *pkey=X509_get_pubkey(x509);
 if (pkey == NULL) {
  ERR_print_errors_fp (stderr);
  return false;
 }

 /* Verify the signature */
 EVP_MD_CTX md_ctx;
 EVP_VerifyInit   (&md_ctx, EVP_sha1());
 EVP_VerifyUpdate (&md_ctx, plainText, strlen((char*)plainText));
 int err = EVP_VerifyFinal (&md_ctx, cipherText, cipherTextLen, pkey);
 EVP_PKEY_free (pkey);

 if (err != 1) {
  ERR_print_errors_fp (stderr);
  return false;
 }
 return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
 char certFile[] = "cert.pem";//含共匙
 char keyFile[]  = "key.pem";//含私匙

 char plainText[]     = "I owe you...";//待签名的明文
 unsigned char cipherText[1024*4];
 unsigned int cipherTextLen;

 InitOpenSSL();

 memset(cipherText,0,sizeof(cipherText));
 if(NULL==GetSign(keyFile,plainText,cipherText,&cipherTextLen))
 {
  printf("签名失败!\n");
  return -1;
 }

 if(false==VerifySign(certFile,cipherText,cipherTextLen,plainText))
 {
  printf("验证签名失败!\n");
  return -2;
 }


 printf ("Signature Verified Ok.\n");
 return 0;
}

面向工程应用:市面上的些密码学课程和密码学的书籍,很多都是从考证出发,讲解算法原理并不面向工程应用,而我们现在缺少的是工程应用相关的知识,本课程从工程应用出发,每种技术都主要讲解其在工程中的使用,并演示工程应用的代码。 从零实现部分算法: 课程中实现了base16编解码 ,XOR对称加解密算法,PKCS7 pading数据填充算法,通过对些简单算法的实现,从而加深对密码学的理解。理论与实践结合: 课程如果只是讲代码,同学并不能理解接口背后的原理,在项目设计中就会留下隐患,出现错误也不容易排查出问题。如果只讲理论,比如对密码学的些研究,对于大部分从事工程应用的同学并没有必要,而是理论与实践结合,切为了工程实践。代码现场打出: 代码不放在ppt而是现场打出,更好的让学员理解代码编写的逻辑,老师现场敲出代码正是展示出了工程项目的思考,每个步骤为什么要这么做,考虑了哪些异常,易学不枯燥: 课程为了确保大部分人开发者都学得会,理解算法原理(才能真正理解算法特性),学会工程应用(接口调用,但不局限接口调用,理解接口背后的机制,并能解决工程中会出现的问题),阅读算法源码但不实现密码算法,,并能将密码学投入到实际工程中,如果是想学习具体的加密算法实现,请关注我后面的课程。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值