Public-Key

如何在 Python 中將壓縮的公鑰轉換為未壓縮的公鑰?

  • August 30, 2022

我已經非常努力地搜尋了一個將壓縮的公鑰轉換為未壓縮的公鑰的 Python 腳本,因為我不知道如何編碼。我偶然發現了這些連結:

https://stackoverflow.com/questions/43629265/deriving-an-ecdsa-uncompressed-public-key-from-a-compressed-one

如何解壓公鑰?

https://bitcointalk.org/index.php?topic=644919.0

但所有 python 腳本/函式要麼不起作用,要麼輸出不正確的值。我試圖調試它們但失敗了。您能否指出如何在 Python 中編寫類似這樣的程式碼或給出一個工作程式碼的範例?以下是要使用的值(HEX):

#Test case 1
#Compressed Pubkey: 025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC
'''
Right Uncompressed Pubkey:
045A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC9BD5DCD1B4A737244D6BB7B96E256391B8597D3A7972A6F8CA9096D4AEA1F37E
'''

#Test case 2
#Compressed Pubkey: 035728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED9
'''
Right Uncompressed Pubkey:
045728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED99E2DF8D9CB1A575D55264692629AE22E518BC14AD02592941C13BE6755C72973
'''

#Test case 3
#Compressed Pubkey:039E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15
'''
Right Uncompressed Pubkey:
049E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15A3B647C8C4A0766420917B7B445CDCD6BFEC2900175C5534C6113954F3FF00D9
'''

使用這個庫(免責聲明:我是作者):

>>> from cryptotools import PublicKey

>>> PublicKey.from_hex('025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC').hex(compressed=False)
'045a2146590b80d1f0d97cc7104e702011afff21bfaf817f5c7002446369ba9ddc9bd5dcd1b4a737244d6bb7b96e256391b8597d3a7972a6f8ca9096d4aea1f37e'

引用自:https://bitcoin.stackexchange.com/questions/115088