⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 openssl.c

📁 openssl加密例子
💻 C
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#define OPENSSL_C /* tells apps.h to use complete apps_startup() */
#include "apps.h"
#include <openssl/bio.h>
#include <openssl/crypto.h>
#include <openssl/lhash.h>
#include <openssl/conf.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#ifndef OPENSSL_NO_ENGINE
#include <openssl/engine.h>
#endif
#define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
#include "progs.h"
#include "s_apps.h"
#include <openssl/err.h>
#include <openssl/fips.h>

/* Make sure there is only one when MONOLITH is defined */
#ifdef MONOLITH
CONF *config=NULL;
BIO *bio_err=NULL;
int in_FIPS_mode=0;
#endif

int main(void)
{
	ARGS arg;
#define PROG_NAME_SIZE	39
	char *to_free=NULL;
	int i,ret=0;
	char *p;
	LHASH *prog=NULL;
	long errline;

	int Signc = 3;
	char *Signv[] = {"-sign", "-inkey", "bprv.pem"/*,"-in","test.txt","-out","test.sig"*/};
	int Verifyc = 4;
	char *Verifyv[] = {"-verify", "-pubin", "-inkey", "bpub.pem"/*,"-in","test.sig","-out","test.ver"*/};
	int Encryptc = 4;
	char *Encrypt[] = {"-encrypt", "-pubin", "-inkey", "bpub.pem"};
	int Decryptc = 3;
	char *Decrypt[] = {"-decrypt", "-inkey", "bprv.pem"};
	int ch,inlen,outlen,ret_code;
	char inbuf[4096],outbuf[4096];

	struct stat f_stat;
	int fd;
	char sign_name[255] = "./sign.txt";
	char verify_name[255] = "./ver.txt";
	char enc_name[255] = "./enc.txt";
	char dec_name[255] = "./dec.txt";

	arg.data=NULL;
	arg.count=0;

	in_FIPS_mode = 0;

	for(;;)
	{
		printf("\n");
		printf("\t\t         a. sign \n");
		printf("\t\t         b. verify \n");
		printf("\t\t         c. encrypt \n");
		printf("\t\t         d. decrypt \n");
		printf("\t\t         q. EXIT           \n");
		ch=fgetc(stdin);
		switch(ch)
		{
		case 'q': 
			return(0);
		case 'a':
			strcpy(inbuf,"100000000000000023wwwwwwwwwwwwwww100000000000000023wwwwwwwwwwwwwww100000000000000023wwwwwwwwwwwwwww100000000000000023wwwwwwwwwwwwwww");
			inlen=strlen(inbuf);
			outlen=0;
			memset(outbuf,0,sizeof(outbuf));
			ret_code=rsautl_main(Signc,Signv,inbuf,&inlen,outbuf,&outlen);
			fd=open(sign_name, O_RDWR | O_CREAT, 0660);
			write(fd, outbuf, outlen);
			close(fd);
			break;
		case 'b':
			memcpy(inbuf, outbuf, outlen);
			inlen=outlen;
			
			fd=open(sign_name, O_RDWR | O_CREAT, 0660);
			if(stat(sign_name, &f_stat) < 0) {
				printf("stat err:%s", sign_name);
				exit(1);
			}
			inlen = f_stat.st_size;
			read(fd, inbuf, inlen);
			close(fd);
			outlen=0;
			memset(outbuf,0,sizeof(outbuf));
			ret_code=rsautl_main(Verifyc,Verifyv,inbuf,&inlen,outbuf,&outlen);
			fd = open(verify_name, O_RDWR | O_CREAT, 0660);
			write(fd, outbuf, outlen);
			close(fd);
			break;
		case 'c':
			strcpy(inbuf,"100000000000000023wwwwwwwwwwwwwww");
			inlen=strlen(inbuf);
			outlen=0;
			memset(outbuf,0,sizeof(outbuf));
			ret_code=rsautl_main(Encryptc,Encrypt,inbuf,&inlen,outbuf,&outlen);
			fd=open(enc_name, O_RDWR | O_CREAT, 0660);
			write(fd, outbuf, outlen);
			close(fd);
			break;
		case 'd':
			memcpy(inbuf, outbuf, outlen);
			inlen = outlen;
			fd=open(enc_name, O_RDWR | O_CREAT, 0660);
			if(stat(sign_name, &f_stat) < 0) {
				printf("stat err:%s", sign_name);
				exit(1);
			}
			inlen = f_stat.st_size;
			read(fd, inbuf, inlen);
			close(fd);
			outlen = 0;
			memset(outbuf, 0, sizeof(outbuf));
			ret_code=rsautl_main(Decryptc,Decrypt,inbuf,&inlen,outbuf,&outlen);
			fd=open(dec_name, O_RDWR | O_CREAT, 0660);
			write(fd, outbuf, outlen);
			close(fd);
			break;
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -