Blockchain Practical 1-4
Blockchain Practical 1-4
PRACTICAL NO 1
AIM: Understand basics of Block Chain and install various
practical 's software's required to perform Blockchain
INTRODUCTION TO BLOCK CHAIN:
What Is a Block chain?
Ablock chain is a distributed
database or ledger that is shared among the nodes of a
network. As a database, a block chain stores information computer
best known for their crucial role in electronically in digital format. Block chains are
decentralized record of transactions. cryptocurrency systems, such as Bitcoin, for maintaining a secure and
The innovation with a block chain is that it guarantees the
security of a record of data and generates trust vwithout the need fidelity and
fora trusted third party.
One key difference between a typical database and a block chain is how the data is
structured. A block chain
collects information together in groups, known as blocks, that hold sets of information.
storage capacities and, when filled, are closed and linked to the previously filled block, Blocks
forming
have certain
a chain of
data known as the block chain. All nevw information that follows that freshly added block is
compiled into a
newly fomed block that will then also be added to the chain once filled.
A database usually stuctures its data into tables, whereas a block chain, as its name implies, structures its
data into chunks (blocks) that are strung together. This data structure inherently makes an irreversible
timeline of data when implemented in a decentralized nature. When a block is filled, it is set in stone and
becomes
chain.
apart of this timeline. Each block in the chain is given an exact timestamp when it is added to the
Ubuntu and other versions of Debian Linux ship with Python 3 pre-installed. To make sure that our
versions are up-to-date, update your local package index:
sudo apt update
Then upgrade the packages installed on your system to ensure you have the latest versions:
sudo apt -y upgrade
The -y flag will confirm that we are agreeing for all items to be installed, but depending on your
version of Linux, youmay need to confirm additional prompts as your system
updates and upgrades.
Once the process is complete, we can check the version of Python 3 that is
typing: installed in the system by
python3 -V
You ll receive output in the terminal window that will
let you know the version number. While this
number may vary, the output will be similar to this:
Output
Python 3.8.10
To manage software packages Python, let's install pip, a tool
for
programming packages we that will install and manage
modules or packages that youmaycanwant to use in our
development projects.
install with pip by reading How To You can learn more abou
sudo apt install -y python3-pip Import Modules in Python 3.
Python packages can be installed by
typing:
pip3 installpackage name
Here, package name can
developmnent or NumPy for refer to any Python package or
so with the command scientific computing. So if you wouldlibrary, such as Django for we
pip3 install numpy. like to install
C
There are a few more NumPy, vou can a0
for our
programmi ng packages and development tools to
sudo apt install -y environment: install to ensure that we
have a robust seuP
build-essential libssl-dev libffi-dev
CSE/SEM-VII/BCF/PRO1 python3-dev
Once Python is set up, and pip and other tools are installed, we can set up a virtual environment for
Our development projects.
Step2: Setting Up Python 3
Virtual environments enable you to have an isolated space on your server for Python projects,
ensuring that each of your projects can have its own set of dependencies that won't disrupt any of
your other projects,.
While there are a few ways to achieve a programming environment in Python, we'll be using
the venv module here, which is part of the standard Python 3 library. Let's install venv by typing:
sudoaptinstall-y python3-veny
choose which directory we
With this installed, we are ready tO create environments. Let's either new directory with mkdir, as
wouldlike to put our Python programming environments in, cor create a
in:
mkdir environments
your programming environments:
then navigateto the directorywhere you'llstore
cd environments
would like the environments to live, you can create an
where you
Once you are in the directory command:
environment by running the following
python3-m venv my env with the ls
contains a few items which we can view
Essentially, pyvenv scts up a new directory that
command:
Is my env
CSE/SEM-VII/BCE/PRO1
nano hello.py
2) Etherum:
sno
a decentralized open-source platform based on blockchain domain. used to run
Ethereum is
exactly as it was programmed without th
contracts i.e. applications that execute the program censorship, or downtime. It serves a platform
possibility of any fraud, interference from a third party, generated by ethereu
for nearly 2,60,000 different cryptocurrencies. Ether is a cryptocurrency
blockchain.
miners, used to reward for the computations perfornmed to secure the
Solidity
Solidity is a brand-new programming language created by the Ethereum which is the
market of cryptocurrency by capitalization, released in the year 2015 1ed by second-largest
Christian Reitwiessner.
Some key features of solidityare listed below:
Solidity is a high-level
o It is statically-typed programming language designed for implementing smartcontracts.
object-oriented(contract-oriented)
o Solidity is highly influenced by
Virtual Machine(EVM).
language.
Python, c+-t, and JavaScript which runs on the
Ethereum
o Solidity supports
o Solidity is primarycomplex user-defined programming, libraries and inheritance.
o Solidity can be used language for blockchains running platforms.
to creating contracts like
signature wallets, etc. voting. blind auctions, crowdfunding, mut
Smart Contract
Smart contracts are high-level
theethereum
blockchain for program codes that are compiled to
interference further
of the third party, execution. It allowS us to EVM byte code and
smart contracts are Solidity (a these perform deployed 0
credible transactions without
transactions are trackable and
Python,but deprecated), LLL (alanguage library with similarities to Cirreversible. ay
low-level Lisp-like and Languages used to wI
REMIX IDE language), and JavaScript), Serpent
Mutan (Go-based, but (similar
Remix 1DE allows
deprecated).
blockchains. It can alsodeveloping,
be used asdeploying and
The Remix is an
a
administering
learning platform. Smart contracts for
Ethereum like
Solidity programminIntegrated
g language.Devel
TheoIIDE
pmentcanEnvironment
be used (|DE)compile,
or
CSE/SEM-VI /BCF/PRO1 to write, developing
and debugsmart contractsCO
the Solidity
writtenin.
Itwas JavaScript and supports testing, debugging and
Remix-IDEcanbe
more.
accessed in many different ways: deploying smart Contracts, and much
Step1:
INSTALLATION:
Step 2:
Docker
oRun this command to download the current stable release of Docker Compose:
Page 5
Sipna
College ofof
Department EngiComput
neerin
erg & Technology,
Science &
Session 2022-2023 Engineering
Amravati.
Branch :-Computer Sci. &
Subject :-Block Chain Engg.
FundamentalTeacher
s Lab manual
Manual
Class :- Final Year
Sem :- VII
PRACTICAL N0 2
AIM: Implement Diffie-Hellman Algorithm
S/W REQUIRED: Phython
Diffie-Hellman algorithm
The Diffie-Hellman algorithm is being used to establish a shared secret
communications while exchanging data over a public network using the that can be used for secret
get the secret key using the parameters. elliptic curve to generate points and
For the sake of simplicity and practical
one prime Pand G (a primitive root of P)implementation of the algorithm, we will consider only 4
and two private values a and b. variables,
P and Gare bothpublicly available numbers. Users
(say Alice and Bob) pick private values a and b and they
generate a key and exchange it publicly. The opposite person receives the key and that generates a secret
key, after which they have the same secret key to
encrypt.
Step by Step Explanation
Alice Bob
encrypt
symmetric secret key to
Users now have a
Example:
numbers P = 2.3, G=9
Step 1:Alice and Bob get public
and
Step 2: Alice selected a private key a = 4
Bob selected a private key b=3
Implementation:
thegenerated key
#gets
y= int(pow(G,5,P))
for Alice
# Secretkey P))
int(pow(y,a,
ka=
for Bob
# Secret key
P))
kb =int(pow(x, b,
is :%d'%(ka)
print('Secret key for the Alice :%d'%(kb))
print(Secret Keyfor the Bob is
Output:
The value of P: 23
The value of G:9
Diffie-Hellman Algorithm.
implemented a
CONCLUSION: Thus we have
CSE/SEM-V/BCF/PR02
Sipna College of
DepartmentofEngineering
Computer
&
Technology,
Science &
Amravati.
Session 2022-2023 Engineering
Branch:-Computer Sci. &Engg,
Subject :-Block Chain
FundamentalsTeacher
Lab manual
Manual
Class :- Final Year
Sem :- VII
PRACTICAL NO 3
AIM: Implement various SHA algorithms
SW REQUIRED: Phython
1. SHA256 : This hash function belong to hash class SHA-2, the internal block size of it is 32 bits.
2. SHA384:This hash function belong to hash class SHA-2, the internal block size of it is 32 bits. This
IS one of the truncated version.
3. :: This hash function belongto hash class SHA-2,the internal block size
SHA224
IS oneof the truncated
of it is 32 bits. This
version.
4. SHA5 12 :This hash function belong to hash class SHA-2, the internal block size of it is 64 bits.
5. SHA:The l60 bit hash function that resembles MD5 hash in working and was discontinued to be
used seeing its security vulnerabilities.
Implementation:
demonstrate
# Python 3 code to
# SHA hash algorithms.
import hashlib
#initializing string
str ="SIPNACOET"
# encoding SIPNA
# then sending to COETusing encode()
SIHAS120
result = hashlib.sha512(str.encode())
CSE/SEM-V/BCF/PRO3
Page 2
equivalenthexadecimal value.
the
print("The
printing hexadecimal equivalent of SHAS12 is: ")
!
print(result.hexdigest())
print("r")
#initializingstring
SIPNACOET"
str=
encoding ,SIlPNACOETusing encode()
# sendingtoSHA1)
#then hashli.shal (str.encode()
result=
equivalent hexadecimal value.
# printingthe
print("The hexadecimal equivalent of SHA1 is:")
print(result.hexdigest()
Output:
s9\hello\SHA algorithms.py
The hexadecimal equivalent of SHA256 is :
ebe3f7219907ff819e1fbae2327f25f85158114309f6cff1f48fcf3259c30784
The hexadecimal equivalent of SHA384 is :
dse62h881gh089ec7929933b6fc1928dd64b5df31bcde6381b9d3f90438d253248490460C9a5a1a873da8236C12ef9b3
The hexadecimal equivalent of SHA224 is :
173994f309f727ca939bb185036cd7b36e66141c9e52ba0bdcfd145d
The hexadecinal equivalent of SHAS12 is :
edsfts370a5bf7b92be4865cdf8b658a82209624e33ed7ICAe353bYdf254Y75db63d1ba35ad99fF26f1b399C31f3cG66a7f c67ecef3bdcdb7d6eedaa
9eb722
Page 3
PRACTICAL N0 4
AIM: Implement RSA Encryption and
Decryption
S/W REQUIRED: Phython
Rivest-Shamir-Adleman(RSA)
pSA abbreviation is
decrypt messages. t 1SRivest-Shamir--Adleman.
This algorithm is used by
an asymmetric cryptographic algorithm which meansmany that
companies to encrypt and
ie.. the public key and the private key. This is also known as there are two different keys
keys can be given to anyone. Companies such as Acer, Asus, HP, public-key cryptography because one of the
their products. Lenovo, etc., use encryption techniques in
How does RSA algorithm work?
let us learn the mechanism behind RSA algorithm by
considering one example :
1. Generating Public Key :
An integer.
Not be a factor of n.
decrypt 1394 :
5. Now wewill
n.
Decrypted Data = cd modcomes
Data out to be 89
Thus our Encrypted
"HI".
8 = H and I = 9 i.e.
Implementation:
import math
print("RSA ENCRYPTOR/DECRYPTOR") *** * *)
**** *** ********
**************k
nrint("****** ** * *** ***
check_p-prime_check(p)
check_q - prime_check(q)
while((check p==False)or(check_q-False))):
"))
p=int(input("Enter aprime number for p:"))
number for q:
q= int(input("Enter aprime
check p= prime check(p)
check q= prime check(q) Page2
CSE/SEM-V/BCF/PR04
RSAModulus
"CALCULATIONOF RSA MODULUS 'n'"
17p*q
("RSA Modulus(n) is:",n)
print
EulersToitent
CALCULATION OF EULERS TOITENT T"
rp-l)*(q-l)
print("Eulers Toitent(r) is:"r)
int(********************.
GCD
"CALCULATION OF GCD FOR 'e' CALCULATION,"
defegcd(e.r):
while(r!=0):
e,rFr,e%r
return e
#Euclid's Algorithm
defeugcd(e,r):
for iin range(1.r):
while(e!=0):
a.b=r/le.r%e
if(b!-0):
print("%d =%d*(%d) + %d"%(r.a.e.b))
Fe
e-b
#Multiplicative Inverse
def mult inv(e.r):
gcd,s, =eea(e.r)
if(ged!=1):
return None
else:
s-od."%(s,s,s%r)
if(s<0): 0, s= s(modr), i.e.,
print("'s=%d. Since %d is less than
elif(s>0):
print("'s=%d."%(s))
return s%r
CSE/SEM-V/BCF/PR04
for i in range(1,1000):
if(egcd(i,r--1):
e-i
print("The value ofe is:".e) ***********)
****** **
print(1 k**** ***** *****
#Encryption
"ENCRYPTION ALGORITHM.""
def encrypt(pub_key,n _text):
e,n-pub key
x=]
m=0
for i in n text:
if(i.isupper():
m =ord(i)-65
c-(m**e)%n
X.append(c)
elif(i.islower()):
m= ord(i)-97
c-(m**e) %n
X.append(c)
elif(i.isspace()):
spe-400
X.append(400)
returnX
#Decryption
"DECRYPTION ALGORITHM"
def decrypt(priv key,c_text):
d,n=priv_ key
txt-c text.split(, )
X="
m=0
for iin txt:
if(i==400): Page4
CSE/SEM-V/BCF/PR04
else:
m-(int(i)* *d) %n
m+=65
c=chr(m)
X+=c
return x
EMessage
2ne= input("What would you like encrypted or
print("Your message is:"message) decrypted2(Separate numbers with' for decryption):")
Choose Encrypt or Decrypt and Print
choose =input("Type '1' for encryption and '2'
ilchoose-=l): for decryption.")
enc msg=encrypt(public,message)
print("Your encrypted message is:",enc msg)
nrint("Thank you for using the RSA Encryptor. Goodbye!")
eifl choose-2):
nrint("Your decrypted message
else:
is:",decrypt(private,message))
print("Thank you for using the RSA Encryptor. Goodbye!")
print("You entered the wrong option.")
print("Thank you for using the RSA Encryptor. Goodbye!")
Output:
RSA ENCRYPTOR/DECRYPTOR
$****************** **** ****%**** ****%**** *
PLEASE ENTER THE 'p' AND 'q' VALUES BELOW:
Enter a prime number for p:3
Enter a prime number for q: 5
$**************k********%*********k **
RSA Modulus(n) is: 15
Eulers Toitent(r) is: 8
**********%***% %%********** *****
he value of e is: 999
$******** *;*
EUCLID'S ALGORITHM:
8=0*(999)
999 = +8
124*(8)1 +7
8=1*(7)+
END OF THE STEPS USED TO ACHIEVE EUCLID'S ALGORITHM,.
EUCLID'S+(-1y(7)
=8*(1) EXTENDED ALGORITHM;
=999*(-1l) +(125)*(8)
S--1.
END SiOFnce -1 is less than 0, s= s(modr), i.e.,s7.
The THE STEPS USED TO ACHIEVE THE
value of d is:1 VALUE OF 'd,
CSE/SEM-V/BCF/PR04
Page6