INS Practical Journal
INS Practical Journal
1) Design and implement algorithms to encrypt and decrypt messages using classical
substitution techniques
def encrypt(text,s):
result = ""
# traverse text
for i in range(len(text)):
char = text[i]
return result
Output-
import java.io.*;
class GFG {
public static char normalChar[]
= { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
// return encryptedString
return encryptedString;
}
// printing encryptedString
System.out.println("Encrypted message: "
+ encryptedString);
Output-
2) Design and implement algorithms to encrypt and decrypt messages using classical
Transposition techniques
for i in range(len(text)):
rail[row][col] = text[i]
col += 1
if dir_down:
row += 1
else:
row -= 1
result = []
for i in range(key):
for j in range(len(text)):
if rail[i][j] != '\n':
result.append(rail[i][j])
return("" . join(result))
dir_down = None
row, col = 0, 0
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key - 1:
dir_down = False
rail[row][col] = '*'
col += 1
if dir_down:
row += 1
else:
row -= 1
index = 0
for i in range(key):
for j in range(len(cipher)):
if ((rail[i][j] == '*') and
(index < len(cipher))):
rail[i][j] = cipher[index]
index += 1
result = []
row, col = 0, 0
for i in range(len(cipher)):
if row == 0:
dir_down = True
if row == key-1:
dir_down = False
if (rail[row][col] != '*'):
result.append(rail[row][col])
col += 1
if dir_down:
row += 1
else:
row -= 1
return("".join(result))
# Driver code
if __name__ == "__main__":
print(encryptRailFence("Vanga", 3))
print(encryptRailFence("Sapdunga ", 3))
print(encryptRailFence("Poitu Vanga", 3))
print(decryptRailFence("Vaagn", 3))
print(decryptRailFence("Su adnapg", 3))
print(decryptRailFence("Punot agiVa", 3))
Output:
def removeSpaces(text):
newText = ""
for i in text:
if i == " ":
continue
else:
newText = newText + i
return newText
def Diagraph(text):
Diagraph = []
group = 0
for i in range(2, len(text), 2):
Diagraph.append(text[group:i])
group = i
Diagraph.append(text[group:])
return Diagraph
def FillerLetter(text):
k = len(text)
if k % 2 == 0:
for i in range(0, k, 2):
if text[i] == text[i+1]:
new_word = text[0:i+1] + str('x') + text[i+1:]
new_word = FillerLetter(new_word)
break
else:
new_word = text
else:
for i in range(0, k-1, 2):
if text[i] == text[i+1]:
new_word = text[0:i+1] + str('x') + text[i+1:]
new_word = FillerLetter(new_word)
break
else:
new_word = text
return new_word
list1 = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
compElements = []
for i in key_letters:
if i not in compElements:
compElements.append(i)
for i in list1:
if i not in compElements:
compElements.append(i)
matrix = []
while compElements != []:
matrix.append(compElements[:5])
compElements = compElements[5:]
return matrix
char2 = ''
if e2c == 4:
char2 = matr[e2r][0]
else:
char2 = matr[e2r][e2c+1]
char2 = ''
if e2r == 4:
char2 = matr[0][e2c]
else:
char2 = matr[e2r+1][e2c]
char2 = ''
char2 = matr[e2r][e1c]
if ele1_x == ele2_x:
c1, c2 = encrypt_RowRule(Matrix, ele1_x, ele1_y, ele2_x, ele2_y)
# Get 2 letter cipherText
elif ele1_y == ele2_y:
c1, c2 = encrypt_ColumnRule(Matrix, ele1_x, ele1_y, ele2_x, ele2_y)
else:
c1, c2 = encrypt_RectangleRule(
Matrix, ele1_x, ele1_y, ele2_x, ele2_y)
cipher = c1 + c2
CipherText.append(cipher)
return CipherText
text_Plain = 'Vetrivel Muruganuku'
text_Plain = removeSpaces(toLowerCase(text_Plain))
PlainTextList = Diagraph(FillerLetter(text_Plain))
if len(PlainTextList[-1]) != 2:
PlainTextList[-1] = PlainTextList[-1]+'z'
key = "Arogharaa"
print("Key text:", key)
key = toLowerCase(key)
Matrix = generateKeyTable(key, list1)
CipherText = ""
for i in CipherList:
CipherText += i
print("CipherText:", CipherText)
Output:
Practical-2
Code:
import math
#step1
p=3
q=7
#step2
n=p*q
print("n =",n)
#step3
phi=(p-1)* (q-1)
#step4
e=2
while (e<phi):
if (math.gcd(e, phi) == 1):
break
else:
e+=1
print("e =",e)
#step5
k=2
d=((k*phi)+1)/e
print("d =",d)
print("Public key:",e,n)
print("Private key:",d,n)
#plaintext
msg=11
print("Original message:",msg)
#encryption
C=pow(msg,e)
C=math.fmod(C,n)
print("Encrypted message:",C)
#decryption
M=pow(C,d)
M=math.fmod(M,n)
print("Decrypted message:",M)
Output:
Practical 3
Message Authentication Codes: Implement algorithms to generate and verify message authentication
codes (MACs) for ensuring data integrity and authenticity.
1) Using hmac
import hmac
message="Welcome to TYCS."
key="INS"
hmac2=hmac.new(key=key.encode(), digestmod="sha1")
hmac2.update(bytes(message, encoding="utf-8"))
message_digest2=hmac2.digest()
print("Message Digest 2: ",hmac2.name, message_digest2)
hmac3=hmac.new(key=key.encode(), digestmod="sha1")
hmac3.update(bytes("Welcome to ", encoding="utf-8"))
hmac3.update(bytes("CoderzColumn.", encoding="utf-8"))
message_digest3=hmac3.digest()
Output:
2) Python Code for implementing MD5 Algorithm
import hashlib
result = hashlib.md5(b'NetworkSecurity')
result1 = hashlib.md5(b'NetworkSecuriti')
Output:
import hashlib
str = input(" Enter the value to encode ")
result = hashlib.sha1(str.encode())
print("The hexadecimal equivalent if SHA1 is : ")
print(result.hexdigest())
Output:
Practical 4
# Verify the signature using the public key and the original hash
try:
pkcs1_15.new(RSA.import_key(public_key)).verify(original_hash, signature)
print("Signature is valid.")
except (ValueError, TypeError):
print("Signature is invalid.")
Output:
Practical - 5
Code:
def prime_checker(p):
if p<1:
return -1
elif p>1:
if p==2:
return 1
for i in range(2,p):
if p%i==0:
return-1
return 1
def primitive_check(g,p,L):
for i in range(1,p):
L.append(pow(g, i) % p)
for i in range(1,p):
if L.count(i)>1:
L.clear()
return -1
return 1
l=[]
while 1:
P=int(input("Enter P:"))
if prime_checker(P)==-1:
print("Number Is Not Prime, Please Enter Again!")
continue
break
while 1:
G=int(input(f"Enter The Primitive Root Of {P}:"))
if primitive_check(G,P,l) == -1:
print(f"Number Is Not A Primitive Root Of {P}. Please Try Again!")
continue
break
x1,x2=int(input("Enter The Private Key Of User 1:")),int(
input("Enter The Private Key Of User 2:"))
while 1:
if x1>P or x2>=P:
print(f"Private Key Of Both The Users Should Be Less Than (P)!")
continue
break
y1,y2 = pow(G,x1) % P,pow(G, x2) % P
k1,k2= pow(y2,x1) % P,pow(y1, x2) % P
if k1== k2:
print("Keys Have Been Exchanged Successfully")
else:
print("Keys Have Not Been Exchanged Successfully")
Output:
Practical – 6
Part 1: Implementing the Topology using Cisco Packet Tracer, configure the IP and set
the IP route
Step1 : Implement the topology as given in the below diagram
Part 2: Configure the Hostname on all Routers and enable the security
package on R1 and R2, Ping on PC from the other(All packets are lost)
Step 1: Click on router 1 click on CLI and execute the following command
Router>enable
Router#configure terminal
Router(config)#ip route
0.0.0.0 0.0.0.0 20.0.0.2
Step 2 : Click on router 2 and click on CLI and execute the following command
Router>enable
Router#configure terminal
Router(config)#ip route 0.0.0.0 0.0.0.0 30.0.0.2
Step3: Now ping one PC from another from the command line( Ping should fail)
Part 3: Apply the Access Control List(ACL) at Router 1 and 2, Set the
ISALMP policy and ISAKMP key, Set IPSec transform set
Step 1: Go to the CLI of router 1 and type the following command and enable
security package
Router>enable
Router#configure terminal
Router(config)#hostname R1
R1(config)#exit
R1#copy run startup-config
R1#reload
R1>enable
R1#show version
R1(config-isakmp)#exit
R1(config)#crypto isakmp key ismile address 30.0.0.1
R1(config)#crypto ipsec transform-set R1->R2 esp-aes 256 esp-sha-hamc
R1(config)#crypto ipsec transform-set R2->R1 esp-aes 256 esp-sha-hamc
0.0.0.255
R2 (config)#crypto isakmp policy 10
R2 (config-isakmp)#encryption aes 256
R2 (config-isakmp)#authentication pre-share R2
(config-isakmp)#group 5
R2 (config-isakmp)#exit
R2(config)#crypto isakmp key ismile address 20.0.0.1
R2(config)#crypto ipsec transform-set R2->R1 esp-aes 256 esp-sha-hamc
Part 4 : Create the crypto map and apply to the required interface. Verify the output by
pinging one PC from other
R1(config)#Crypto map IPSEC-MAP 10 ipsec-isakmp
R1(Config-crypto-map)#set peer 30.0.0.1
Turn on the IIS Management Console and World Wide Web services
Now from window explorer, open IIS Manager
Click on server in the left pane and Choose default web site.
Create Self Signed Certificate, give a proper name and certificate store as Web hosting
Click on Default Website & Click on bindings and bind with type https port 443 by clicking Add btn.
Type localhost in google URL and you would get the following page
Now, Type https://localhost in google URL and you would get the following page
Now, click on Advanced and then click on proceed to localhost and you will get the following screen.
Practical – 9
Aim: To do Detect and Analyse Malware
Steps:
Open the website www.virusshare.com to download the clean sample of Malware.
Create account by sending a mail to Melissa at [email protected] with 'access' in the subject. She
will review your request and hopefully send you an invitation link.
By clicking the above download icon the Malware gets downloaded in ZIP format.
Do not unzip the file, we create a folder “Malware” on desktop and save the file in the folder. In order to
analyse the Malware, we select the website www.virustotal.com
Click on “Choose File” and select the file from the location (ZIP file will do, if asks for password enter
infected). We get the following after the upload is completed
After clicking the ‘Next’ button we need to name the rule and click finish
The Inbound rule is added
We repeat all the above steps for creating ‘Outbound Rules’, and then try to access the internet.
We see that the accessed is blocked
Part 2: Blocking the website www.android.com
We open the browser and access the website, which is now accessible
IPv4 216.58.196.68
IPv6 2404:6800:4009:809::2004
We open the windows Firewall settings and apply the Inbound Rule
Insert the IP addresses both IPv4 and IPv6