標(biāo)題:
用openssl做rsa數(shù)字簽名、校驗(yàn),及n、e到PEM格式轉(zhuǎn)換
[打印本頁]
作者:
wangyin
時間:
2014-8-16 23:08
標(biāo)題:
用openssl做rsa數(shù)字簽名、校驗(yàn),及n、e到PEM格式轉(zhuǎn)換
0. 產(chǎn)生測試用的文件
echo "hello world" >hello.txt
1. 做一下sha256 hash
$ openssl dgst -sha256 hello.txt
SHA256(hello.txt)= a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447
或者,來個二進(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
上面這個命令只能輸出"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
或者來個二進(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個字節(jié)是hello.txt的sha256值,前面19個字節(jié)是按照pkcs規(guī)范添加的用來標(biāo)示hash算法的數(shù)據(jù)。
有時候你拿到的公鑰可能不是PEM格式的,而只是一個n和e,openssl的標(biāo)準(zhǔn)命令是不能直接用的,也沒有提供轉(zhuǎn)換命令(存疑,反正我是沒發(fā)現(xiàn)比自己寫個小程序更簡單的辦法)。這時候就需要用openssl庫自己寫個小程序來做轉(zhuǎn)換了。好在這個程序?qū)懫饋聿宦闊,用到的幾個函數(shù)在下面幾個頭文件中定義:
openssl/bn.h、openssl/rsa.h、openssl/pem.h
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1