0% found this document useful (0 votes)
14 views

INS Practical

Uploaded by

sameermane7777
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)
14 views

INS Practical

Uploaded by

sameermane7777
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/ 4

Practical 1A - A Python code for implementing Caesar Cipher

def encrypt(text, s):


result = ""

# Traverse text
for i in range(len(text)):
char = text[i]

# Encrypt uppercase characters


if char.isupper():
result += chr((ord(char) + s - 65) % 26 + 65)
# Encrypt lowercase characters
elif char.islower():
result += chr((ord(char) + s - 97) % 26 + 97)
else:
result += char # Non-alphabetic characters are not changed

return result

# Check the above function


text = input("Enter the text to encrypt: ")
s=3
print("Text : " + text)
print("Cipher: " + encrypt(text, s))

Practical 1B - Python code for implementing Railfence Cipher

def RailFence(txt):
result = ""

# Traverse the string and add characters at even indices first


for i in range(len(txt)):
if i % 2 == 0:
result += txt[i]

# Then add characters at odd indices


for i in range(len(txt)):
if i % 2 != 0:
result += txt[i]

return result
# Get user input
string = input("Enter a string: ")
print(RailFence(string))

Practical 2 - Python Code for implementing RSA Alogorithm

from Crypto.PublicKey import RSA


from Crypto.Cipher import PKCS1_OAEP
import binascii

# Generate RSA key pair


keyPair = RSA.generate(1024)
pubKey = keyPair.publickey()

# Display public key


print(f"Public key: (n={hex(pubKey.n)}, e={hex(pubKey.e)})")
pubKeyPEM = pubKey.export_key()
print(pubKeyPEM.decode('ascii'))

# Display private key


print(f"Private key: (n={hex(pubKey.n)}, d={hex(keyPair.d)})")
privKeyPEM = keyPair.export_key()
print(privKeyPEM.decode('ascii'))

# Encryption
msg = 'Ismile Academy'
encryptor = PKCS1_OAEP.new(pubKey)
encrypted = encryptor.encrypt(msg.encode('utf-8')) # Encode the message to bytes
print("Encrypted:", binascii.hexlify(encrypted).decode('ascii')) # Decode to string for display

Practical 3 - Python code for implementing SHA Algorithm

import hashlib

# Get user input


str_input = input("Enter the value to encode: ")
result = hashlib.sha1(str_input.encode())

# Print the hexadecimal equivalent of SHA-1


print("The hexadecimal equivalent of SHA-1 is:")
print(result.hexdigest())
Practical 4 - Python code for implementing SHA Algorithm

from Crypto.Signature import PKCS1_v1_5


from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto import Random

def generate_signature(private_key, message):


# Load the private key
key = RSA.importKey(private_key)

# Generate SHA-256 hash of the message


hashed_message = SHA256.new(message.encode('utf-8'))

# Create a signature using the private key


signer = PKCS1_v1_5.new(key)
signature = signer.sign(hashed_message)
return signature

def verify_signature(public_key, message, signature):


# Load the public key
key = RSA.importKey(public_key)

# Generate SHA-256 hash of the message


hashed_message = SHA256.new(message.encode('utf-8'))

# Verify the signature using the public key


verifier = PKCS1_v1_5.new(key)
return verifier.verify(hashed_message, signature)

# Generate RSA key pair


random_generator = Random.new().read
key_pair = RSA.generate(2048, random_generator)

# Extract public and private keys


public_key = key_pair.publickey().export_key()
private_key = key_pair.export_key()

# Example usage
message = "Hello, World!"
# Generate a digital signature
signature = generate_signature(private_key, message)
print("Generated Signature:", signature)

# Verify the digital signature


is_valid = verify_signature(public_key, message, signature)
print("Signature Verification Result:", is_valid)

Practical 5 - Python code for implementing Diffie-Hellman Algorithm

from random import randint

if __name__ == '__main__':
P = 23
G=9

print('The Value of P is: %d' % (P))


print('The Value of G is: %d' % (G))

a = 4 # Secret number for Alice


print('Secret Number for Alice is: %d' % (a))
x = int(pow(G, a, P)) # Alice's public key

b = 6 # Secret number for Bob


print('Secret Number for Bob is: %d' % (b))
y = int(pow(G, b, P)) # Bob's public key

ka = int(pow(y, a, P)) # Secret key for Alice


kb = int(pow(x, b, P)) # Secret key for Bob

print('Secret Key for Alice is: %d' % (ka))


print('Secret Key for Bob is: %d' % (kb))

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy