使用openssl实现ras-密钥创建-加密-解密(c语言)

一、下载安装过程
openssl下载安装过程
二、使用介绍
1-使用指令实现
/*******************************************************************************************
**1.openssl genrsa -out test.key 1024 —>生成一个密钥
在这里插入图片描述

**2.openssl rsa -in test.key -pubout -out test_pub.key —>提取密钥中的公钥
在这里插入图片描述
**3.openssl rsautl -encrypt -in hello -inkey test_pub.key -pubin -out hello.en
** —>-in指定要加密的文件,-inkey指定密钥,-pubin表明是用纯公钥文件加密,-out为加密后的文件。
在这里插入图片描述

**4.openssl rsautl -decrypt -in hello.en -inkey test.key -out hello.de
** —>-in指定被加密的文件,-inkey指定私钥文件,-out为解密后的文件。
在这里插入图片描述

********************************************************************************************/

2.c代码实现

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

#include<openssl/rsa.h>
#include<openssl/pem.h>
#include<openssl/err.h>
#include<openssl/bio.h>
#include<openssl/evp.h>


#define PUB_KEY  "./public_key.txt"
#define PRI_KEY  "./prive_key.txt"


/*************************
**1---------------创建密钥
**************************/
void creat_key()
{
   
   	
	RSA *r = RSA_new();//初始化RSA结构体
	int bits=1024;
	BIGNUM *e = BN_new();//新生成一个BIGNUM结构
	BN_set_word(e,65537);//设置e为65537 

/***
**RSA_generate_key_ex:生成一对钥匙
**参数1:RSA结构         参数2:大小
**参数3:BIGNUM结构 参数4:一般置空
******/
	RSA_generate_key_ex(r, bits, e, NULL);

/****
**int RSA_print_fp(FILE *fp, const RSA *r,int offset);
**							-----将生成的密钥输出到文件
**参数1:文件fp指针              参数2:RSA结构体
**参数3:打印偏移量offset是为了调整输出格式
******/
	//RSA_print_fp(stdout,r,0);
	
	BIO *out;
	
/****
**BIO *BIO_new_file(const char *filename, const char *mode);
*****/
	out = BIO_new_file
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值