Sunteți pe pagina 1din 30

SMT.

SUSHILADEVI DESHMUKH COLLEGE OF ARTS,


SCIENCE AND COMMERCE, Airoli, Navi Mumbai – 400 708

Date:__________

Certificate

This is to certify that Mr./Miss__________________________________ seat no ______ of

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

Curriculum of Degree of Bachelor of Science in Computer Science, University of Mumbai.

Signature of Internal Guide Signature of HOD

College Seal
T.Y.B.Sc C.S. Practicals SEM-V 2019-20

INDEX

Sr. No. Practical’s Date Sign.

1. Write programs to implement the following Substitution


Cipher Techniques:
1. Caesar Cipher.
2. Monoalphabetic Cipher.

2. Write programs to implement the following Substitution


Cipher Techniques:
1. Playfair Cipher.

3. Write programs to implement the following Transposition


Cipher Techniques:
1. Vernam Cipher.
2. Rail Fence Cipher.
3. Simple Columnar Technique.

4. Write a program to implement RSA algorithm to perform


encryption / decryption of a given string.

5. Write a program to implement the Diffie-Hellman Key


Agreement algorithm to generate symmetric keys.

6. .Write program to encrypt and decrypt strings using:


1. DES Algorithm.
2. AES Algorithm.

7. Write a program to implement the MD5 algorithm compute


the message digest.

8. Write a program to calculate HMAC-SHA1 Signature


T.Y.B.Sc C.S. Practicals SEM-V 2019-20

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

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);
returnsOutput;
}
}
}
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

2. Rail Fence Cipher

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

3. Simple Columnar Technique

Input :
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;

namespacesimple_columnar
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("Enter Plain Text : ");


string PT = Console.ReadLine();
string[] PTA=new string[PT.Length];
Console.WriteLine("Enter Number of steps : ");
int n=Convert.ToInt32(Console.ReadLine());
int[] c=new int[PT.Length];
for (inti = 0; i<PT.Length; i++)
{
PTA[i] = PT.Substring(i, 1);
}
string[] CTA = new string[PT.Length];
for (inti = 0; i< n; i++)
{
Console.WriteLine("\nEnter Numbers within range 0 to "+(PT.Length-1));
for (int j = 0; j <PT.Length; j++)
{
int x = Convert.ToInt32(Console.ReadLine());
while (x < 0 || x >PT.Length - 1)
{
Console.WriteLine("Enter Numbers within range 0 to " + (PT.Length - 1));
x = Convert.ToInt32(Console.ReadLine());
}
CTA[j] = PTA[x];
}
for (int j = 0; j <PT.Length; j++)
{
PTA[j] = CTA[j];
Console.Write(CTA[j]);
}

}
//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

staticintFind_D(int pie, int e)


{
int k = 1;
intdvalue = 0;
bool state = true;
while (state)
{
if ((k * pie + 1) % e == 0)
{
dvalue = (k * pie + 1) / e;
state = false;
}
++k;
}
returndvalue;
}
staticintFind_Text(int iNum1, int iNum2, int iNum3)
{
intiCount, t;
iCount = 1;
t = 1;
while (iCount<= iNum2)
{
t = t * iNum1;
t = t % iNum3;
iCount++;
}
return (t % iNum3);
}
static void Main(String[] args)
{
int p, q, n, d, e, pi, pt, ct;
//clrscr();
Console.WriteLine("------IMPLIMENTATION OF R.S.A ALGORITHM------");
Console.WriteLine("Enter a prime number:");
p = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter another prime number:");
q = Convert.ToInt32(Console.ReadLine());
if (IsPrime(p) &&IsPrime(q))
{
n = Multiply(p, q);
Console.WriteLine("/*Intermediate Calculation..");
Console.WriteLine("n:" + n);
pi = Multiply(p - 1, q - 1);
Console.WriteLine("pi:" + pi);
e = find_E(pi);
Console.WriteLine("e:" + e);
d = Find_D(pi, e);
Console.WriteLine("d:" + d);
Console.WriteLine("*/");
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);

int k1 = 1, k2 = 1, a = m1, b = m2;


m1 = m2 = 1;
for (inti = 0; i< x; i++)
{
m1 = m1 * b;
}
k1 = m1 % n;
for (inti = 0; i< y; i++)
{
m2 = m2 * a;
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20

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;

public class HmacSha1Signature {


private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";

private static String toHexString(byte[] bytes) {


Formatter formatter = new Formatter();

for (byte b : bytes) {


formatter.format("%02x", b);
}

returnformatter.toString();
}

public static String calculateRFC2104HMAC(String data, String key)


throwsSignatureException, NoSuchAlgorithmException,

InvalidKeyException
{
SecretKeySpecsigningKey = new SecretKeySpec

(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
returntoHexString(mac.doFinal(data.getBytes()));
}

public static void main(String[] args) throws Exception {


String hmac = calculateRFC2104HMAC("data", "key");

System.out.println(hmac);
asserthmac.equals

("104152c5bfdca07bc633eebd46199f0255c9f49d");
}
T.Y.B.Sc C.S. Practicals SEM-V 2019-20

Output :

S-ar putea să vă placă și