// OpensslTest2.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <string> #include <openssl/pem.h> #include <openssl/rsa.h> //解决错误:OPENSSL_Uplink(0098E000,07): no OPENSSL_Applink 错误分析 //有兴趣的可以去此blog看下:http://blog.csdn.net/ecjtuync/article/details/3278995 extern "C" { #include <openssl/applink.c> } typedef unsigned char BYTE; const int RSA_LEN = 2048; const int DATA_LEN = 128; /*加密最大长度为加密长度-41*/ bool GetPubKey(RSA * pRsa); bool GetPrivKey(RSA * pRsa); bool CreateKeys(); bool SavePubKey(RSA *rsaKey); bool SavePrivKey(RSA *rsaKey); int RsaEncrypt(int nLen,BYTE * pData,BYTE *pEncrypt,RSA *rsa,int padding = RSA_PKCS1_PADDING); int RsaDecrypt(int nLen,BYTE * pData,BYTE *pDecrypt,RSA *rsa,int padding = RSA_PKCS1_PADDING); //加密文件 const char pub_key[] = "pub.key"; const char priv_key[] = "priv.key"; RSA* pubRsa = NULL; RSA *privRsa = NULL; int _tmain(int argc, _TCHAR* argv[]) { // 原始明文 char plain[256]="测试测试,hello123"; // 用来存放密文 char encrypted[RSA_LEN] = {0}; // 用来存放解密后的明文 char decrypted[RSA_LEN] = {0}; // ------------------------------------------------------- // 利用公钥加密明文的过程 // ------------------------------------------------------- //创建keys if(!CreateKeys()) { printf("CreateKeys false\n"); return -1; } //保存keys if(!SavePubKey(pubRsa)) { printf("GetPubKey false\n"); return -1; } if(!SavePrivKey(privRsa)) { printf("SavePrivKey false\n"); return -1; } //解析keys if(!GetPubKey(pubRsa)) { printf("GetPubKey false\n"); return -1; } if(!GetPrivKey(privRsa)) { printf("GetPrivKey false\n"); return -1; } //检测需要加密的数据的长度 if(strlen(plain)>=RSA_size(pubRsa)-41){ printf("check input len false\n"); return -1; } //加密 if(RsaEncrypt(DATA_LEN,(BYTE *)plain,(BYTE *)encrypted,pubRsa) < 0) { printf("RsaEncrypt false\n"); return -1; } printf("RsaEncrypt:\n%s\n",encrypted); //解密 if(RsaDecrypt(RSA_size(privRsa),(BYTE *)encrypted,(BYTE *)decrypted,privRsa) < 0) { printf("RsaDecrypt false\n"); return -1; } printf("RsaDecrypt:%s\n",decrypted); getchar(); return 0; } /*加密最大长度为加密长度-41*/ bool GetPubKey(RSA * pRsa) { // 打开公钥文件 FILE* pub_fp=fopen(pub_key,"r"); if(pub_fp==NULL){ printf("failed to open pub_key file %s!\n", pub_key); return false; } // 从文件中读取公钥 pRsa=PEM_read_RSA_PUBKEY(pub_fp, NULL, NULL, NULL); fclose(pub_fp); if(pubRsa==NULL){ printf("unable to read public key!\n"); return false; } return true; } bool GetPrivKey(RSA * pRsa) { // 打开私钥文件 FILE* priv_fp=fopen(priv_key,"r"); if(priv_fp==NULL){ printf("failed to open priv_key file %s!\n", priv_key); return false; } // 从文件中读取私钥 pRsa = PEM_read_RSAPrivateKey(priv_fp, NULL, NULL, NULL); fclose(priv_fp); if(pRsa==NULL){ printf("unable to read private key!\n"); return false; } return true; } //RSA_public_encrypt(strlen(plain), (unsigned char *)plain, (unsigned char *)encrypted, rsa1, RSA_PKCS1_PADDING); int RsaEncrypt(int nLen,BYTE * pData,BYTE *pEncrypt,RSA *rsa,int padding) { return RSA_public_encrypt(nLen, pData, pEncrypt, rsa, padding); } //RSA_public_encrypt(strlen(plain), (unsigned char *)plain, (unsigned char *)encrypted, rsa1, RSA_PKCS1_PADDING); int RsaDecrypt(int nLen,BYTE * pData,BYTE *pDecrypt,RSA *rsa,int padding) { return RSA_private_decrypt(nLen, pData, pDecrypt, rsa, padding); } bool CreateKeys() { RSA* pRsa = RSA_generate_key( RSA_LEN ,RSA_F4,NULL,NULL); //生成RSA密钥 pubRsa = RSAPublicKey_dup(pRsa); privRsa = RSAPrivateKey_dup(pRsa); if(pRsa == NULL) { printf("RSA_generate_key false\n"); return false; } if(pubRsa == NULL) { printf("RSAPublicKey_dup false\n"); return false; } if(privRsa == NULL) { printf("RSAPrivateKey_dup false\n"); return false; } return true; } bool SavePrivKey(RSA *rsaKey) { FILE *file = NULL; file = fopen(priv_key,"wb"); if(file == NULL) return false; if(rsaKey == NULL) return false; PEM_write_RSAPrivateKey(file, rsaKey, NULL, NULL, RSA_LEN, NULL, NULL); fclose(file); return true; } bool SavePubKey(RSA *rsaKey) { FILE *file = NULL; file = fopen(pub_key,"wb"); if(file == NULL) return false; if(rsaKey == NULL) return false; PEM_write_RSAPublicKey(file, rsaKey); fclose(file); return true; }