rsa-c++

转载 2016年08月29日 00:11:14

rsa-c++

#include <stdio.h>
#include <stdlib.h>

#include <openssl/rsa.h>
#include<openssl/pem.h>
#include<openssl/err.h>
#include <openssl/bio.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
#pragma comment(lib, "libeay32.lib")
#pragma comment(lib, "ssleay32.lib")

void generateKey() {

	/* 生成公钥 */
	RSA* rsa = RSA_generate_key( 1024, RSA_F4, NULL, NULL);
	BIO *bp = BIO_new( BIO_s_file() );
	BIO_write_filename( bp, "public.pem" );
	PEM_write_bio_RSAPublicKey(bp, rsa);
	BIO_free_all( bp );
	/* 生成私钥 */
	char passwd[]="1234";
	bp = BIO_new_file("private.pem", "w+");
	PEM_write_bio_RSAPrivateKey(bp, rsa, EVP_des_ede3(), (unsigned char*)passwd, 4, NULL, NULL);
	BIO_free_all( bp );
	RSA_free(rsa);
}

std::string bio_read_privateKey(string data) {
	OpenSSL_add_all_algorithms();
    
    BIO* bp = BIO_new( BIO_s_file() );
	
    BIO_read_filename( bp, "private.pem" );
    
    char passwd[]="1234";
    RSA* rsaK = PEM_read_bio_RSAPrivateKey( bp, NULL, NULL, passwd );
	if (NULL == rsaK) {
		perror("read key file fail!");
	}else{
		printf("read success!\n");
	}

	int nLen = RSA_size(rsaK);
	//printf("len:%d\n",nLen);
	char *pEncode = new char[nLen + 1];
	int ret = RSA_private_decrypt(data.length(),(const unsigned char*)data.c_str(),(unsigned char *)pEncode,rsaK,RSA_PKCS1_PADDING);
	std::string strRet;
	if (ret >= 0) {
		strRet = std::string(pEncode, ret);
		//printf("%s",strRet.c_str());
	}
	
	delete[] pEncode;
	CRYPTO_cleanup_all_ex_data();
	BIO_free_all( bp );
	RSA_free(rsaK);
	return strRet;
}

std::string bio_read_publicKey(string data){
	OpenSSL_add_all_algorithms();
    BIO* bp = BIO_new( BIO_s_file() );
    BIO_read_filename( bp, "public.pem" );
    RSA* rsaK = PEM_read_bio_RSAPublicKey( bp, NULL, NULL, NULL );
	if (NULL == rsaK) {
		perror("read key file fail!");
	}else{
		printf("read success!");
		int nLen = RSA_size(rsaK);
		printf("len:%d\n",nLen);
	}
	int nLen = RSA_size(rsaK);
	char *pEncode = new char[nLen + 1];
	int ret = RSA_public_encrypt(data.length(),(const unsigned char*)data.c_str(),
		(unsigned char *)pEncode,rsaK,RSA_PKCS1_PADDING);
	std::string strRet;
	if (ret >= 0) {
		strRet = std::string(pEncode, ret);
		//printf("%s\n",strRet.c_str());
	}
	delete[] pEncode;
	CRYPTO_cleanup_all_ex_data();
	BIO_free_all( bp );
	RSA_free(rsaK);
	return strRet;
}

void encryptFile(string inputfile,string outputfile){
	ifstream file(inputfile.c_str());
	ofstream outfile(outputfile.c_str());
	string tsum;
	string ss;
	while (getline(file,ss)) {
		tsum.append(ss.append("\n"));
	}
	cout<<"徐加密内容:"<<tsum<<endl;
	string mw = bio_read_publicKey(tsum);
	cout<<mw<<endl;
	outfile<<mw;
	outfile.flush();
	outfile.close();
	file.close();
}

void decryptFile(string inputfile,string outputfile){
	ifstream file(inputfile.c_str());
	ofstream outfile(outputfile.c_str());
	std::string tsum,ss;
	while (getline(file,ss)) {
		tsum.append(ss);
	}
	std::string cw = bio_read_privateKey(tsum);
	cout<<"恢复明文:"<<cw;
	outfile<<cw;
	outfile.flush();
	outfile.close();
	file.close();
}

int main() {
	char *str = "第一步,首先需要在openssl官网下载openssl包http://www.openssl.org/source/;\n第二步,自己查资料去!";
	//system("openssl genrsa -out private.key 1024");
	//generateKey();bio_read_publicKey();
	printf("原文:%s\n",str);
	std::string m = bio_read_publicKey(str);
	printf("密文:\n------------%s--------------\n\n",m.c_str());
	string miwen = m;
	std::string c = bio_read_privateKey(miwen);
	printf("解密后:\n------------%s--------------\n\n",c.c_str());
	
	printf("----------------加密文件--------------------------\n");
	encryptFile("d:/before.txt","f:/my.txt");
	cout<<"------------------done-------------------------------"<<endl;
	/*
	ifstream infile("f:/my.txt");
	std::string instr,intemp;
	while (getline(infile,intemp)) {
		instr.append(intemp);
	}
	cout<<instr<<endl;
	std::string cwen = bio_read_privateKey(instr);
	cout<<cwen;
	*/
	printf("-----------------解密文件----------------------------\n");
	decryptFile("f:/my.txt","f:/jiemihou.txt");
	cout<<"------------------done-------------------------------"<<endl;
	
	return 0;
}


相关文章推荐

C++利用Crypto++,vs2005环境下的RSA应用

固定生成随机密钥,存储和装载。解决加密时字符串长度问题

RSA算法的c++实现(简化版)

#include #include #include using namespace std; //encrypt void encrypt(char *str,int pk,int n,int *a...

Visual Studio C++ RSA加解密方案

本文的应用场景是,Unity c#向vc++ 工程传递用户名和密码,可能会面临的安全性问题:假如第三方对dll进行替换,c#的账号密码直接往下传,可能导致用户密码泄露的风险。针对此种情况,对用户名和密...

【语言-c++】RSA加密算法 (C++)

RSAEx.h /*************************************************/ /*********Author:Cherish ***************...

四种加密算法之RSA源代码-C++

RSA.h文件: [cpp] view plaincopy //! RSA 动态链接库实现   H文件   /*!   @author ...

C++实现RSA加密解密

RSA算法的描述
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)