Program-1
Aim: To develop a program to implement encryption and decryption using rail fence
transportation technique.
Code: def rail_fence_encrypt(plaintext, rails):
ciphertext = [''] * rails
index, step = 0, 1
for char in plaintext:
ciphertext[index] += char
if index == 0:
step = 1
elif index == rails - 1:
step = -1
index += step
return ''.join(ciphertext)
def rail_fence_decrypt(ciphertext, rails):
plaintext = [''] * len(ciphertext)
index, step = 0, 1
for i, char in enumerate(ciphertext):
plaintext[index] += char
if index == 0:
step = 1
elif index == rails - 1:
step = -1
index += step
return ''.join(plaintext)
# Example usage
plaintext = "Anirudh Kaushik 22SCSE1012524"
rails = 3
ciphertext = rail_fence_encrypt(plaintext, rails)
print("Ciphertext:", ciphertext)
decrypted_text = rail_fence_decrypt(ciphertext, rails)
print("Decrypted Text:", decrypted_text)
Program-2
Aim: To develop a program to implement Data Encryption Standard for encryption and
decryption.
Code: from Crypto.Cipher import DES
def des_encrypt(plaintext, key):
cipher = DES.new(key, DES.MODE_ECB)
ciphertext = cipher.encrypt(plaintext)
return ciphertext
def des_decrypt(ciphertext, key):
cipher = DES.new(key, DES.MODE_ECB)
plaintext = cipher.decrypt(ciphertext)
return plaintext
# Example usage
plaintext = b"Anirudh Kaushik 22SCSE1012524"
key = b"12345678"
ciphertext = des_encrypt(plaintext, key)
print("Ciphertext:", ciphertext.hex())
decrypted_text = des_decrypt(ciphertext, key)
print("Decrypted Text:", decrypted_text.decode())
Program-3
Aim: To develop a program to implement Advanced Encryption Standard for encryption
and decryption.
Code: from Crypto.Cipher import AES
def aes_encrypt(plaintext, key):
cipher = AES.new(key, AES.MODE_ECB)
ciphertext = cipher.encrypt(plaintext)
return ciphertext
def aes_decrypt(ciphertext, key):
cipher = AES.new(key, AES.MODE_ECB)
plaintext = cipher.decrypt(ciphertext)
return plaintext
# Example usage
plaintext = b"Anirudh Kaushik,22SCSE1012524"
key = b"1234567890123456"
ciphertext = aes_encrypt(plaintext, key)
print("Ciphertext:", ciphertext.hex())
decrypted_text = aes_decrypt(ciphertext, key)
print("Decrypted Text:", decrypted_text.decode())
Program-4
Aim: Develop a program to implement RSA algorithm for encryption and decryption.
Code: from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
def rsa_encrypt(plaintext, public_key):
cipher = PKCS1_OAEP.new(public_key)
ciphertext = cipher.encrypt(plaintext)
return ciphertext
def rsa_decrypt(ciphertext, private_key):
cipher = PKCS1_OAEP.new(private_key)
plaintext = cipher.decrypt(ciphertext)
return plaintext
# Example usage
key = RSA.generate(2048)
public_key = key.publickey()
private_key = key
plaintext = b"Anirudh Kaushik,22SCSE1012524"
ciphertext = rsa_encrypt(plaintext, public_key)
print("Ciphertext:", ciphertext.hex())
decrypted_text = rsa_decrypt(ciphertext, private_key)
print("Decrypted Text:", decrypted_text.decode())
Program-5
Aim: Develop a program to implement Secure Hash Algorithm.
Code: import hashlib
def sha256_hash(message):
hash_object = hashlib.sha256()
hash_object.update(message.encode())
return hash_object.hexdigest()
# Example usage
message = "Anirudh Kaushik,22SCSE1012524"
hash_value = sha256_hash(message)
print("SHA-256 Hash:", hash_value)
Program-6
Aim: Implement Markel tree to encoded block chain data.
Code: import hashlib
def merkle_tree(leaves):
if len(leaves) == 1:
return leaves[0]
else:
hashes = []
for i in range(0, len(leaves), 2):
leaf1 = leaves[i]
leaf2 = leaves[i + 1] if i + 1 < len(leaves) else ''
hash_object = hashlib.sha256()
hash_object.update((leaf1 + leaf2).encode())
hashes.append(hash_object.hexdigest())
return merkle_tree(hashes)
# Example usage
leaves = ["Anirudh", "Kaushik", "Blockchain", "22SCSE1012524"]
merkle_root = merkle_tree(leaves)
print("Merkle Root:", merkle_root)
Program-7
Aim: How to Create a consecutive Block.
Code: import hashlib
class Block:
def __init__(self, data, previous_hash):
self.data = data
self.previous_hash = previous_hash
self.hash = self.calculate_hash()
def calculate_hash(self):
hash_object = hashlib.sha256()
hash_object.update((self.data + self.previous_hash).encode())
return hash_object.hexdigest()
# Example usage
block1 = Block("AnirudhKaushik", "0")
block2 = Block("22SCSE1012524", block1.hash)
print("Block 1 Hash:", block1.hash)
print("Block 2 Hash:", block2.hash)
Program-8
Aim: Write a program in Solidity Language.
Code: pragma solidity ^0.8.0;
contract SimpleContract {
address private owner;
constructor() public {
owner = msg.sender;
function getOwner() public view returns (address) {
return owner;
}
Program-9
Aim: Implementation Supply chain Management system through Blockchain.
Code: import hashlib
class SupplyChain:
def __init__(self):
self.blocks = []
def add_block(self, data):
previous_hash = self.get_previous_hash()
block = Block(data, previous_hash)
self.blocks.append(block)
def get_previous_hash(self):
if len(self.blocks) == 0:
return "0"
else:
return self.blocks[-1].hash
class Block:
def __init__(self, data, previous_hash):
self.data = data
self.previous_hash = previous_hash
self.hash = self.calculate_hash()
def calculate_hash(self):
hash_object = hashlib.sha256()
hash_object.update((self.data + self.previous_hash).encode())
return hash_object.hexdigest()
# Example usage
supply_chain = SupplyChain()
supply_chain.add_block("Anirudh")
supply_chain.add_block("Kaushik")
supply_chain.add_block("22SCSE1012524")
print("Supply Chain Blocks:")
for block in supply_chain.blocks:
print("Hash:", block.hash)
print("Data:", block.data)
Program-10
Aim: Implementation of Healthcare solution through Block chain.
Code: import hashlib
class HealthcareSystem:
def __init__(self):
self.blocks = []
def add_block(self, data):
previous_hash = self.get_previous_hash()
block = Block(data, previous_hash)
self.blocks.append(block)
def get_previous_hash(self):
if len(self.blocks) == 0:
return "0"
else:
return self.blocks[-1].hash
class Block:
def __init__(self, data, previous_hash):
self.data = data
self.previous_hash = previous_hash
self.hash = self.calculate_hash()
def calculate_hash(self):
hash_object = hashlib.sha256()
hash_object.update((self.data + self.previous_hash).encode())
return hash_object.hexdigest()
# Example usage
healthcare_system = HealthcareSystem()
healthcare_system.add_block("Patient medical record")
healthcare_system.add_block("Patient treatment plan")
healthcare_system.add_block("Patient test results")
print("Healthcare System Blocks:")
#Anirudh Kaushik 22SCSE1012524
for block in healthcare_system.blocks:
print("Hash:", block.hash)
print("Data:", block.data)