Cybersecurityprgrams
Cybersecurityprgrams
import hashlib
def hash_password(password):
# Encode the password as bytes
password_bytes = password.encode('utf-8')
return password_hash
import random
import string
def generate_password(length=8):
# Define the characters to use in the password
all_characters = string.ascii_letters + string.digits + string.punctuation
return password
password_length_str = input("Input the desired length of your password:")
if password_length_str:
password_length = int(password_length_str)
else:
password_length = 8
Program 3: Write a Python program to check if a password meets the following criteria:
a. At least 8 characters long, b. Contains at least one uppercase letter, one lowercase
letter, one digit, and one special character (!, @, #, $, %, or &), c. If the password meets
the criteria, print a message that says "Valid Password." If it doesn't meet the criteria,
print a message that says "Password does not meet requirements."
import re
def validate_password(password):
# Check if the password has at least 8 characters
if len(password) < 8:
return False
if is_valid:
print("Valid Password.")
else:
print("Password does not meet requirements.")
Program 4: Write a Python program that reads a file containing a list of passwords, one
per line. It checks each password to see if it meets certain requirements (e.g. at least 8
characters, contains both uppercase and lowercase letters, and at least one number and
one special character). Passwords that satisfy the requirements should be printed by the
program
import re
def check_password(password):
# Define regular expressions for each requirement
length_regex = re.compile(r'^.{8,}$')
uppercase_regex = re.compile(r'[A-Z]')
lowercase_regex = re.compile(r'[a-z]')
digit_regex = re.compile(r'\d')
special_regex = re.compile(r'[\W_]')
# check length
if len(password) >= 8:
score += 1
else:
suggestions.append("Password should be at least 8 characters long")
# Make a request to "Have I Been Pwned" API to check if the password has
been leaked
response =
requests.get(f"https://api.pwnedpasswords.com/range/{password_hash[:5]}")
# If the response status code is 200, it means the password has been leaked
if response.status_code == 200:
# Get the list of hashes of leaked passwords that start with the same 5
characters as the input password
hashes = [line.split(':') for line in response.text.splitlines()]
# Check if the hash of the input password matches any of the leaked
password hashes
for h, count in hashes:
if password_hash[5:] == h:
print(f"Password for user {username} has been leaked {count}
times.")
break
else:
print(f"Could not check password for user {username}.")
Program 7: Write a Python program that simulates a brute-force attack on a password by
trying out all possible character combinations.
import itertools
import string
def bruteforce_attack(password):
chars = string.printable.strip()
attempts = 0
for length in range(1, len(password) + 1):
for guess in itertools.product(chars, repeat=length):
attempts += 1
guess = ''.join(guess)
if guess == password:
return (attempts, guess)
return (attempts, None)
# traverse text
for i in range(len(text)):
char = text[i]
return result
# calculating totient, t
t = (p - 1) * (q - 1)
# performing encryption
ct = (message ** e) % n
print(f"Encrypted message is {ct}")
# performing decryption
mes = (ct ** d) % n
print(f"Decrypted message is {mes}")
# Testcase - 1
RSA(p=53, q=59, message=89)
# Testcase - 2
RSA(p=3, q=7, message=12)
Program 11: Python program for encoding and decoding using Base64
import base64
string1=input("Enter the text:")
byte1=string1.encode('ascii')
byte2=base64.b64encode(byte1)
print("Encoded string is:",byte2)
decoded_bytes=base64.b64decode(byte2)
decoded_string=decoded_bytes.decode('ascii')
print("Decoded string is:",decoded_string)
Program 12: Python program to implement symmetric encryption using python library
# Fernet module is imported from the
# cryptography package
from cryptography.fernet import Fernet
# key is generated
key = Fernet.generate_key()