Crypto Lab Manual
Crypto Lab Manual
Crypto Lab Manual
Indore
Prerequisites:
Python 3.x installed on your computer.
cryptography library installed. Install using pip install cryptography.
Lab Setup:
1. Install Required Libraries:
pip install pycryptodome
Sections:
1. Introduction to Asymmetric Encryption
2. Key Generation
3. Encryption
4. Decryption
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
priv_file.write(private_key)
pub_file.write(public_key)
Private Key
3. Encryption
Using the public key, you can encrypt a message.
public_key = RSA.import_key(pub_file.read())
# Message to be encrypted
cipher_rsa = PKCS1_OAEP.new(public_key)
encrypted_message = cipher_rsa.encrypt(message.encode())
encrypted_message_base64 = b64encode(encrypted_message)
Output
4. Decryption
Using the private key, you can decrypt the ciphertext back to the original message.
def add_padding(base64_string):
private_key = RSA.import_key(priv_file.read())
encrypted_message = b64decode(encrypted_message_base64)
cipher_rsa = PKCS1_OAEP.new(private_key)
decrypted_message = cipher_rsa.decrypt(encrypted_message)
Project 01
Topic:- Login Page which stores password as Hash in
database match it fir login access
import hashlib
print(hashlib.algorithms_available)
$OUTPUT-
import itertools
# Optionally, if you need the dictionary to hold some values, you can
initialize it differently, e.g., with a count or some default value
# word_dict = {''.join(combination): 0 for combination in
combinations}
$OUTPUT-
import hashlib
print(hashlib.md5(b'Hello World').hexdigest())
$OUTPUT-
# 4. Write a program to store the hash and salt using the PBKDF2_HMAC
algorithm?
import hashlib
import os
import base64
def generate_salt(length=16):
"""Generate a random salt."""
return os.urandom(length)
# Example usage:
password = "secure_password"
$OUTPUT-
import hashlib
print(hashlib.sha256(b'Hello World').hexdigest())
$OUTPUT-
import hashlib
print(hashlib.sha512(b'cryptography').hexdigest())
$OUTPUT-
# 7. Write a program to convert the string 'hello world' to a hash
using the sha3_512 hashing algorithm
import hashlib
print(hashlib.sha3_512(b'hello World').hexdigest())
$OUTPUT-
import hashlib
print(hashlib.blake2b(b'black2b hash').hexdigest())
$OUTPUT-
import hashlib
import os
import base64
import json
def generate_salt(length=16):
"""Generate a random salt."""
return os.urandom(length)
def load_user_db(filename='user_db.json'):
"""Load the user database from a file."""
try:
with open(filename, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
def main():
user_db = load_user_db()
if __name__ == "__main__":
main()
$OUTPUT-
alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
in_string = input("Enter message, Hint HELLO: ")
shift = int(input("Enter a shift number :"))
n = len(in_string)
out_string = ""
for i in range(n):
c = in_string[i]
loc = alpha.find(c.upper())
new_loc = loc + shift
if new_loc >= 26:
new_loc -= 26
out_string += alpha[new_loc]
print (i, c, loc, new_loc, out_string)
print ("Output After Encryption :", out_string)
$OUTPUT-
import hashlib
print(hashlib.algorithms_guaranteed)
$OUTPUT-
import hashlib
print(hashlib.shake_128(b'128 hashing').hexdigest(20))
$OUTPUT-
# 13. Write a program for password verification attempt with
(incorrect password or correct password) using any hashing algorithm?
import hashlib
import os
import base64
import json
def generate_salt(length=16):
"""Generate a random salt."""
return os.urandom(length)
def load_user_db(filename='user_db.json'):
"""Load the user database from a file."""
try:
with open(filename, 'r') as f:
return json.load(f)
except FileNotFoundError:
return {}
def main():
user_db = load_user_db()
while True:
action = input("Do you want to add a user (add) or verify a
password (verify)? ").strip().lower()
if action == 'add':
username = input("Enter username: ").strip()
password = input("Enter password: ").strip()
add_user(username, password, user_db)
save_user_db(user_db)
print(f"User '{username}' added successfully!")
if username in user_db:
stored_data = user_db[username]
is_valid = verify_password(
stored_data['hash'], password,
stored_data['salt'],
stored_data['iterations'],
stored_data['hash_name']
)
if is_valid:
print("Password is correct!")
else:
print("Password is incorrect!")
else:
print("Username not found.")
else:
print("Invalid action. Please enter 'add' or 'verify'.")
if __name__ == "__main__":
main()
$OUTPUT-
$OUTPUT-
import hashlib
print(hashlib.sha3_384(b'crypto').hexdigest())
$OUTPUT-
import hashlib
print(hashlib.sha1(b'sha1 hash').hexdigest())
$OUTPUT-
import os
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms,
modes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.asymmetric import padding as
asym_padding
input_file = 'test.jpeg'
output_file = 'test_encrypted.jpeg'
encrypt_file(input_file, output_file, key, iv)
if __name__ == "__main__":
main()
import hashlib
print(hashlib.sha3_256(b'crypto world').hexdigest())
$OUTPUT-
import hashlib
print(hashlib.shake_256(b'cryptography').hexdigest(20))
$OUTPUT-
import hashlib
print(hashlib.shake_256(b'cryptography').hexdigest(20))
$OUTPUT-