c调用openssl从pem文件获取公钥私钥加解密

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
#define PUBLICKEY "public.pem"
#define OPENSSLKEY "private.pem"
#define BUFFSIZE 1024
char *my_encrypt(char *str, char *path_key); //加密
char *my_decrypt(char *str, char *path_key); //解密
int main(void)
{
	char *source = "i like dancing !!!";
	char *ptf_en, *ptf_de;
	printf("source is :%s\n", source);
	//1.加密
	ptf_en = my_encrypt(source, PUBLICKEY);
	if (ptf_en == NULL){
		return 0;
	}else{
		printf("ptf_en is :%s\n", ptf_en);
	}
	//2.解密
	ptf_de = my_decrypt(ptf_en, OPENSSLKEY);
	if (ptf_de == NULL){
		return 0;
	}else{
		printf("ptf_de is :%s\n", ptf_de);
	}
	if(ptf_en) free(ptf_en);
	if(ptf_de) free(ptf_de);
	return 0;
}
//加密
char *my_encrypt(char *str, char *path_key)
{
	char *p_en = NULL;
	RSA *p_rsa = NULL;
	FILE *file = NULL;
	int length = 0; //flen为源文件长度, rsa_len为秘钥长度
	//1.打开秘钥文件
	if((file = fopen(path_key, "rb")) == NULL)
	{
		perror("fopen() error 111111111 ");
		goto End;
	}
	//2.从公钥中获取 加密的秘钥
	if((p_rsa = PEM_read_RSAPublicKey(file, NULL,NULL,NULL )) == NULL)
	{
		ERR_print_errors_fp(stdout);
		goto End;
	}

	length = strlen(str);
	p_en = (char *)malloc(256);
	if(!p_en)
	{
		perror("malloc() error 2222222222");
		goto End;
	}
	memset(p_en, 0, 256);
	//5.对内容进行加密
	if(RSA_public_encrypt(length, (unsigned char*)str, (unsigned char*)p_en, p_rsa, RSA_PKCS1_PADDING) < 0)
	{
		perror("RSA_public_encrypt() error 2222222222");
		goto End;
	}
End:
	//6.释放秘钥空间, 关闭文件
	if(p_rsa) RSA_free(p_rsa);
	if(file) fclose(file);
	return p_en;
}
//解密
char *my_decrypt(char *str, char *path_key)
{
	char *p_de = NULL;
	RSA *p_rsa = NULL;
	FILE *file = NULL;
	//1.打开秘钥文件
	file = fopen(path_key, "rb");
	if(!file)
	{
		perror("fopen() error 22222222222");
		goto End;
	}
	//2.从私钥中获取 解密的秘钥
	if((p_rsa = PEM_read_RSAPrivateKey(file, NULL,NULL,NULL )) == NULL)
	{
		ERR_print_errors_fp(stdout);
		goto End;
	}

	p_de = (char *)malloc(245);
	if(!p_de)
	{
		perror("malloc() error ");
		goto End;
	}
	memset(p_de, 0, 245);
	//5.对内容进行解密
	if(RSA_private_decrypt(RSA_size(p_rsa), (unsigned char*)str, (unsigned char*)p_de, p_rsa, RSA_PKCS1_PADDING) < 0)
	{
		perror("RSA_public_encrypt() error ");
		goto End;
	}
End:
	//6.释放秘钥空间, 关闭文件
	if(p_rsa) RSA_free(p_rsa);
	if(file) fclose(file);
	return p_de;
}

编译:gcc sslrsa.c -o sslrsa.o -lcrypto

在Python中,你可以使用OpenSSL库来处理RSA加密和解密。首先,你需要安装`pyOpenSSL`库,可以使用pip进行安装: ```bash pip install pyopenssl ``` 以下是生成公钥私钥、加密和解密的基本步骤: 1. **导入必要的模块**: ```python from OpenSSL import crypto ``` 2. **生成RSA密钥对**: ```python key = crypto.PKey() key.generate_key(crypto.TYPE_RSA, 2048) # 生成2048位的密钥 private_key = crypto.dump_privatekey(crypto.FILETYPE_PEM, key) public_key = crypto.dump_publickey(crypto.FILETYPE_PEM, key) ``` 3. **保存公钥私钥**: - 私钥通常需要安全地存储,不应该明文发布。 - 公钥可以公开分享用于接收者的验证。 4. **加载公钥私钥**(如果需要): ```python def load_keys(private_key_path, public_key_path): with open(private_key_path, "rb") as f: private_key = crypto.load_privatekey(crypto.FILETYPE_PEM, f.read()) with open(public_key_path, "rb") as f: public_key = crypto.load_publickey(crypto.FILETYPE_PEM, f.read()) return private_key, public_key # 使用时传入文件路径替换这里的路径 private_key, public_key = load_keys("path_to_your_private.pem", "path_to_your_public.pem") ``` 5. **加密示例**: ```python def encrypt(message, public_key): encrypted = public_key.encrypt(message.encode(), 32) # RSA OAEP padding return encrypted # 解密示例 def decrypt(encrypted_message, private_key): decrypted = private_key.decrypt(encrypted_message) return decrypted.decode() ``` 6. **调用上述函数并传递数据**: ```python message = "Hello, world!" encrypted = encrypt(message, public_key) decrypted = decrypt(encrypted, private_key) print(f"原始消息: {message}") print(f"加密后: {encrypted.hex()}") print(f"解密后: {decrypted}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值