调用OpenSSL实现数字签名功能例程(一)
2012-04-23 15:29
411 查看
// 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; }
相关文章推荐
- 调用OpenSSL实现数字签名功能例程(二)
- 调用OpenSSL实现数字签名功能例程(一)
- 调用OpenSSL实现数字签名功能例程(二)
- 调用OpenSSL实现数字签名功能例程(一)
- 调用OpenSSL实现数字签名功能例程(二)
- 调用OpenSSL实现数字签名功能例程(二)
- 调用OpenSSL实现数字签名功能例程(二)
- 调用OpenSSL实现RSA加解密和签名操作
- 使用openssl实现ECDSA签名以及验证功能(附完整测试源码)
- 想用rxjava+rxandroid 替换handle实现数字时钟,出现无响应,不能隔一秒调用onNext
- 小字典之MFC简单的多线程调用类的成员函数实现逐步画线功能 VS2015 C++ 完成
- DSA数字签名原理及JAVA实现
- Android 调用已安装市场,进行软件评分的功能实现
- 利用ST提供的USB例程实现USB IAP功能
- java调接口实现发送手机短信验证码功能,手机验证码,接口调用
- angular 实现的输入框数字千分位及保留几位小数点功能示例
- Java2下Applet数字签名具体实现方法
- java调接口实现发送手机短信验证码功能,手机验证码,接口调用
- SetWaitableTimer 通过异步程序调用(APC)实现的定时功能