https://blog.csdn.net/vr7jj/article/details/53588089
對上面播客中
openssl rsa in userkey.pem -pubout -out userpub.key
這一句其實有點小問題,執行會不成功,然後執行 openssl rsa -help發現如下
-help Display this summary
-inform format Input format, one of DER NET PEM
-outform format Output format, one of DER NET PEM PVK
-in val Input file
-out outfile Output file
-pubin Expect a public key in input file
-pubout Output a public key
-passout val Output file pass phrase source
-passin val Input file pass phrase source
-RSAPublicKey_in Input is an RSAPublicKey
-RSAPublicKey_out Output is an RSAPublicKey
-noout Don't print key out
-text Print the key in text
-modulus Print the RSA key modulus
-check Verify key consistency
-* Any supported cipher
-pvk-strong Enable 'Strong' PVK encoding level (default)
-pvk-weak Enable 'Weak' PVK encoding level
-pvk-none Don't enforce PVK encoding
-engine val Use engine, possibly a hardware device
很容易發現in掉了-
還有主函數中在驗籤的地方memset(szTmp1, 0, sizeof(szTmp1));錯誤,簽名的數據被清空了,那驗籤肯定不成功。下面將修改後的原播客貼一下:
祕鑰對是我是使用openssl命令生產的給出的參考如下。另外祕鑰文件格式有很多種,請注意。
生產私鑰
openssl genrsa -out userkey.pem 1024
1
從私鑰中導出公鑰
openssl rsa -in userkey.pem -pubout -out userpub.key//此處修改-in
1
簽名測試代碼
#include <string.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
RSA* getPrivateKey(char* in_szKeyPath)
{
FILE *fp = NULL;
char szKeyPath[1024];
RSA *priRsa = NULL, *pubRsa = NULL, *pOut = NULL;
memset(szKeyPath, 0 ,sizeof(szKeyPath));
if(256 < strlen(in_szKeyPath))
strncpy(szKeyPath, in_szKeyPath, 256);
else
strncpy(szKeyPath, in_szKeyPath, strlen(in_szKeyPath));
printf("密鑰文件路徑[%s]", szKeyPath);
/* 打開密鑰文件 */
if(NULL == (fp = fopen(szKeyPath, "rb")))
{
printf( "打開密鑰文件[%s]出錯", szKeyPath);
return NULL;
}
/* 獲取私密鑰 */
if(NULL == (priRsa = PEM_read_RSAPrivateKey(fp, &priRsa, NULL,NULL)))
{
printf( "讀出私鑰內容出錯\n");
fclose(fp);
return NULL;
}
fclose(fp);
printf("提取私鑰\n");
pOut = priRsa;
return pOut;
}
RSA* getPublicKey(char* in_szKeyPath)
{
FILE *fp = NULL;
char szKeyPath[1024];
RSA *priRsa = NULL, *pubRsa = NULL, *pOut = NULL;
memset(szKeyPath, 0 ,sizeof(szKeyPath));
if(256 < strlen(in_szKeyPath))
strncpy(szKeyPath, in_szKeyPath, 256);
else
strncpy(szKeyPath, in_szKeyPath, strlen(in_szKeyPath));
printf("密鑰文件路徑[%s]", szKeyPath);
/* 打開密鑰文件 */
if(NULL == (fp = fopen(szKeyPath, "rb")))
{
printf( "打開密鑰文件[%s]出錯", szKeyPath);
return NULL;
}
/* 獲取公密鑰 */
if(NULL == (priRsa = PEM_read_RSA_PUBKEY(fp, &priRsa, NULL,NULL)))
{
printf("讀出私鑰內容出錯\n");
fclose(fp);
return NULL;
}
fclose(fp);
printf("提取公鑰\n");
pOut = priRsa;
return pOut;
}
int main(void)
{
int flen,rsa_len, ienLen, iRet;
RSA *prsa = NULL;
char szEnData[]="orderId=01010500201502000004reqTime=20150205012727ext=20151120ext2=1";
char szTmp[10240], szTmp1[10240];
if(NULL == (prsa = getPrivateKey("userkey.pem")))
{
RSA_free(prsa);
printf("獲取私鑰失敗\n");
return -1;
}
// RSA_print_fp(stdout, prsa, 11);
flen = strlen(szEnData);
printf("待簽名數據:[%s]\n", szEnData);
memset(szTmp, 0, sizeof(szTmp));
memset(szTmp1, 0, sizeof(szTmp1));
// 對待簽名數據做SHA1摘要
SHA1(szEnData, flen, szTmp);
//使用私鑰對SHA1摘要做簽名
ienLen = RSA_sign(NID_sha1, (unsigned char *)szTmp, 20, (unsigned char*)szTmp1, &iRet, prsa);
if(ienLen != 1 )
{
printf("簽名失敗\n");
RSA_free(prsa);
return -1;
}
RSA_free(prsa);
printf("簽名成功\n");
//簽名串szTmp1二進制數據需要轉成base64編碼
//mac=base64encode(szTmp1)這是僞碼,生產MAC值,給對方去校驗
//驗證簽名
//驗證簽名的是需要獲取MAC值,明文簽名數據,對“明文簽名數據”做SHA1,獲得摘要。在對MAC做basedecode(mac),然後調用函數驗證簽名
if(NULL == (prsa = getPublicKey("userpub.key")))
{
RSA_free(prsa);
printf("獲取私鑰失敗\n");
return -1;
}
flen = strlen(szEnData);
printf("待簽名數據:[%s]\n", szEnData);//簽名數據 和 mac 因該是由通信報文中獲得,這裏演示直接用使用同一變量
memset(szTmp, 0, sizeof(szTmp));
//memset(szTmp1, 0, sizeof(szTmp1));//此處修改
// 對待簽名數據做SHA1摘要
SHA1(szEnData, flen, szTmp);
ienLen = RSA_verify(NID_sha1, (unsigned char *)szTmp, 20, (unsigned char*)szTmp1, iRet, prsa);
if(ienLen != 1 )
{
printf("簽名不合法\n");
RSA_free(prsa);
return -1;
}
else
printf("驗籤成功\n");
RSA_free(prsa);
return 0;
}
---------------------
作者:vr7jj
來源:CSDN
原文:https://blog.csdn.net/vr7jj/article/details/53588089
版權聲明:本文爲博主原創文章,轉載請附上博文鏈接!
然後其他正常,總而言之,原博主也還是很不錯的。