我想使用Python根据OAuth 1 RFC Section http://tools.ietf.org/html/rfc5849#section-3.4.3中的RSA-SHA1签名方法签署一条消息。我试图通过比较openssl的结果来验证我是在正确的轨道上。
现在我突然发现自己有一个额外的八位字节,因为我对密码学的了解最多,我需要一些帮助。通过查看加密源和openssl的手册页以及输出非常相似的事实,我认为我至少使用了正确的算法。
然而,当使用openssl rsautl时,我甚至不能接近......
$ openssl genrsa -out private.pem 1024
$ cat message
Lorem ipsum
$ cat sign.py
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
key = RSA.importKey(open("private.pem").read())
message = open("message").read()[:-1] # skip last newline
h = SHA.new(message)
p = PKCS1_v1_5.new(key)
signature = p.sign(h)
signature_trim = p.sign(h)[:-1] # will give same output as openssl dgst -sign
print signature # remove print when not using hexdump
Python的输出
$ python sign.py | hexdump
0000000 1346 38af 89a8 d203 ee26 0cfa a4bc 3a6c
0000010 44fd a436 2c50 03ba 7c84 333a 910a 843e
0000020 f71b 5731 1d2a 8895 9f5c 86b1 1838 7de9
0000030 5c13 d7e5 a019 6ad1 e5a5 d4d5 bd6f 0032
0000040 f320 c5ad fc41 da2c a9c3 2d9a cdce f6d6
0000050 4ef4 6dbd 1ba2 edc1 648e 184a 2e6c e746
0000060 fd92 ba61 b4da f607 d7a4 fbef 8230 378d
0000070 a143 b444 c711 7121 6e08 9d88 bb05 0d25
0000080 000a
0000081
使用openssl dgst签名输出(不确定这是否真的是rsa pkcs#1 v1.5)
$ echo -n $(cat message) | openssl dgst -sign private.pem | hexdump
0000000 1346 38af 89a8 d203 ee26 0cfa a4bc 3a6c
0000010 44fd a436 2c50 03ba 7c84 333a 910a 843e
0000020 f71b 5731 1d2a 8895 9f5c 86b1 1838 7de9
0000030 5c13 d7e5 a019 6ad1 e5a5 d4d5 bd6f 0032
0000040 f320 c5ad fc41 da2c a9c3 2d9a cdce f6d6
0000050 4ef4 6dbd 1ba2 edc1 648e 184a 2e6c e746
0000060 fd92 ba61 b4da f607 d7a4 fbef 8230 378d
0000070 a143 b444 c711 7121 6e08 9d88 bb05 0d25
0000080
在sign.py中,我删除签名打印并根据其公钥验证签名。由于修剪签名将无法通过验证,因此我认为不应该这样做,但我以前做错了。
pubkey = key.publickey()
pp = PKCS1_v1_5.new(pubkey)
print pp.verify(h, signature) # True
print pp.verify(h, signature_trim) # False
openssl rsautl的输出(不确定这是否是pkcs#1 v1.5)
$ echo -n $(cat message) | openssl dgst -sha1 | openssl rsautl -sign -inkey private.pem | hexdump
0000000 14a4 f02c 527f 26f9 29f6 281c 3185 4a1a
0000010 def8 052b b620 cca2 38d9 a389 0b44 112a
0000020 283c ebff 4228 6f77 7a65 9d53 4b98 a073
0000030 bbd9 1aca 3447 a917 d7c3 0968 63c4 6806
0000040 6112 6f36 2d38 a770 5afa a8e0 adf3 4bef
0000050 120c cc10 5194 75ad bdda 91e6 fd79 8f4c
0000060 b864 efb8 cc88 a4da e977 b488 6241 15fb
0000070 e105 1d11 8627 75bd 345b 34da 538f a8db
0000080
我显然做错了什么,现在我想知道多少钱。除了base64编码和“Lorem ipsum”不是有效的签名基本字符串的面...
...我需要修改哪些内容才能使其成为有效的RSA-SHA1签名?
答案 0 :(得分:4)
Python的print
将始终附加换行符,这是您看到的额外字节。
据我所知,没有简洁的方法可以避免这种情况,print signature,
也无法正常工作。
另一种方法是使用较低级别sys.stdout.write(signature)
。
答案 1 :(得分:0)
找到答案。什么都没有。
使用
创建证书+密钥$ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -sha1 -subj \
'/C=US/ST=CA/L=Mountain View/CN=www.example.com' -keyout myrsakey.pem \
-out myrsacert.pem
RSA-SHA1签名方法
def sign_rsa(method, url, params, private_rsa):
"""Sign a request using RSASSA-PKCS #1 v1.5.
Per `section 3.4.3`_ of the spec.
.. _`section 3.4.3`: http://tools.ietf.org/html/rfc5849#section-3.4.3
"""
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
key = RSA.importKey(private_rsa)
message = prepare_base_string(method, url, params)
h = SHA.new(message)
p = PKCS1_v1_5.new(key)
return escape(binascii.b2a_base64(p.sign(h))[:-1])
RSA-SHA1验证
def verify_rsa(method, url, params, public_rsa, signature):
"""Verify a RSASSA-PKCS #1 v1.5 base64 encoded signature.
Per `section 3.4.3`_ of the spec.
.. _`section 3.4.3`: http://tools.ietf.org/html/rfc5849#section-3.4.3
"""
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA
key = RSA.importKey(public_rsa)
message = prepare_base_string(method, url, params)
h = SHA.new(message)
p = PKCS1_v1_5.new(key)
signature = binascii.a2b_base64(urllib.unquote(signature))
return p.verify(h, signature)
好奇的人可以找到它们以及prepare_base_string并在https://github.com/ib-lundgren/oauthlib/blob/master/oauthlib/oauth.py转义
仍然不知道hexdumps,但如果我搞清楚会更新。