【求助】EDK2的OPENSSL库问题

[复制链接]
craike 发表于 2018-9-19 21:58:06 | 显示全部楼层 |阅读模式
如下代码,如果用gcc直接编译或者vs,都能完整跑通,可以加密可以解密,但是换到edk2上,挂在CryptoPkg下,编译出来,shell下运行,加密成功,解密失败
尝试了能百度谷歌到的所有代码和方法,均是失败,还请高手赐教,这个key没有问题,我特意导出rsa结构体的b,n,d,验证过的,key的格式没问题

请高手赐教

[C] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>

const char *publicKey = "-----BEGIN PUBLIC KEY-----\n\
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqu3MuVY88pNf/vlAwBNh\n\
K5/uSONbuGpIgG2Q/bbrH+rUytEHi+kij8lL8ZPjG3/OZckjf8b6KNFZMnpftiCj\n\
t8FKmABdSR6Fj0ICTOpFxSUxGFRlPPdxnpE771mQMPu5UBO565F51KrmQfQgkYHC\n\
hoGuoCzerKq8ziNucRoYwmiVJNN//rHToWOkA1I4YSdjzHhy65Dn1H+6lCVGKltP\n\
30i44xD3rVYfqEwe37ASHv+jGtPqsWrj6ov0cXkVLJEfwn4azODOvYq+FkscoDgA\n\
/kDMi2klPj44NQtmKSq5u05RzskOXIMnTJMNlpBNABhmEcV35wB+W/ixPnKpKgI6\n\
8NJce75fwq7bgGnWarjgG4nqDiKqQKF2MGcCqVDU9xVk9XcjOBxdz8coj5LYkLpN\n\
3ryRy1G3vqiDOez1NGkGN1/d6pbeV4oIRYEvEiYeioQedzB0+R9HpZY6GIKpedhj\n\
8pXyy3/cLACJMFa5By46kbUqG/InbVtnwi4J5MNYa4xMMIYmUpzLsmz/w4IWRdZ1\n\
1mBghHX8CLui4kqyihY3JO0d3t3I1baiyhjbDiiBc5ND7NdQMM0xzOHWLnWFRSqz\n\
KVbYMEGxeZFLqNKdA9Mn1h8vIoywKXVPqbU4iTweeHtxDlLUk1kxuZTL+QPbQMKl\n\
Wi+Y2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8NVKx1JASyYlo7xc0S5F/9do3idb8oFU4KPqW85+ipBim4Ng\n\
NlwRzAJhsfH/esPD/dhwJliZwmYmwGF4f30f8a+1dYrx/zcA9X8anYzYgMVb3YlQ\n\
0UzAB2vsCTggF/rE4A0gWzfvrmuU3WDWqfQ4JFW7Y/plXoBiqv8k4yYptCe9Vw==\n\
-----END RSA PRIVATE KEY-----";


 
int main(int argc, char *argv[])
{
	BIO *bio = NULL;
	RSA *publicRsa = NULL;
	RSA *privateRsa = NULL;
	if ((bio = BIO_new_mem_buf((void *)publicKey, -1)) == NULL)
	{
		printf("BIO_new_mem_buf publicKey error\n");
		return -1;
	} 	
   
	if ((publicRsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)) == NULL) 
	{
		printf("PEM_read_bio_RSA_PUBKEY error\n");
		return -1;
	}
	BIO_free_all(bio);
	
	if ((bio = BIO_new_mem_buf((void *)privateKey, -1)) == NULL)
	{
		printf("BIO_new_mem_buf privateKey error\n");
		return -1;
	}
	OpenSSL_add_all_algorithms();//密钥有经过口令加密需要这个函数
	if ((privateRsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL) == NULL) 
	{
		printf("PEM_read_RSAPrivateKey error\n");
		return NULL;
	}
	BIO_free_all(bio);
	
	unsigned char *source = (unsigned char *)"123456789";
		
	int rsa_len = RSA_size(publicRsa);
 
	unsigned char *encryptMsg = (unsigned char *)malloc(rsa_len);
	memset(encryptMsg, 0, rsa_len);
 		
	int len = rsa_len - 11;
 		
	if (RSA_public_encrypt(len, source, encryptMsg, publicRsa, RSA_PKCS1_PADDING) < 0)
		printf("RSA_public_encrypt error\n");
	else 
	{
		rsa_len = RSA_size(privateRsa);
		unsigned char *decryptMsg = (unsigned char *)malloc(rsa_len);
		memset(decryptMsg, 0, rsa_len);
	    
		int mun =  RSA_private_decrypt(rsa_len, encryptMsg, decryptMsg, privateRsa, RSA_PKCS1_PADDING);
	 
		if ( mun < 0)
			printf("RSA_private_decrypt error\n");
		else
			printf("RSA_private_decrypt %s\n", decryptMsg);
	}	
	
	RSA_free(publicRsa);
	RSA_free(privateRsa);
	return 0;
}




上一篇:英特尔:给你一份邀请函,共享工程师的大脑。
下一篇:2018年9月20日签到记录贴
 楼主| craike 发表于 2018-9-19 22:30:59 | 显示全部楼层
有没有大佬帮解答
回复

使用道具 举报

本版积分规则

QQ|Archiver|手机版|小黑屋|RD之家 - 研发工程师的伊甸园 ( 京ICP备18037383号 )
360导航 360安全浏览器 蚂蚁搜索 速搜全球 酷帝网站目录 搜狗导航 114啦网址导航

GMT+8, 2018-10-22 18:29

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表