今天來練習EEC的程式碼
安裝ecdsa庫
pip install ecdsa
程式碼
import ecdsa
from ecdsa import SigningKey, VerifyingKey
# 生成EEC
private_key = SigningKey.generate(curve=ecdsa.SECP256k1)
public_key = private_key.get_verifying_key()
# 要加密的消息
message = b"Hello~~!"
# 使用私鑰對訊行進行加密
signature = private_key.sign(message)
# 使用公鑰驗證簽名
try:
public_key.verify(signature, message)
print("簽名有效。訊息:", message.decode())
except ecdsa.BadSignatureError:
print("簽名無效。")
# 打印公鑰和私鑰的十六進制表示
print("私鑰 (hex):", private_key.to_string().hex())
print("公鑰 (hex):", public_key.to_string().hex())
結果
簽名有效。訊息: Hello~~!
私鑰 (hex): ef8d883caed359f35b6f02a1ab246d68ac3eeb6a065c7968a625b9ef43dcc1a4
公鑰 (hex): e23bd8642f6761bb2cba0352df252876fb28d18ad122de66276291cffb8dac256b5c6b432c46b810afb06e1cf31fef736b7066fdbaeabd9df1ccd45ea01b8c69