def _(): cv = Curve.get_curve('secp256k1'); pu_key = ECPublicKey( Point(0x65d5b8bf9ab1801c9f168d4815994ad35f1dcb6ae6c7a1a303966b677b813b00, 0xe6b865e529b8ecbf71cf966e900477d49ced5846d7662dd2dd11ccd55c0aff7f, cv)) pv_key = ECPrivateKey( 0xfb26a4e75eec75544c0f44e937dcf5ee6355c7176600b9688c667e5c283b43c5, cv) ; signer = ECDSA(fmt="ITUPLE") sig = signer.sign(b'01234567890123456789012345678912', pv_key) ;return sig
def getSignData(original_data, pk): json_str = json.dumps(original_data).replace(' ', '') byte_str = json_str.encode() sha256 = SHA256Hash(byte_str) byte_str_hash = sha256.digest() # 返回byte类型哈希 # 私钥签名 signer = ECDSA() raw_sig = signer.sign(byte_str_hash, pk) # sign返回的byte对象 hex_sig = ByteToHex(raw_sig).lower() # 将签名后byte转为hex,并修改字母为小写 return hex_sig
def gen_random_tx(curve): n = curve.order P = curve.generator sA = random.randint(0, n) sk = ECPrivateKey(sA, curve) QA = sA * P pk = ECPublicKey(QA) payee_sA = random.randint(0, n) payee_sk = ECPrivateKey(payee_sA, curve) payee_QA = sA * P payee_pk = ECPublicKey(payee_QA) sum_string = "*** Bitcoin transaction ***\n" serial = random.getrandbits(128) sum_string += "Serial number: " + str(serial) + "\n" sum_string += "Payer Public key - x: " + str(QA.x) + "\n" sum_string += "Payer Public key - y: " + str(QA.y) + "\n" sum_string += "Payee Public key - x: " + str(payee_QA.x) + "\n" sum_string += "Payee Public key - y: " + str(payee_QA.y) + "\n" amount = random.randint(1, 1000000) sum_string += "Amount: " + str(amount) + " Satoshi" + "\n" signer = ECDSA() sig = signer.sign(sum_string.encode('UTF-8'), sk) (r, s) = decode_sig(sig) # k = random.randint(1, n - 1) # R = k * P # r = R.x % n # #r = str(r).encode('UTF-8') # h = hashlib.sha3_256() # h.update(sum_string.encode('UTF-8')) # # h.update(str(r).encode('UTF-8')) # #h.update(r) # m + r # s = (modinv(k, n) * ((int(h.hexdigest(), 16)) + (sA * r))) % n # #h = int(h.hexdigest(), 16) sum_string += "Signature - r: " + str(r) + "\n" sum_string += "Signature - s: " + str(s) + "\n" return sum_string
def generate_signature(self, private_key): """ :return: {Message}SK, where Message = contents of TxIn and TxOut and Transaction ID """ txin = self.tx_Ins[-1] txout = self.tx_Outs[-1] tx_in_str = txin.get_tx_in_content() tx_out_str = txout.get_tx_out_content() tran_id = self.get_transaction_id() message = tx_in_str + tx_out_str + tran_id signer = ECDSA() sig = signer.sign(message.encode('utf-8'), private_key) txin.signature = sig return sig
def gen_random_tx(curve): # get a random 128 BIT integer for serial number serial_num = Num.getRandomNBitInteger(128) # create the public key for sender n = curve.order P = curve.generator sA = Num.getRandomRange(0, n + 1) sK = ECPrivateKey(sA, curve) QA = sA * P pk = ECPublicKey(QA) signer = ECDSA() # create the public key for sendee sA_2 = Num.getRandomRange(0, n + 1) sK_2 = ECPrivateKey(sA_2, curve) P2 = curve.generator QA_2 = sA_2 * P2 pk_2 = ECPublicKey(QA_2) # header for the block temp = "*** Bitcoin transaction ***\n" # add the serial number to the block temp = temp + "Serial number: " + str(serial_num) + "\n" # write payers public keys temp = temp + "Payer public key - x: " + str(QA.x) + "\n" temp = temp + "Payer public key - y: " + str(QA.y) + "\n" # write payees public keys temp = temp + "Payee public key - x: " + str(QA_2.x) + "\n" temp = temp + "Payee public key - y: " + str(QA_2.y) + "\n" # get random transaction val amount = Num.getRandomRange(0, 1000001) temp = temp + "Amount: " + str(amount) + "\n" sig = signer.sign(temp.encode("utf-8"), sK) (r, s) = decode_sig(sig) temp = temp + "Signature (r): " + str(r) + "\n" temp = temp + "Signature (s): " + str(s) + "\n" return temp
def gen_random_tx(curve): serial = random.randrange(pow(2, 127), pow(2, 128)) amount = random.randrange(1, 1000001) n = curve.order P = curve.generator sA = random.randint(0, n) sB = random.randint(0, n) skA = ECPrivateKey(sA, curve) skB = ECPrivateKey(sB, curve) QA = sA * P QB = sB * P pkA = ECPublicKey(QA) pkB = ECPublicKey(QB) signer = ECDSA() trans = "**** Bitcoin transaction ****" + \ "\nSerial number: " + str(serial) + \ "\nPayer public key - x: " + str(QA.x) + \ "\nPayer public key - y: " + str(QA.y) + \ "\nPayee public key - x: " + str(QB.x) + \ "\nPayee public key - y: " + str(QB.y) + \ "\nAmount: " + str(amount) + "\n" t = trans.encode("UTF-8") sig = signer.sign(t, skA) (r, s) = decode_sig(sig) trans += "Signature (r): " + str(r) + "\n" + "Signature (s): " + str( s) + "\n" return trans
def gen_random_tx(curve): serial = random.randint(0, 2**128 - 1) # creates 128 bit random serial number n = curve.order P = curve.generator sA = random.randint(0, n) sk = ECPrivateKey(sA, curve) QA = sA * P pk = ECPublicKey(QA) sB = random.randint(0, n) skB = ECPrivateKey(sB, curve) QB = sB * P pkB = ECPublicKey(QB) amount = random.randint(1, 1000000) # create a random int for amount transaction = "**** Bitcoin transaction ****\n" transaction += "Serial number: " + str(serial) + "\n" transaction += "Payer public key - x: " + str(QA.x) + "\n" transaction += "Payer public key - y: " + str(QA.y) + "\n" transaction += "Payee public key - x: " + str(QB.x) + "\n" transaction += "Payee public key - y: " + str(QB.y) + "\n" transaction += "Amount: " + str(amount) + "\n" signer = ECDSA() message = transaction message = message.encode('UTF-8') sig = signer.sign(message, sk) (r, s) = decode_sig(sig) transaction += "Signature (r): " + str(r) + "\n" transaction += "Signature (s): " + str(s) + "\n" return transaction
### ECS # test key cv = Curve.get_curve('secp256k1') pv_key = ECPrivateKey(0xf028458b39af92fea938486ecc49562d0e7731b53d9b25e2701183e4f2adc991,cv) pu_key = ECPublicKey(Point(0x81bc1f9486564d3d57a305e8f9067df2a7e1f007d4af4fed085aca139c6b9c7a, 0x8e3f35e4d7fb27a56a3f35d34c8c2b27cd1d266d5294df131bf3c1cbc39f5a91, cv)) k = pv_key.get_public_key() assert(k.W.x == pu_key.W.x) assert(k.W.y == pu_key.W.y) print("Public key ok") msg = 0x8c7632afe967e2e16ae7f39dc32c252b3d751fa6e01daa0efc3c174e230f4617 msg = msg.to_bytes(32,'big') sig = 0x304402203a329589dbc6f3bb88bf90b45b5d4935a18e13e2cb8fcee0b94b3102ec19645702202f61af55df0e56e71d40a9f5f111faeb2f831c1fd314c55227ac44110fb33049 sig = sig.to_bytes(70,'big') ## verify signer = ECDSA() while True: sig = signer.sign(msg,pv_key) signer.verify(msg,sig,pu_key) assert(signer.verify(msg,sig,pu_key))
if sys.version_info < (3, 6): import sha3 # You can keep this part (i.e., curve setting and key generation) curve = Curve.get_curve('secp256k1') n = curve.order P = curve.generator sA = random.randint(0, n) sk = ECPrivateKey(sA, curve) QA = sA * P pk = ECPublicKey(QA) # You need to change sign and verify methods below signer = ECDSA() # this line can be removed message = b'Anything goes here' sig = signer.sign(message, sk) # new sign method here verifier = ECDSA() # this line can be removed message = b'Anything goes here' try: assert (verifier.verify(message, sig, pk)) # new sign method here print("Signature verifies") except: print("Signature does not verify") message = b'Anything goes heree' try: assert (verifier.verify(message, sig, pk)) # new sign method here print("Signature verifies") except:
if sys.version_info < (3, 6): import sha3 curve = Curve.get_curve('secp256k1') n = curve.order P = curve.generator sA = random.randint(0,n) sk = ECPrivateKey(sA, curve) QA = sA*P pk = ECPublicKey(QA) signer = ECDSA() message = b'Anything goes here' sig = signer.sign(message, sk) (r, s) = decode_sig(sig) f = open("deneme.txt", "w") f.write("Public key - x: " + str(QA.x)+"\n") f.write("Public key - y: " + str(QA.y)+"\n") f.write("Signature - r: " + str(r)+"\n") f.write("Signature - s: " + str(s)+"\n") f.close() f = open("deneme.txt", "r") x1 = int(f.readline()[16:-1]) y1 = int(f.readline()[16:-1]) r1 = int(f.readline()[15:-1]) s1 = int(f.readline()[15:-1])
ad += chr(ord(b'a') + random.randint(0,25)).encode() send = {} send['ad'] = ad ecdhkey = d2['pubkey'] * d1['privkey'] bs = AES.block_size iv = random.randint(0, (1 << (8*bs))-1) fmt = '%%0%dx' % (bs * 2) ivhex = fmt % iv ivbin = binascii.unhexlify(ivhex) counter = Counter.new(AES.block_size * 8, initial_value=iv) key = hashlib.sha256(ecdhkey.compress()).digest() cryptor = AES.new(key, AES.MODE_CTR, counter = counter) ciphertext = ivbin + cryptor.encrypt(msg) b64cipher = base64.b64encode(ciphertext) send['b64cipher'] = b64cipher sig = ecdsa.sign(d1['privkey'], ciphertext, ad) send['sig'] = sig print(send) print('') print('') recv = send recdhkey = d1['pubkey'] * d2['privkey'] assert recdhkey == ecdhkey rciphertext = base64.b64decode(recv['b64cipher']) assert rciphertext == ciphertext rve = recdsa.verify(d1['pubkey'], recv['sig'], rciphertext, recv['ad']) assert rve == True rbs = AES.block_size assert rbs == bs rivbin = rciphertext[:rbs] assert rivbin == ivbin