0% found this document useful (0 votes)
11 views6 pages

BT All

The document contains multiple code snippets demonstrating concepts in Solidity and Python, including a simple coin contract, an overflow check, RSA encryption, and the Diffie-Hellman key exchange. The Solidity contracts allow minting and sending coins, while the Python scripts implement mathematical functions for encryption and key exchange. Overall, it showcases basic implementations of cryptographic principles and smart contracts.

Uploaded by

arrollaprakash35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views6 pages

BT All

The document contains multiple code snippets demonstrating concepts in Solidity and Python, including a simple coin contract, an overflow check, RSA encryption, and the Diffie-Hellman key exchange. The Solidity contracts allow minting and sending coins, while the Python scripts implement mathematical functions for encryption and key exchange. Overall, it showcases basic implementations of cryptographic principles and smart contracts.

Uploaded by

arrollaprakash35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

pragma solidity ^0.5.

9;
contract Coin {
// The keyword "public" makes these variables readable from outside.
address public minter;
mapping(address => uint) public balances;
// Events allow light clients to react to changes efficiently.
event Sent(address from, address to, uint amount);
// Constructor: runs only when the contract is deployed.
constructor() public {
minter = msg.sender;
}

// Function to mint new coins (only the minter can mint).


function mint(address receiver, uint amount) public {
if (msg.sender != minter) return;
balances[receiver] += amount;
}

// Function to send coins to another address.


function send(address receiver, uint amount) public {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
pragma solidity ^0.5.0;
contract revertStatement {
function checkOverflow(uint _num1, uint _num2) public pure
returns(string memory, uint) {
uint sum = _num1 + _num2;
if(sum > 255){
revert ("Overflow Exist");
}
return ("No Overflow", sum);
}
}
RSA

def gcd(a, b):


return a if b == 0 else gcd(b, a % b)

def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
return False
return True

def modular_exponentiation(base, exp, mod):


result = 1
base %= mod
while exp > 0:
if exp % 2 == 1:
result = (result * base) % mod
exp //= 2
base = (base * base) % mod
return result

def find_d(e, phi):


for d in range(1, phi):
if (d * e) % phi == 1:
return d
return -1

def main():
while True:
p = int(input("Enter prime number p: "))
if is_prime(p):
break
print("Error: p must be a prime number.")

while True:
q = int(input("Enter prime number q: "))
if is_prime(q):
break
print("Error: q must be a prime number.")

e = int(input("Enter public exponent e: "))


n=p*q
phi = (p - 1) * (q - 1)

if gcd(e, phi) != 1:
print("e must be coprime with phi(n).")
return

d = find_d(e, phi)
if d == -1:
print("Could not find modular inverse.")
return

message = input("Enter message: ")

print("Encrypted: ", end="")


ciphertext = [modular_exponentiation(ord(char), e, n) for char in message]
print(" ".join(map(str, ciphertext)))

print("Decrypted: ", end="")


decrypted_message = "".join(chr(modular_exponentiation(c, d, n)) for c in ciphertext)
print(decrypted_message)

if __name__ == "__main__":
main()
DIFFIE
import random

def power_mod(base, exp, mod):


"""Efficient modular exponentiation."""
result = 1
base = base % mod
while exp > 0:
if exp % 2 == 1: # If exp is odd
result = (result * base) % mod
exp //= 2 # Divide exp by 2
base = (base * base) % mod
return result

def diffie_hellman(p, g):


"""Implements Diffie-Hellman key exchange."""
# Alice's private key
a = random.randint(1, p - 1)
# Bob's private key
b = random.randint(1, p - 1)

# Compute public keys


A = power_mod(g, a, p) # Alice's public key
B = power_mod(g, b, p) # Bob's public key

# Compute shared secrets


alice_shared_secret = power_mod(B, a, p)
bob_shared_secret = power_mod(A, b, p)

# Print results
print(f"Prime number (p): {p}")
print(f"Base (g): {g}")
print(f"Alice's private key: {a}")
print(f"Bob's private key: {b}")
print(f"Alice's public key (A): {A}")
print(f"Bob's public key (B): {B}")
print(f"Alice's shared secret: {alice_shared_secret}")
print(f"Bob's shared secret: {bob_shared_secret}")

# Verify that shared secrets match


if alice_shared_secret == bob_shared_secret:
print("The shared secret is the same for both Alice and Bob.")
else:
print("There is an error in the shared secret computation.")

# Input prime number (p) and base (g)


p = int(input("Enter a prime number (p): "))
g = int(input("Enter a base number (g): "))

# Run Diffie-Hellman key exchange


diffie_hellman(p, g)

You might also like