Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

assignment_datap_suryanshOri

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Assignment(Data Privacy Practical

Qs)

Q1. Write a program to perform encryption and decryption


using Caesar cipher

Code :
#include<iostream>
#include<string>
using namespace std;

int main() {
int key;
string strInp;
cout << "Enter the string here: ";
cin >> strInp;
cout << "Enter the key value: ";
cin >> key;
string enCodedStr;

for (int i = 0; i < strInp.length(); i++) {


char atPoint = strInp[i];
int valOfNum = int(atPoint);
valOfNum = valOfNum + key;
char encodedChar = char(valOfNum);
enCodedStr = enCodedStr + encodedChar;
}
cout << enCodedStr;

return 0;
}
Output :

Q2. Write a program to perform encryption and decryption


using Rail Fence Cipher (transpositional cipher)

Code:
def encrypt(text, key):
rails = ['' for _ in range(key)]
direction_down = False
row = 0

for char in text:


rails[row] += char
if row == 0 or row == key - 1:
direction_down = not direction_down
row += 1 if direction_down else -1

encrypted_text = ''.join(rails)
return encrypted_text

def decrypt(cipher, key):


rails = ['' for _ in range(key)]
direction_down = None
row = 0
index = 0

for i in range(len(cipher)):
rails[row] += '*'
if row == 0:
direction_down = True
elif row == key - 1:
direction_down = False
row += 1 if direction_down else -1

for r in range(key):
for j in range(len(rails[r])):
if rails[r][j] == '*':
rails[r] = rails[r][:j] + cipher[index] +
rails[r][j+1:]
index += 1

result = []
row = 0
direction_down = False

for i in range(len(cipher)):
result.append(rails[row][0])
rails[row] = rails[row][1:]
if row == 0:
direction_down = True
elif row == key - 1:
direction_down = False
row += 1 if direction_down else -1

return ''.join(result)

if __name__ == "__main__":
text = input("Enter the text to be encrypted or
decrypted: ")
key = int(input("Enter the value of key: "))
enOrDe = input("Enter your Choice(E/D): ")
if(enOrDe=='E' or enOrDe=='e'):
encrypted_text = encrypt(text, key)
print(f"Encrypted Text: {encrypted_text}")
elif(enOrDe=='D' or enOrDe=='d'):
decrypted_text = decrypt(text, key)
print(f"Decrypted Text: {decrypted_text}")
else:
print("Incorrect Choice")

Output :
Q3. Write a Python program that defines a function and takes a
password string as input and returns its SHA-256 hashed
representation as a hexadecimal string.

Code:
import hashlib

def hash_password(password):
sha256 = hashlib.sha256()
return sha256.hexdigest()

password = input("Enter your password: ")


hashed_password = hash_password(password)
print("Hashed Password is:",hashed_password)

Output:
Q5. Write a Python program that generates a password using a
random combination of words from a dictionary file.

Code:
import random
def load_words(file_path):
with open(file_path,'r') as file:
words = file.read().splitlines()
return words

def genPass(words, words_count = 4):


return ''.join(random.sample(words, words_count))

def main():
dictionary_file = r'random_words.txt'
words = load_words(dictionary_file)
password = genPass(words)
print(f"Generated Password: {password}")

if __name__ == "__main__":
main()
Output:

Q6. Write a Python program that simulates a brute-force attack


on a password by trying out all possible character
combinations.

Code:
import string
import itertools
import time

def bforce(password):
characters = string.ascii_letters + string.digits +
string.punctuation
attempts = 0

start_time = time.time()

for length in range(1, len(password) + 1):


for guess_tuple in itertools.product(characters,
repeat=length):
guess = ''.join(guess_tuple)
attempts += 1
if guess == password:
end_time = time.time()
total_time = end_time - start_time
print("Password found:", guess, "in",
attempts, "attempts and took", total_time, "seconds")
return

end_time = time.time()
total_time = end_time - start_time
print("Password not found in", attempts, "attempts
and took", total_time, "seconds")

def main():
password = input("Enter the password to be cracked:
")
bforce(password)

if __name__ == "__main__":
main()

Output:

You might also like