-
-
Openssl中RSA 签名的疑问
-
发表于:
2012-2-15 18:22
7170
-
我没有使用RSA_Sign 函数,它无非是把私钥加密公钥认证封装了一层而已, 我的疑问是 私钥(d)加密的时候一定要有俩公钥(n, e)作为参数吗? 如果我去掉红色的3行就会失败。。。谁能解答一下疑问。
#include "stdafx.h"
#include "windows.h"
#include <openssl\rsa.h>
#pragma comment(lib, "libeay32.lib")
//1.私钥加密
bool PrivateEn(int isize, char *szOir, char *szEn, char * D, char * N)
{
RSA * r = RSA_new();
BIGNUM * bd = BN_new();
BIGNUM * bn = BN_new();
[COLOR="Red"] BIGNUM * be = BN_new(); //add by kyle ----- 1[/COLOR]
BN_hex2bn(&bd, D);
BN_hex2bn(&bn, N);
[COLOR="red"] BN_hex2bn(&be, "47"); //add by kyle ----- 2[/COLOR]
r->d = bd;
r->n = bn;
[COLOR="red"] r->e = be; ////add by kyle ----- 3[/COLOR]
if (isize != RSA_size(r))
{
RSA_free(r);
return false;
}
if (RSA_private_encrypt(isize, (const unsigned char *)szOir, (unsigned char *)szEn, r, RSA_NO_PADDING) < 0)
{
RSA_free(r);
return false;
}
RSA_free(r);
return true;
}
//2.公钥解密
bool PublicDe(int isize, char *szEn, char *szDe, char * E, char * N)
{
RSA * r = RSA_new();
BIGNUM * bn = BN_new();
BIGNUM * be = BN_new();
BN_hex2bn(&bn, N);
BN_hex2bn(&be, E);
r->n = bn;
r->e = be;
if (isize != RSA_size(r))
{
RSA_free(r);
return false;
}
if (RSA_public_decrypt(isize, (const unsigned char *)szEn, (unsigned char *)szDe, r, RSA_NO_PADDING) < 0)
{
RSA_free(r);
return false;
}
RSA_free(r);
return true;
}
int main(int argc, char* argv[])
{
//p = 0xc95f
//q = 0xcedd
#define E "47" //0x47 == 71d
//N = p*q 公钥
#define N "A2B84903"
//d = e^MOD((p-1)(q-1)) 私钥
#define D "24AAF8F7"
char szOir[100] = {"abcd"};
char szEn[100] = {0};
char szDe[100] = {0};
bool bRet = false;
//1.私钥加密
bRet = PrivateEn(4, szOir, szEn, D, N);
if (bRet == false)
{
printf("PrivateEn error");
return 0;
}
//2.公要解密
bRet = PublicDe(4, szEn, szDe, E, N);
if (bRet == false)
{
printf("PublicDe error.");
return 0;
}
printf("#### %s", szDe);
return 0;
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!