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

Cns Prac

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

Cns Prac

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

1.

Ceaser cipher

def ceaser_cipher(text, key):

encrypted_text = ""

for char in text:

if char.isalpha():

is_upper = char.isupper()

shifted = ord(char) + key

if is_upper:

if shifted > ord('Z'):

shifted -= 26

else:

if shifted > ord('z'):

shifted -=26

encrypted_text += chr(shifted)

else:

encrypted_text +=char

return encrypted_text

plaintext = input("Enter the plaintext: ")

key = int(input("Enter the key (integer between 1 and 25): "))

if 1 <= key <= 25:

encrypted_message = ceaser_cipher(plaintext, key)

print("Encrypted message:",encrypted_message)

else:

print("Invalid key")

def ceaser_decrypt(ciphertext, key):

decrypted_text = ""

for char in ciphertext:

if char.isalpha():

is_upper = char.isupper()

shifted = ord(char) - key

if is_upper:

if shifted < ord('A'):


shifted += 26

else:

if shifted < ord('a'):

shifted += 26

decrypted_text += chr(shifted)

else:

decrypted_text += char

return decrypted_text

ciphertext = input("Enter the encrypted message: ")

print("Brute Force Decryption: ")

for key in range(1, 26):

decrypted_message = ceaser_decrypt(ciphertext,key)

print(f"key{key}: {decrypted_message}")

2. Vernam cipher

def vernam_encrypt(message, key):

encrypted_message =""

for i in range(len(message)):

encrypted_char = chr((ord(message[i]) ^ ord(key[i])) % 26 + ord('A'))

encrypted_message += encrypted_char

return encrypted_message

def vernam_decrypt(encrypted_message, key):

decrypted_message = ""

for i in range(len(encrypted_message)):

decrypted_char = chr((ord(encrypted_message[i]) - ord(key[i])) % 26 + ord('A'))

decrypted_message += decrypted_char

return decrypted_message

message = input("Enter the message: ").upper()

key = input("Enter the key (same length as the message): ").upper()

if len(key) != len(message):
print("Key length must be the same as the message length.")

else:

encrypted_message = vernam_encrypt(message, key)

print("Encrypted Message:", encrypted_message)

decrypted_message = vernam_decrypt(encrypted_message, key)

print("Decrypted message:", decrypted_message)

3. rail fence

def fence_cipher_encrypt(text, rails):

fence = [['' for _ in range(len(text))] for _ in range(rails)]

rail = 0

direction = 1

for char in text:

fence[rail][fence[rail].index('')] = char

rail += direction

if rail == rails - 1 or rail == 0:

direction *= -1

encrypted_text = ''.join([char for rail in fence for char in rail if char != ''])

return encrypted_text

text = input("Enter text: ")

rails =3

encrypted_text = fence_cipher_encrypt(text, rails)

print(encrypted_text)

4.Diffie Hellman

def mod_pow(base, exponent, modulus):

result = 1

while exponent > 0:

if exponent % 2 == 1:

result = (result * base) % modulus


exponent //= 2

base = (base * base)% modulus

return result

def diffie_hellman(prime, base):

a_private = int(input("Alice's private key: "))

b_private = int(input("Bob's private key: "))

a_public = mod_pow(base, a_private, prime)

b_public = mod_pow(base, b_private, prime)

shared_secret_alice = mod_pow(b_public, a_private, prime)

shared_secret_bob = mod_pow(a_public, b_private, prime)

if shared_secret_alice == shared_secret_bob:

print("Shared secret:",shared_secret_alice)

else:

print("Key exchange failed: ")

if __name__ == "__main__":

prime = int(input("Enter prime modulus: "))

base = int(input("Enter base: "))

diffie_hellman(prime, base)

4. SHA 512

import hashlib

def enc(message):

md5 = hashlib.md5()

md5.update(message.encode('utf-8'))

return md5.hexdigest()

def enc1(message):

sha1 = hashlib.sha1()

sha1.update(message.encode('utf-8'))

return sha1.hexdigest()

message = input("Enter the input string")

print(f"MD5 Hashing String:{enc(message)}")

print(f"SHA-I Hashed String:{enc1(message)}")


5 RSA

import math

def gcd(a, h):

temp = 0

while(1):

temp = a % h

if (temp == 0):

return h

a=h

h = temp

p = int(input("enter the prime number p"))

q = int((input("Enter the prime number q")))

n = p*q

e=2

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

while (e < phi):

# e must be co-prime to phi and

# smaller than phi.

if(gcd(e, phi) == 1):

break

else:

e = e+1

def intcheck(phi, e):

k=1

while True:

d = (1 + (k * phi)) / e

if d == int(d):

return int(d)
k += 1

# Message to be encrypted

d=intcheck(phi,e)

msg = int(input("Enter the plaintext: "))

print("Message data = ", msg)

# Encryption c = (msg ^ e) % n

c = pow(msg, e)

c = math.fmod(c, n)

print("Encrypted data = ", c)

# Decryption m = (c ^ d) % n

m = pow(c, d)

m = math.fmod(m, n)

print("Original Message Sent = ", m)

6.DES

You might also like