SHA1WithRSA签名使用openssl 实现
引言:
2017年就要到了,想想自己使用阿里云搭的博客 眼看就要到期了。 雖說(shuō)沒(méi)寫幾遍有質(zhì)量的文章吧,但是放其不管也于心不忍。這幾天就琢磨著把幾遍有內(nèi)容的轉(zhuǎn)到博客中。在尋求著落點(diǎn)的時(shí)候發(fā)現(xiàn)Markdown。看了CSDN中Markdown的范文,再看看自己曾經(jīng)寫的文章。只能呵呵了
想著就借此機(jī)會(huì)學(xué)習(xí)一下Markdown的話語(yǔ)。
最近一個(gè)項(xiàng)目對(duì)接。要使用SHA1WithRSA簽名驗(yàn)簽。
之前接觸過(guò)DES、3DES、 AES、 SHA1、 MD5、RSA 看這加密想當(dāng)然的覺得是就是先對(duì)數(shù)據(jù)做個(gè)SHA1摘要再做個(gè)RSA加密嘛,簡(jiǎn)單不是。
man 了一下 openssl 關(guān)于RSA加解密。霹靂啪啦幾個(gè)小時(shí)就把 理解的“SHA1WithRSA”實(shí)現(xiàn)了。
但是測(cè)試時(shí)對(duì)方就是驗(yàn)簽不過(guò)。
后面還是問(wèn)廣大的網(wǎng)絡(luò)。才讓我得了解自己的無(wú)知。
這不也有人和犯一樣的錯(cuò) 哈哈
簡(jiǎn)單閱讀一下就能大概知道實(shí)際上可以通過(guò)openssl的RSA_sign實(shí)現(xiàn)
來(lái)看一下RSA_sign
DESCRIPTION
RSA_sign() signs the message digest m of size m_len using the private key rsa as specified in PKCS #1 v2.0. It stores the signature in sigret and the signature size in siglen. sigret must point to RSA_size(rsa) bytes of memory. Note that PKCS #1 adds meta-data, placing limits on the size of the key that can be used. See RSA_private_encrypt for lower-level operations.
type denotes the message digest algorithm that was used to generate m. If type is NID_md5_sha1, an SSL signature (MD5 and SHA1 message digests with PKCS #1 padding and no algorithm identifier) is created.
RSA_verify() verifies that the signature sigbuf of size siglen matches a given message digest m of size m_len. type denotes the message digest algorithm that was used to generate the signature. rsais the signer’s public key.
RETURN VALUES
RSA_sign() returns 1 on success. RSA_verify() returns 1 on successful verification.
簡(jiǎn)單看看,其實(shí)RSA_sing()函數(shù)第一為送NID_sha1
通過(guò)這篇文章知道需要簽名的是SHA1摘要而非所有報(bào)文。所以要先對(duì)明文數(shù)據(jù)做SHA1。再調(diào)用 RSA_sign()簽名。
結(jié)論:
對(duì)openSSL源碼一知半解的我,竟把RSA加密|解密和RSA簽名|驗(yàn)證混淆。
最后 私鑰加密 ≠ 簽名
20170519更新
這幾天看了下這文章閱讀人數(shù)相比還挺多的。空閑之余就把整理下自己以前寫的test程序提供給大家參考下。
秘鑰對(duì)是我是使用openssl命令生產(chǎn)的給出的參考如下。另外秘鑰文件格式有很多種,請(qǐng)注意。
生產(chǎn)私鑰
從私鑰中導(dǎo)出公鑰
openssl rsa in userkey.pem -pubout -out userpub.key簽名測(cè)試代碼
#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);elsestrncpy(szKeyPath, in_szKeyPath, strlen(in_szKeyPath));printf("密鑰文件路徑[%s]", szKeyPath);/* 打開密鑰文件 */if(NULL == (fp = fopen(szKeyPath, "rb"))){printf( "打開密鑰文件[%s]出錯(cuò)", szKeyPath);return NULL;}/* 獲取私密鑰 */if(NULL == (priRsa = PEM_read_RSAPrivateKey(fp, &priRsa, NULL,NULL))){printf( "讀出私鑰內(nèi)容出錯(cuò)\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);elsestrncpy(szKeyPath, in_szKeyPath, strlen(in_szKeyPath));printf("密鑰文件路徑[%s]", szKeyPath);/* 打開密鑰文件 */if(NULL == (fp = fopen(szKeyPath, "rb"))){printf( "打開密鑰文件[%s]出錯(cuò)", szKeyPath);return NULL;}/* 獲取公密鑰 */if(NULL == (priRsa = PEM_read_RSA_PUBKEY(fp, &priRsa, NULL,NULL))){printf("讀出私鑰內(nèi)容出錯(cuò)\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("待簽名數(shù)據(jù):[%s]\n", szEnData);memset(szTmp, 0, sizeof(szTmp));memset(szTmp1, 0, sizeof(szTmp1));// 對(duì)待簽名數(shù)據(jù)做SHA1摘要SHA1(szEnData, flen, szTmp);//使用私鑰對(duì)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二進(jìn)制數(shù)據(jù)需要轉(zhuǎn)成base64編碼//mac=base64encode(szTmp1)這是偽碼,生產(chǎn)MAC值,給對(duì)方去校驗(yàn)//驗(yàn)證簽名//驗(yàn)證簽名的是需要獲取MAC值,明文簽名數(shù)據(jù),對(duì)“明文簽名數(shù)據(jù)”做SHA1,獲得摘要。在對(duì)MAC做basedecode(mac),然后調(diào)用函數(shù)驗(yàn)證簽名if(NULL == (prsa = getPublicKey("userpub.key"))){RSA_free(prsa);printf("獲取私鑰失敗\n");return -1;}flen = strlen(szEnData);printf("待簽名數(shù)據(jù):[%s]\n", szEnData);//簽名數(shù)據(jù) 和 mac 因該是由通信報(bào)文中獲得,這里演示直接用使用同一變量memset(szTmp, 0, sizeof(szTmp));memset(szTmp1, 0, sizeof(szTmp1));// 對(duì)待簽名數(shù)據(jù)做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;}elseprintf("驗(yàn)簽成功\n");RSA_free(prsa);return 0; }源碼和秘鑰文件資源
總結(jié)
以上是生活随笔為你收集整理的SHA1WithRSA签名使用openssl 实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 蚕豆
- 下一篇: 语音识别(ASR)论文优选:A comp