2017-09-13 228 views
0

下一個問題: 消息在Python應用程序後,將在二郎進行驗證與公共密鑰ECDSA簽名驗證

Python可以通過兩種方式登錄消息:

1)簽名的muttable長度 - 二郎山驗證OK

2)簽字:日期的imuttable長度 - 二郎着驗證

蟒蛇lib中的一些信息: 也有多種方式represe nt簽名。爲了簡單和最小的開銷,默認的sk.sign()和vk.verify()方法將其呈現爲短字符串。要使用不同的方案,請使用sk.sign(sigencode =)和vk.verify(sigdecode =)參數。在「ecdsa.util」模塊中有助手功能,在這裏可以使用。

例子:

DataToSign = 
<<96,0,0,0,0,0,253,0,254,128,131,0,33,1,255,255,48,142,92,131,100,75,214,244,255,60,0,0,0,0,0,0,0,0,0,0,0,0,0,25,0,0,16,92,17,66,0,1,0,0,0,0,0,0,3,9,89,185,3,24,89,185,5,112,0,0,123,3,0,3,253,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>. 

PublicKey = {{'ECPoint',<<4,71,71,171,183,56,205,109,58,24,36,197,220,179,74,166,21,145, 
       79,209,245,157,150,237,93,96,31,1,248,198,232,97,28,197,90,243, 
       89,210,74,169,213,236,46,124,133,31,44,215,222,184,221,251,95, 
       40,182,101,193,50,35,62,174,134,178,142,120>>}, 
{namedCurve,{1,2,840,10045,3,1,7}}}. 



Signature from python (with constant length): 

ImmutableSign = 
<<179,160,225,5,1,154,246,29,57,97,160,132,95,35,135,238,69,21,136,152,51,104,224,126,124,226,168,143,253,213,37,233,196,67,254,155,60,218,45,8,60,140,76,44,20,189,213,143,42,231,4,177,62,124,141,95,236,20,59,20,28,10,58,232>>. 

signed with sk.sign(data, hashfunc=hashlib.sha256) 

MutableSign = 



<<48,68,2,32,6,82,67,217,251,248,93,74,223,170,47,192,93,228,218,228,132,4,69,177,57,49,20,161,241,48,52,106,39,130,197,2,2,32,88,35,228,122,252,140,214, 
191,187,117,10,187,54,106,16,36,198,155,49,78,198,58,103,57,124,212,244,31,9,242,101,169>> 

signed with sk.sign(data, hashfunc=hashlib.sha256, sigencode=ecdsa.util.sigencode_der) 


public_key:verify(DataTosign, sha256, ImmutableSign, PublicKey) -> false 

public_key:verify(DataTosign, sha256, MutableSign, PublicKey) -> true 

如果通過OpenSSL的核實也很有趣的事情

可變的長度跡象:

openssl dgst -ecdsa-with-SHA1 -verify ec-pub.pem -signature sig2.bin data 
Verification Failure 

失敗(因爲SHA256嘆息)

不可變長代碼:

openssl dgst -ecdsa-with-SHA1 -verify ec-pub.pem -signature sig.bin data 
Error Verifying Data 
33773:error:0D07207B:asn1 encoding routines:ASN1_get_object:header too long:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64.50.6/src/crypto/asn1/asn1_lib.c:153: 
33773:error:0D068066:asn1 encoding routines:ASN1_CHECK_TLEN:bad object header:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64.50.6/src/crypto/asn1/tasn_dec.c:1331: 
33773:error:0D07803A:asn1 encoding routines:ASN1_ITEM_EX_D2I:nested asn1 error:/BuildRoot/Library/Caches/com.apple.xbs/Sources/OpenSSL098/OpenSSL098-64.50.6/src/crypto/asn1/tasn_dec.c:387:Type=ECDSA_SIG 
+1

我複製粘貼你的代碼'erl',我得到了兩個''PUBLIC_KEY FALSE':verify'電話。你能否檢查你是否粘貼了正確的數據? (我必須修復一個錯字:'DataTosign' - >'DataToSign'。) – Dogbert

+0

@Dogbert哦,是的。複製粘貼 –

+1

時出現錯誤我沒有看到這裏的問題是什麼。 – bartonjs

回答

1

所以我們需要格式化短標誌DER從而驗證密碼

fix_signed(<<First:8, _/bytes>> = Value) when First >= 16#80 -> <<0:8, Value/bytes>>; 
fix_signed(Value) -> Value. 

get_signed_big_endian(Value) -> 
    Res = binary:encode_unsigned(Value), 
    fix_signed(Res). 

sign_to_der_format(<<R:256, S:256>>) -> 

    RBin = get_signed_big_endian(R), 
    SBin = get_signed_big_endian(S), 

    RSize = byte_size(RBin), 
    SSize = byte_size(SBin), 

    Len = 4 + RSize + SSize, 

    <<48:8, Len:8, 2:8, RSize:8, RBin/bytes, 2:8, SSize:8, SBin/bytes>>.