找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 11285|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

用openssl做rsa數(shù)字簽名、校驗(yàn),及n、e到PEM格式轉(zhuǎn)換

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:51024 發(fā)表于 2014-8-16 23:08 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式

0. 產(chǎn)生測(cè)試用的文件
echo "hello world" >hello.txt

1. 做一下sha256 hash
$ openssl dgst -sha256 hello.txt
SHA256(hello.txt)= a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
或者,來(lái)個(gè)二進(jìn)制輸出
$ openssl dgst -sha256 -binary -out hello.sha256 hello.txt
$ od -v -An -t x1 hello.sha256
a9 48 90 4f 2f 0f 47 9b 8f 81 97 69 4b 30 18 4b
0d 2e d1 c1 cd 2a 1e c0 fb 85 d2 99 a1 92 a4 47

2. 創(chuàng)建rsa密鑰
$ openssl genrsa -out key.pri -f4 2048
Generating RSA private key, 2048 bit long modulus
......................................................+++
..........................................+++
e is 65537 (0x10001)

3. 看一下密鑰內(nèi)容
$ openssl rsa -inform PEM -in key.pri -text
....

4. 導(dǎo)出公鑰
$ openssl rsa -inform PEM -outform PEM -in key.pri -out key.pub -pubout
writing RSA key

5. 查看公鑰
$ openssl rsa -inform PEM -in key.pub -pubin -text
....

6. hash并用私鑰簽名
$ openssl dgst -sha256 -out hello.sig -sign key.pri -keyform PEM hello.txt
$ od -v -An -t x1 hello.sig
....

7. 公鑰校驗(yàn)
$ openssl dgst -sha256 -keyform PEM -verify key.pub -signature hello.sig hello.txt
Verified OK
上面這個(gè)命令只能輸出"Verification OK"或者"Verification Failure"。如果想看到恢復(fù)的數(shù)據(jù),可以用下面的命令:
$ openssl rsautl -in hello.sig -inkey key.pub -pubin -verify -hexdump
0000 - 30 31 30 0d 06 09 60 86-48 01 65 03 04 02 01 05   010...`.H.e.....
0010 - 00 04 20 a9 48 90 4f 2f-0f 47 9b 8f 81 97 69 4b   .. .H.O/.G....iK
0020 - 30 18 4b 0d 2e d1 c1 cd-2a 1e c0 fb 85 d2 99 a1   0.K.....*.......
0030 - 92 a4 47                                          ..G

或者不去掉pkcs填充的字節(jié):
$ openssl rsautl -in hello.sig -inkey key.pub -pubin -verify -hexdump -raw
0000 - 00 01 ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0010 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0020 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0030 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0040 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0050 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0060 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0070 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0080 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
0090 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
00a0 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
00b0 - ff ff ff ff ff ff ff ff-ff ff ff ff ff ff ff ff   ................
00c0 - ff ff ff ff ff ff ff ff-ff ff ff ff 00 30 31 30   .............010
00d0 - 0d 06 09 60 86 48 01 65-03 04 02 01 05 00 04 20   ...`.H.e.......
00e0 - a9 48 90 4f 2f 0f 47 9b-8f 81 97 69 4b 30 18 4b   .H.O/.G....iK0.K
00f0 - 0d 2e d1 c1 cd 2a 1e c0-fb 85 d2 99 a1 92 a4 47   .....*.........G

或者來(lái)個(gè)二進(jìn)制的文件:
$ openssl rsautl -in hello.sig -out hello.rec -inkey key.pub -pubin -verify
$ od -v -An -t x1 hello.rec
30 31 30 0d 06 09 60 86 48 01 65 03 04 02 01 05
00 04 20 a9 48 90 4f 2f 0f 47 9b 8f 81 97 69 4b
30 18 4b 0d 2e d1 c1 cd 2a 1e c0 fb 85 d2 99 a1
92 a4 47
最后32個(gè)字節(jié)是hello.txt的sha256值,前面19個(gè)字節(jié)是按照pkcs規(guī)范添加的用來(lái)標(biāo)示hash算法的數(shù)據(jù)。

有時(shí)候你拿到的公鑰可能不是PEM格式的,而只是一個(gè)n和e,openssl的標(biāo)準(zhǔn)命令是不能直接用的,也沒(méi)有提供轉(zhuǎn)換命令(存疑,反正我是沒(méi)發(fā)現(xiàn)比自己寫個(gè)小程序更簡(jiǎn)單的辦法)。這時(shí)候就需要用openssl庫(kù)自己寫個(gè)小程序來(lái)做轉(zhuǎn)換了。好在這個(gè)程序?qū)懫饋?lái)不麻煩,用到的幾個(gè)函數(shù)在下面幾個(gè)頭文件中定義:
openssl/bn.h、openssl/rsa.h、openssl/pem.h


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表