openssl 签名和验证签名 举例

openssl

最近有不少人问我如何用openssl命令行来进行签名,验证签名,以及其它一些openssl的命令。

其实我对openssl也不太了解,倒是对rsa了解得比较多一些,像rsa背后的欧拉公式以及证明啊什么的,呵呵呵。

写一个小脚本,里面包含了加密/解密,签名/验证签名等一些例子,仅供参考。

#!/bin/bash

set -e
set -x

# Generate 1024 bits rsa private key and output to file "test.key".
# This private key was encrypted by AES256 with the password "hkp".
openssl genrsa -aes256 -passout pass:hkp   -out test.key 1024
# Generate public key from the private key file "test.key" and output it to public key file "test.pub"
# Since the private key has been encrypted, so we need to pass in the password to decrypt it.
openssl rsa -passin pass:hkp -in test.key -pubout -out test.pub
# Encrypt the plain message from "plain.txt" and output to cipher file "cipher.txt" with public key.
# NB., if encrypt with public key, the option "-pubin" should be provided,
# if encrypt with private key, the password should be provided if needed.
#
# As per the manual of openssl,
# -encrypt encrypt with public key
# -decrypt decrypt with private key
# So even we use private key file "test.key" to encrypt the message, openssl only use the public part of "test.key"
openssl rsautl -encrypt -pubin -inkey test.pub -in plain.txt -out cipher.txt
openssl rsautl -encrypt -passin pass:hkp -inkey test.key -in plain.txt -out cipher_by_private_key_file.txt

# Decrypt the cipher message from "cipher.txt" and output the decrypted message into "decrypt.txt" with private key.
# Since the private key has been encrypted, so we need to pass in the password to decrypt it.
openssl rsautl -decrypt -inkey test.key -passin pass:hkp -in cipher.txt -out decrypt.txt
openssl rsautl -decrypt -inkey test.key -passin pass:hkp -in cipher_by_private_key_file.txt -out decrypt_also_by_private_key_file.txt

# Sign the digest of message with sha256 as the digest algorithm and output the signature to "plain.sig"
openssl dgst -sign test.key -passin pass:hkp -sha256 -out plain.sig plain.txt

# Verify the signature with the public key from public key file "test.pub",
# Please be note that, the signature is of the digest of the message from file "plain.txt".
openssl dgst -verify test.pub -sha256 -signature plain.sig plain.txt
# Since the public key can be retrieved from private key file,
# so openssl also support verify signature via private key file.
openssl dgst -prverify test.key -passin pass:hkp  -sha256 -signature plain.sig plain.txt

怪盗基德

19970924_212612

在一个幽静的夜晚,罗格钻石公司上出现了一个白色的身影,那个身影手上的便是日本最大的红宝石,诅咒之石。那个白色的身影后有十几个武警追捕着他,那个白色的身影停了下来,大声说道:“我就是可以把不可能变为可能的怪盗基德,我非常谢谢你们的钻石,”突然间眼前一阵白烟,怪盗基德冲出了玻璃从三十九楼的跳了下去,展开了自己那犹如羽翼般的滑翔翼,在天空给我们画上了一个梦幻般的夜晚。

— 冯传可

Bang Bang

bangbang

也是大半夜睡不着。

正带着我的耳机准备眯一会儿,突然蹦出了bang bang。就像穿过柯南脑袋的闪电,立马就想起了张靓颖在《我是歌手》上的神演绎,更有评论们的神点评。

天雷滚滚,简直画面太美不敢想。

如果让我穿越到现场,要说怎么形容此情此景,就像你在看网易新闻,然后看网易网友的评论。区别是网易新闻小编虽然经常敷衍了事,但是易友们的评论有时还是蛮犀利的。

难道只有我,觉得张靓颖把这首歌唱出了洋洋的乡土气息?先放着歌手不说,也许是不符合人家的风格,她唱《画皮》之类的作品就非常好听。

牛的是那帮点评,依稀记得某某说“嗯,张靓颖完美的演绎了一个需要3个巨星一起才能驾驭的歌曲,我觉得吧啦吧啦吧啦吧啦”。当时我就怀疑是我电视坏掉了,现在想想,都是套路。

各自安好吧。

球状闪电

bl

Claire 问我,这本书就这么吸引你?

——它是一本科幻小说,却让我几乎相信这就是真的。

现在是周日,准确的说是周一的凌晨1点57,明天要上班,我刚看完这本书。

《三体》的作者刘慈欣写的,完整看过三体的第二部,就没看其它。原因是觉得观点虽然新颖,但很多地方情节拖沓,就像一个是一个网络作家在面对平台每周催稿时,应付了事的拼凑章节。缺乏作为一个作家的功底。

所以当我读到《球状闪电》的时候,才翻几页,就眼前一亮——这成熟了好多啊!于是去翻出版时间,一看2004年。

难道那只是第一版?那应该有后续的再版再印刷。

没有。

真是有点惊呆了。

也不管其它,下午8点左右回到家之前只看了10几页,到家之后一口气读到现在,心满意足。

具体内容就不剧透了,应该是我读到的最好的科幻小说之一!

谢谢作者。