1a)
#!pip install pycrytodme
import binascii
import crypto
import crypto.PublicKey import RSA
from crypto import Random
from crypto.Hash import SHA
from crypto.Signature import PKCS1_v1_5
class Client:
def __init__(self):
Random = crypto.Random.new().read
self.private_key = RSA.generate(1024, Random)
self.public_Key =self._private_key.publicKey()
self.signer = PKCS1_v1_5.new(self._private_Key)
@property
def identity(self):
return
binascii.hexlify(self._private_key.exportKey(format='DER')).decode(ascii)
demo=Client()
print(demo.identity)
1b)
import collections
import datetime
import binascii
#!pip install pycrytodme
import crypto
import crypto.PublicKey import RSA
from crypto import Random
from crypto.Hash import SHA
from crypto.Signature import PKCS1_v1_5
class Client:
def __init__(self):
Random = crypto.Random.new().read
self.private_key = RSA.generate(1024, Random)
self.public_Key =self._private_key.publicKey()
self.signer = PKCS1_v1_5.new(self._private_Key)
@property
def identity(self):
return
binascii.hexlify(self._private_key.exportKey(format='DER')).decode(ascii)
class Transaction:
def __init__(self, sender, recipient, value):
self.sender = sender
self.recipient = recipient
self.value = value
self.time = datetime.datetime.now()
def to_dect(self):
if self.sender=="Genesis":
identity="genesis":
else:
identity=self.sender.identity
return collections.OrderedDict({
"sender": identity,
"recipient": self.recipient,
"value": self.value,
"time": self.time
})
def sign_transaction(self):
private_Key=self.sender._privateKey
signer=PKCS1_v1_5.new(Private_Key)
h=SHA.new(str(self.to.dict()).encode('utf8'))
return
binascii.hexlify(signer.sign(h)).decode('ascii')
raja=Client()
rani=Client()
t=Transaction(raja,rani.identity,5.0)
signature=t.sign_transaction()
print(signature)
1c)
import collections
import datetime
import binascii
#!pip install pycrytodme
import crypto
import crypto.PublicKey import RSA
from crypto import Random
from crypto.Hash import SHA
from crypto.Signature import PKCS1_v1_5
class Client:
def __init__(self):
Random = crypto.Random.new().read
self.private_key = RSA.generate(1024, Random)
self.public_Key =self._private_key.publicKey()
self.signer = PKCS1_v1_5.new(self._private_Key)
@property
def identity(self):
return
binascii.hexlify(self._private_key.exportKey(format='DER')).decode(ascii)
class Transaction:
def __init__(self, sender, recipient, value):
self.sender = sender
self.recipient = recipient
self.value = value
self.time = datetime.datetime.now()
def to_dect(self):
if self.sender=="Genesis":
identity="genesis":
else:
identity=self.sender.identity
return collections.OrderedDict({
"sender": identity,
"recipient": self.recipient,
"value": self.value,
"time": self.time
})
def sign_transaction(self):
private_Key=self.sender._privateKey
signer=PKCS1_v1_5.new(Private_Key)
h=SHA.new(str(self.to.dict()).encode('utf8'))
return binascii.hexlify(signer.sign(h)).decode('ascii')
import hashlib
def sha256(message):
return hashlib.sha256(message.encode('ascii')).hexdigest()
def mine(message, diffculty=1):
assert difficulty>=1
prefix='1'*difficulty
for i in range(1000)
digest=sha256(str(hash(message))+str(i))
if digest.starswitch(prefix):
print("after"+str(i)+"iterations found nonce:" + digest)
return digest
class Block:
def __init__(self):
self.verified_trasnaction=[]
self.previous_block_hash=""
self.None=""
last_block_hash=""
def display_transaction(transaction):
dict=transaction.to_dict()
print("sender:" + dict['sender'])
print('--------')
print("recipient:" + dict['recipient'])
print('--------')
print("value:" + dict['value'])
print('--------')
print("time:" + dict['time'])
print('--------')
TPCoins=[]
def dump_blockchain(self):
print("number of blocka in the chain:" +str(len(self)))
for x in range(len(TPCoinss)):
block_temp=TPCoins[x]
print("block#"+str(x))
for transaction in block_temp.verified_transactons:
display_transaction(Transaction)
print('-------------')
print('=============')
last_transaction_index=0
Transaction[]
Raja=Client()
Rani=Client()
Seema=Client()
Reema=client()
t1=Transaction(Raja,Rani.identity,15.0)
t1=sign_transaction()
Transaction.append(t1)
t2=Transaction(Raja,Seema.identity,6.0)
t2=sign_transaction()
Transaction.append(t2)
t3=Transaction(Rani, Reema.identity,2.0)
t3=sign_transaction()
Transaction.append(t3)
t4=Transaction(Seema,Rani.identity,4.0)
t4=sign_transaction()
Transaction.append(t4)
t5=Transaction(Reema,Seema.identity,3.0)
t5=sign_transaction()
Transaction.append(t5)
t6=Transaction(Rani,Seema.identity,3.0)
t6=sign_transaction()
Transaction.append(t6)
t7=Transaction(Seema,Raja.identity,8.0)
t7=sign_transaction()
Transaction.append(t7)
t8=Transaction(Seema,Rnai.identity,1.0)
t8=sign_transaction()
Transaction.append(t8)
t9=Transaction(Reema,Raja.identity,5.0)
t9=sign_transaction()
Transaction.append(t9)
t10=Transaction(Reema,Rani.identity,3.0)
t10=sign_transaction()
Transaction.append(t10)
for transaction in Transaction:
display_transaction(Transaction)
print('--------------')
1 d)
import collections
import datetime
import binascii
#!pip install pycrytodme
import crypto
import crypto.PublicKey import RSA
from crypto import Random
from crypto.Hash import SHA
from crypto.Signature import PKCS1_v1_5
class Client:
def __init__(self):
Random = crypto.Random.new().read
self.private_key = RSA.generate(1024, Random)
self.public_Key =self._private_key.publicKey()
self.signer = PKCS1_v1_5.new(self._private_Key)
@property
def identity(self):
return
binascii.hexlify(self._private_key.exportKey(format='DER')).decode(ascii)
class Transaction:
def __init__(self, sender, recipient, value):
self.sender = sender
self.recipient = recipient
self.value = value
self.time = datetime.datetime.now()
def to_dect(self):
if self.sender=="Genesis":
identity="genesis":
else:
identity=self.sender.identity
return collections.OrderedDict({
"sender": identity,
"recipient": self.recipient,
"value": self.value,
"time": self.time
})
def sign_transaction(self):
private_Key=self.sender._privateKey
signer=PKCS1_v1_5.new(Private_Key)
h=SHA.new(str(self.to.dict()).encode('utf8'))
return binascii.hexlify(signer.sign(h)).decode('ascii')
class Block:
def __init__(self):
self.verified_trasnaction=[]
self.previous_block_hash=""
self.None=""
last_block_hash=""
def display_transaction(transaction):
#for transaction in transactions:
dict=transaction.to_dict()
print("sender:" + dict['sender'])
print('--------')
print("recipient:" + dict['recipient'])
print('--------')
print("value:" + dict['value'])
print('--------')
print("time:" + dict['time'])
print('--------')
Rutuja=Client()
t0=Transaction(
"Genesis"
Rutuja.identity,
500.0
)
block=Block()
block0.previous_block_hash=None
Nonce=None
block0.verified_transaction.append(t0)
digest=hash(block0)
last_block_hash=digest
TPCoins=[]
def dump_blockchain(self):
print("number of blocka in the chain:" +str(len(self)))
for x in range(len(TPCoinss)):
block_temp=TPCoins[x]
print("block#"+str(x))
def dump_blockchain(self):
print("number of blocka in the chain:" +str(len(self)))
for x in range(len(TPCoinss)):
block_temp=TPCoins[x]
print("block#"+str(x))
for transaction in block_temp.verified_transactons:
display_transaction(Transaction)
print('-------------')
print('=============')
TPCoins.append(block0)
dump_blockchain(TPCoins)
1 e)
import hashlib
def sha256(message):
return hashlib.sha256(message.encode('ascii')).hexdigest()
def mine(message, diffculty=1):
assert difficulty>=1
prefix='1'*difficulty
for i in range(1000)
digest=sha256(str(hash(message))+str(i))
if digest.starswitch(prefix):
print("after"+str(i)+"iterations found nonce:" + digest)
return digest
mine("testmessage",2)
2)
import collections
import datetime
import binascii
#!pip install pycrytodme
import crypto
import crypto.PublicKey import RSA
from crypto import Random
from crypto.Hash import SHA
from crypto.Signature import PKCS1_v1_5
class Client:
def __init__(self):
Random = crypto.Random.new().read
self.private_key = RSA.generate(1024, Random)
self.public_Key =self._private_key.publicKey()
self.signer = PKCS1_v1_5.new(self._private_Key)
@property
def identity(self):
return
binascii.hexlify(self._private_key.exportKey(format='DER')).decode(ascii)
class Transaction:
def __init__(self, sender, recipient, value):
self.sender = sender
self.recipient = recipient
self.value = value
self.time = datetime.datetime.now()
def to_dect(self):
if self.sender=="Genesis":
identity="genesis":
else:
identity=self.sender.identity
return collections.OrderedDict({
"sender": identity,
"recipient": self.recipient,
"value": self.value,
"time": self.time
})
def sign_transaction(self):
private_Key=self.sender._privateKey
signer=PKCS1_v1_5.new(Private_Key)
h=SHA.new(str(self.to.dict()).encode('utf8'))
return binascii.hexlify(signer.sign(h)).decode('ascii')
import hashlib
def sha256(message):
return hashlib.sha256(message.encode('ascii')).hexdigest()
def mine(message, diffculty=1):
assert difficulty>=1
prefix='1'*difficulty
for i in range(1000)
digest=sha256(str(hash(message))+str(i))
if digest.starswitch(prefix):
print("after"+str(i)+"iterations found nonce:" + digest)
return digest
class Block:
def __init__(self):
self.verified_trasnaction=[]
self.previous_block_hash=""
self.None=""
last_block_hash=""
def display_transaction(transaction):
#for transaction in transactions:
dict=transaction.to_dict()
print("sender:" + dict['sender'])
print('--------')
print("recipient:" + dict['recipient'])
print('--------')
print("value:" + dict['value'])
print('--------')
print("time:" + dict['time'])
print('--------')
TPCoins=[]
def dump_blockchain(self):
print("number of blocka in the chain:" +str(len(self)))
for x in range(len(TPCoinss)):
block_temp=TPCoins[x]
print("block#"+str(x))
for transaction in block_temp.verified_transactons:
display_transaction(Transaction)
print('-------------')
print('=============')
last_transaction_index=0
Transaction[]
Raja=Client()
Rani=Client()
Seema=Client()
Reema=client()
t1=Transaction(Raja,Rani.identity,15.0)
t1=sign_transaction()
Transaction.append(t1)
t2=Transaction(Raja,Seema.identity,6.0)
t2=sign_transaction()
Transaction.append(t2)
t3=Transaction(Rani, Reema.identity,2.0)
t3=sign_transaction()
Transaction.append(t3)
t4=Transaction(Seema,Rani.identity,4.0)
t4=sign_transaction()
Transaction.append(t4)
t5=Transaction(Reema,Seema.identity,3.0)
t5=sign_transaction()
Transaction.append(t5)
t6=Transaction(Rani,Seema.identity,3.0)
t6=sign_transaction()
Transaction.append(t6)
t7=Transaction(Seema,Raja.identity,8.0)
t7=sign_transaction()
Transaction.append(t7)
t8=Transaction(Seema,Rnai.identity,1.0)
t8=sign_transaction()
Transaction.append(t8)
t9=Transaction(Reema,Raja.identity,5.0)
t9=sign_transaction()
Transaction.append(t9)
t10=Transaction(Reema,Rani.identity,3.0)
t10=sign_transaction()
Transaction.append(t10)
#Minerladdsablock
block=Block()
for i in range(3):
temp_transactions[last_transaction_index]
#validatetransaction
#if valid
block.varified_transaction,append(temp_transaction)
last_transaction_index+=1
block.previous_block_hash=last_block_hash
block.Nonce=mine(block,2)
digest=hash(block)
TPCoins.append(block)
last_block_hash=digest
#miner2 adds a block
block=Block()
for i in range(3):
temp_transactions[last_transaction_index]
#validatetransaction
#if valid
block.varified_transaction,append(temp_transaction)
last_transaction_index+=1
block.previous_block_hash=last_block_hash
block.none=mine(block,2)
digest=hash(block)
TPCoins.append(block)
last_block_hash=digest
dump_blockchain(TPCoins)