Documente Academic
Documente Profesional
Documente Cultură
Date:__________
Certificate
SYBSC CS Semester _5_ has completed the practical work in the subject of
“Information & Network Security” during the academic year 2019 – 20 under the
Guidance of Prof. Soni yadav being the partial requirement for the fulfilment of the
College Seal
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
INDEX
Practical No : 01
Aim :1.Write programs to implement the following Substitution Cipher Techniques:
1. Caesar Cipher.
Input :
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text
namespace Caesar
{
class Program
{
static void Main(string[] args)
{
char c, a;
Console.WriteLine("Enter a Plain Text : ");
string PT = Console.ReadLine();
PT = PT.ToLower();
Console.WriteLine("Cipher Text : ");
for (inti = 0; i<PT.Length; i++)
{
c = Convert.ToChar(PT.Substring(i, 1));
if ((int)c + 3 > 122) a = (char)(c + 3 - 26);
else a = (char)(c + 3);
Console.Write(a);
}
Console.Read();
//z=122
}
}
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
2. Monoalphabetic Cipher
Input :
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceMonoAlphabeticCipher
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[26];
Console.WriteLine("Enter a number");
int n = Convert.ToInt32(Console.ReadLine());
string[] c = { "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" };
for (inti = 0; i< 26; i++)
{
Random r = new Random();
a[i] = r.Next(0, 26);
if (i> 0)
{
for (int j = 0; j <i; j++)
{
if (a[i] == a[j])
{
a[i] = r.Next(0, 26);
}
}
}
Console.WriteLine(a[i]);
}
Console.WriteLine("Enter a String");
string PT = Console.ReadLine();
PT = PT.ToLower();
Console.WriteLine("\n\n");
for (inti = 0; i<PT.Length; i++)
{
for (int j = 0; j < 26; j++)
{
if (c[j] == PT.Substring(i, 1))
{
Console.Write(c[a[j]]);
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
break;
}
else if (PT.Substring(i, 1) == " ")
{
Console.Write(" ");
break;
}
}
Console.ReadLine();
}
}
}
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Practical No : 02
Aim :Write programs to implement the following Substitution Cipher Techniques:
1. Playfair Cipher
Input :
using System;
usingSystem.Linq;
usingSystem.Text.RegularExpressions;
namespacePlayfairCipherCSharp
{
class Program
{
static void Main(string[] args)
{
string SECRET_KEY = "KGYLOPNU";
Console.WriteLine("Enter your String for Playfair Cipher Encryption:");
Console.Write("\n");
stringsPlainText = Console.ReadLine().ToLower();
Console.Write("\n");
stringsCipherText = PlayfairEncryption(sPlainText, SECRET_KEY);
Console.WriteLine("Your Cipher Text: " + sCipherText);
Console.Write("\n");
stringsDecryptedPlainText = PlayfairDecryption(sCipherText, SECRET_KEY);
Console.WriteLine("Your Plain Text: " + sDecryptedPlainText);
Console.Read();
}
static string PlayfairEncryption(string sInput, string sKey)
{
stringsEncryptedText = string.Empty;
if ((sKey != "") && (sInput != ""))
{
sKey = sKey.ToLower();
stringsGrid = null;
stringsAlpha = "abcdefghiklmnopqrstuvwxyz";
sInput = sInput.ToLower();
stringsOutput = "";
Regex rgx = new Regex("[^a-z-]");
sKey = rgx.Replace(sKey, "");
sKey = sKey.Replace('j', 'i');
for (inti = 0; i<sKey.Length; i++)
{
if ((sGrid == null) || (!sGrid.Contains(sKey[i])))
{
sGrid += sKey[i];
}
}
for (inti = 0; i<sAlpha.Length; i++)
{
if (!sGrid.Contains(sAlpha[i]))
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
{
sGrid += sAlpha[i];
}
}
sInput = rgx.Replace(sInput, "");
sInput = sInput.Replace('j', 'i');
for (inti = 0; i<sInput.Length; i += 2)
{
if (((i + 1) <sInput.Length) && (sInput[i] == sInput[i + 1]))
{
sInput = sInput.Insert(i + 1, "x");
}
}
if ((sInput.Length % 2) > 0)
{
sInput += "x";
}
intiTemp = 0;
do
{
intiPosA = sGrid.IndexOf(sInput[iTemp]);
intiPosB = sGrid.IndexOf(sInput[iTemp + 1]);
intiRowA = iPosA / 5;
intiColA = iPosA % 5;
intiRowB = iPosB / 5;
intiColB = iPosB % 5;
if (iColA == iColB)
{
iPosA += 5;
iPosB += 5;
}
else
{
if (iRowA == iRowB)
{
if (iColA == 4)
{
iPosA -= 4;
}
else
{
iPosA += 1;
}
if (iColB == 4)
{
iPosB -= 4;
}
else
{
iPosB += 1;
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
}
}
else
{
if (iRowA<iRowB)
{
iPosA -= iColA - iColB;
iPosB += iColA - iColB;
}
else
{
iPosA += iColB - iColA;
iPosB -= iColB - iColA;
}
}
}
if (iPosA>= sGrid.Length)
{
iPosA = 0 + (iPosA - sGrid.Length);
}
if (iPosB>= sGrid.Length)
{
iPosB = 0 + (iPosB - sGrid.Length);
}
if (iPosA< 0)
{
iPosA = sGrid.Length + iPosA;
}
if (iPosB< 0)
{
iPosB = sGrid.Length + iPosB;
}
sOutput += sGrid[iPosA].ToString() + sGrid[iPosB].ToString();
iTemp += 2;
} while (iTemp<sInput.Length);
sEncryptedText = sOutput;
}
returnsEncryptedText;
}
static string PlayfairDecryption(string sCipherText, string sKey)
{
sKey = sKey.ToLower();
stringsGrid = null;
stringsAlpha = "abcdefghiklmnopqrstuvwxyz";
stringsInput = sCipherText.ToLower();
stringsOutput = "";
sKey = sKey.Replace('j', 'i');
for (inti = 0; i<sKey.Length; i++)
{
if ((sGrid == null) || (!sGrid.Contains(sKey[i])))
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
{
sGrid += sKey[i];
}
}
for (inti = 0; i<sAlpha.Length; i++)
{
if (!sGrid.Contains(sAlpha[i]))
{
sGrid += sAlpha[i];
}
}
intiTemp = 0;
do
{
intiPosA = sGrid.IndexOf(sInput[iTemp]);
intiPosB = sGrid.IndexOf(sInput[iTemp + 1]);
intiRowA = iPosA / 5;
intiColA = iPosA % 5;
intiRowB = iPosB / 5;
intiColB = iPosB % 5;
if (iColA == iColB)
{
iPosA -= 5;
iPosB -= 5;
}
else
{
if (iRowA == iRowB)
{
if (iColA == 0)
{
iPosA += 4;
}
else
{
iPosA -= 1;
}
if (iColB == 0)
{
iPosB += 4;
}
else
{
iPosB -= 1;
}
}
else
{
if (iRowA<iRowB)
{
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Practical No : 03
Aim :Write programs to implement the following Transposition Cipher Techniques:
1. Vernam Cipher
Input :
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
string[] template = { "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" };
Console.WriteLine("Enter a Word");
stringpt = Console.ReadLine();
int[] ptnum = new int[pt.Length];
for(inti=0;i<pt.Length;i++)
{
string s=pt.Substring(i,1);
for (int j=0;j<template.Length;j++)
{
if(template[j]==s)
{
ptnum[i]=j+1;
break;
}
}
}
Console.WriteLine("Enter the key(number for every character)");
int[] key = new int[ptnum.Length];
int[] add = new int[ptnum.Length];
for (inti = 0; i<ptnum.Length; i++)
{
key[i] = Convert.ToInt32(Console.ReadLine());
}
for (inti = 0; i<key.Length; i++)
{
add[i] = key[i] + ptnum[i];
if (add[i] > 26)
{
add[i] = add[i] - 26;
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Console.Write(template[add[i] - 1] + "");
}
Console.Read();
}
}
}
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Input :
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter plain text:");
String pt = Console.ReadLine();
pt = pt.ToLower();
string s1 = null, s2 = null;
for (inti = 0; i<pt.Length; i++)
{
if (i % 2 == 0) s1 = s1 + pt.Substring(i, 1);
else s2 = s2 + pt.Substring(i, 1);
}
Console.WriteLine("\nCipher Text:");
Console.WriteLine(s1 + s2);
Console.Read();
}
}
}
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Input :
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespacesimple_columnar
{
class Program
{
static void Main(string[] args)
{
}
//Console.WriteLine("\nCipher Text : ");
Console.Read();
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
}
}
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Practical No : 04
Aim :Write a program to implement RSA algorithm to perform encryption / decryption
of a given string.
Input ;
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespace ConsoleApplication6
{
class Program
{
static bool IsPrime(intiNumber)
{
intiCount;
int f = 0;
iCount = 2;
if (iNumber< 2)
return true;
while (iCount<= (iNumber / 2))
{
if (iNumber % iCount == 0)
{
f = 1;
break;
}
iCount++;
}
if (f == 0) return true;
return false;
}
staticint Multiply(int iNum1, int iNum2)
{
return iNum1 * iNum2;
}
staticintfind_E(intiNum)
{
intiCount = 2;
while (iCount<iNum)
{
if (iNum % iCount != 0)
returniCount;
iCount++;
}
return 0;
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("----KEYS---");
Console.WriteLine("Public Key:({0},{1})", e, n);
Console.WriteLine("Private Key:(" + d + "," + n + ")");
Console.WriteLine();
Console.WriteLine("----ENCRYPTION----");
Console.WriteLine("Enter the Number to Encrypt");
pt = Convert.ToInt32(Console.ReadLine());
ct = Find_Text(pt, e, n);
pt = Find_Text(ct, d, n);
Console.WriteLine("Cipher Text:" + ct);
Console.WriteLine("----Decryption---");
Console.WriteLine("Plain Text:" + pt);
}
else
{
Console.WriteLine("Error! Enter two prime numbers!");
}
Console.Read();
}
}
}
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Practical No : 05
Aim :Write a program to implement the Diffie-Hellman Key Agreement algorithm to
generate symmetric keys.
Input :
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceD_H_Algorithm
{
class Program
{
static void Main(string[] args)
{
int m1 = 1, m2 = 1, x, y, g, n;
Console.Write("Enter G:");
g = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter N:");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter x:");
x = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Y:");
y = Convert.ToInt32(Console.ReadLine());
for (inti = 0; i< x; i++)
{
m1 = m1 * g;
}
m1 = m1 % n;
for (inti = 0; i< y; i++)
{
m2 = m2 * g;
}
m2 = m2 % n;
Console.WriteLine("m1=\t" + m1 + "\n m2=\t" + m2);
k2 = m2 % n;
Console.WriteLine("k1=\t" + k1 + "\n k2=\t" + k2);
Console.ReadKey();
}
}
}
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Practical No : 06
Aim :Write program to encrypt and decrypt strings using
1. DES Algorithm
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax. crypto.SecretKey;
import javax.swing.JOptionPane;
public class DES
{
public static void main(String[] args)throws Exception
{
KeyGenerator kg=KeyGenerator.getInstance("DES");
SecretKey sk=kg.generateKey();
Cipher cp = Cipher.getInstance("DES");
cp.init(Cipher.ENCRYPT_MODE,sk);
String it = JOptionPane.showInputDialog("Give Input:");
byte[] encryp = cp.doFinal(it.getBytes());
cp.init(Cipher.DECRYPT_MODE,sk);
byte[] decryp = cp.doFinal(encryp);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted : " + new
String(encryp)+"\n"+"decrypted:"+new String(decryp));
System.exit(0);
}
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Output:
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
2. AES Algorithm
Input :
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax. crypto.SecretKey;
import javax.swing.JOptionPane;
public class AESdemo{
public static void main(String[] args) throws Exception{
KeyGenerator kg = KeyGenerator.getInstance("AES");
SecretKey sk=kg.generateKey();
Cipher cp=Cipher.getInstance("AES");
cp.init(Cipher.ENCRYPT_MODE,sk);
String it=JOptionPane.showInputDialog("Give Input: ");
byte[]encryp=cp.doFinal(it.getBytes());
cp.init(Cipher.DECRYPT_MODE,sk);
byte[]decryp=cp.doFinal(encryp);
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(),"encrypted:" + new
String(encryp)+"\n"+"decrypted:"+new String(decryp));
System.exit(0);
}
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Output:
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Practical No : 07
Aim :Write a program to implement the MD5 algorithm compute the message digest.
Input :
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Hash
{
public static void main(String[] args)
{
System.out.println("For null" + md5(""));
System.out.println("For simple text" + md5("This is my text"));
System.out.println("For simple numbers" + md5("12345"));
}
public static String md5(String input)
{
String md5 = null;
if(null== input)return null;
try
{
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(input.getBytes(),0,input.length());
md5 = new BigInteger(1,digest.digest()).toString(16);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
return md5;
}
}
Output :
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Practical No : 08
Aim :Write a program to calculate HMAC-SHA1 Signature
Input :
importjava.security.InvalidKeyException;
importjava.security.NoSuchAlgorithmException;
importjava.security.SignatureException;
importjava.util.Formatter;
importjavax.crypto.Mac;
importjavax.crypto.spec.SecretKeySpec;
returnformatter.toString();
}
InvalidKeyException
{
SecretKeySpecsigningKey = new SecretKeySpec
(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
returntoHexString(mac.doFinal(data.getBytes()));
}
System.out.println(hmac);
asserthmac.equals
("104152c5bfdca07bc633eebd46199f0255c9f49d");
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20
Output :