NetworkSecurity
NetworkSecurity
NetworkSecurity
CERTIFICATE
This is to certified that the experimental work as entered in this journal is as
per syllabus in B.Sc Computer Science for Network Security was done in
the Computer Science laboratory of K.J.Somaiya College of Science
andCommerce by the student
Bro/Sis: Sachin Sudam Ugale having Seat No.:2009858 of class T.Y.B.Sc
Computer Science Semester V during the academic year 2020-2021.
No.of Experiments completed 9 out of 9.
8 Practical 8: RSA 22
Code:
import java.io.*;
public class Caeser
{
static String encryption;
static String decryption;
static char ch;
else
{
ch=str.charAt(i);
decryption+=(char)(ch-3);
}
}
return decryption;
}
public static void main(String args[])throws IOException
{
String en;
Output:
Practical 2: Rail Fence Cipher
Algorithm:
Step 1: Accept plain text
Step 2: Accept No of Row
Step3: Letters of the plaintext are written downwards diagonally after reaching the bottom most
row we go upwards in traverse direction diagonally.
Step 4: Step 3 is repeated until the last letter of the plain text.
Step 5:After Completion of step 4 all the individual row values are combined and gives the
cipher text.
Code: import java.io.*;
class Rail
{
Rail()
{
try{
String msg="";
String str1="";
String str2="";
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a msg");
msg=br.readLine();
for(int i=0;i<msg.length();i++)
{
if(i%2==0)
{
str1=str1+msg.charAt(i);
}
else if(i%2==1)
{
str2=str2+msg.charAt(i);
}
}
System.out.println("Encrypted Text: " +str1+str2);
}
catch(Exception e)
{
}
}
public static void main(String args[])
{
new Rail();
}
}
Output:
Practical 3: Vernam Cipher
Algorithm:
Step1: Accept the plain text and the key with the same size as the plain text.
Step2: Assign a number to each character of the plain-text and the key according to alphabetical
order.
Step3: Add both the number (Corresponding plain-text character number and Key character
number).
Step4: Subtract the number from 26 if the added number is greater than 26, if it isn’t then leave
it.
Code:
import java.io.*;
class Vernam{
Vernam(){
try {
String msg,key,Cipher="";
int sum=0,num1=0,num2=0;
char pt,k;
alpha1[i]=(char)('A'+i);
}
for(int i=0;i<msg.length();i++){
pt=msg.charAt(i);
k=key.charAt(i);
for(int j=0;j<alpha1.length;j++){
if(pt==alpha1[j]){
num1=j;
}
if(k==alpha1[j]){
num2=j;
}
sum=num1+num2;
if(sum>25){
sum=sum%26;
}
}
Cipher+=alpha1[sum];
// System.out.println(alpha1[sum]);
}
System.out.println(Cipher);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
new Vernam();
}
Output:
Practical 4: Monoaplhabetic Cipher
Algorithm:
Step 1: Accept Plain text
Step 2: Plain text alphabet is mapped with alphabet of ciphertext key. For example A is mapped
to G and B is mapped to Z.
Step 3: Values of plain text are replaced by their mapped cipher text values and we get encrypted
text.
Step 4: By doing opposite of Step 2 and 3 we get the decrypted text.
Output:
Practical 5: Deffie-Hellman Key Exchange Algorithm
Algorithm:
Step 1: User A and User B have two large prime numbers public key P and G.
Step 2: User A uses Private Key X and User B uses Private Key Y.
Step 3: Using formula I=G^X mod P User A creates a new key I using his private key X.
Step 4: Using a similar formula User B creates new key J using his own private key Y i.e. the
G^X is replaced by G^Y.
Step 5: Users A and B exchange the new key they generated i.e. I and J.
Step 6: User A uses formula KX=J^X mod P and User B uses formula KY=I^Y mod P.
Step 7: If KX=KY user now have symmetric secret key to encrypt.
// Calculate A
A=g.modPow(x,n);
// Calculate B
B=g.modPow(y,n);
// Calculate key k1 and k2
k1=B.modPow(x,n);
k2=A.modPow(y,n);
if(k1.equals(k2))
{
System.out.println("Key 1 is:"+k1+"Key 2 is:" +k2);
System.out.println("Both keys are equal !!! Successfull");
}
}catch (Exception e) {
System.out.println(e);
}
}
public static void chkPrime(BigInteger n) {
try{
if(n.isProbablePrime(10))
{
System.out.println("Prime,Continue");
}
else
{
System.out.println("Not Prime");
System.out.println("Enter value:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BigInteger x=new BigInteger(br.readLine());
chkPrime(x);
}
}catch(Exception ae){
System.out.println(ae);
}
}
}
Output:
Practical 6: Simple Columnar Transposition
Algorithm:
Step 1:Write the plain text message row-by-row in grids of rectangle of a predefined size.
Step 2: Read the message column by column. However, it needs to be in the order of column
1,2,3,4,5,6 etc. It can be any random order such as 2, 6, 4, 3, 5, and 1.
Step 3: After this operation message obtained is a cipher text message.
Step 4: Simple columnar transposition technique simply arranges the plain text as a sequence of
rows of rectangle that is read in column randomly.
Step 5: Simple columnar transposition technique is also quite simple to break after trying some
permutation combinations of column orders to get original plain text. To make this encryption
more complex we can perform more than one round of transposition using the same technique.
Step 6: Let’s see an example. Suppose we have plain text
HELLO YOU ARE READING THIS ON MY BSC IT.
Step 7: If we perform Simple columnar transposition technique operation on this text in column
order of 2, 6, 4, 3, 5, and 1 then encrypted text will be
EUAHYYRGNILRISSLADIBOENOCHOETMT .
}
catch(Exception e)
{
}
}
public DES(SecretKey key)
{
try{
ecipher=Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE,key);
dcipher=Cipher.getInstance("DES");
dcipher.init(Cipher.DECRYPT_MODE,key);
}
catch(Exception e)
{
}
}
public String encrypt1(String str)
{
try
{
Base64.Encoder encoder = Base64.getEncoder();
byte[] utf8=str.getBytes("UTF8");
byte[] enc=ecipher.doFinal(utf8);
return encoder.encodeToString(enc);
}
catch(Exception e)
{
}
return null;
}
public String decrypt1(String str)
{
try
{
Base64.Decoder decoder = Base64.getDecoder();
byte[]dec =decoder.decode(str);
byte[] utf8=dcipher.doFinal(dec);
return new String(utf8,"UTF8");
}
catch(Exception e)
{
}
return null;
}
}
Output:
Practical 8: RSA Encryption
Algorithm:
Step 1: Choose two different large random prime numbers p and q
Step 2:Calculate n=p*q. n is the modulus for the public key and the private keys
Step 3: Calculate the totient: Ø(n)=(p-1)(q-1).
Step 4:Choose an integer e such that 1 < e < Ø(n), and e is co-prime to Ø(n) i.e.: e and Ø(n),
share no factors other than 1; gcd(e, Ø(n),) = 1. e is released as the public key exponent
Step 5: Compute d to satisfy the congruence relation de Ξ 1 (mod Ø(n)) i.e.: de=1+x Ø(n) for
some integer x. (Simply to say : Calculate d=(1+x Ø(n))/e to be an integer).
d is kept as the private key exponent
int bitLength=512;
int certainty=100;
r=new SecureRandom();
//step 2: n=p*q
n=p.multiply(q);
System.out.println("Prime no P is:"+p.intValue());
System.out.println("Prime no Q is:"+q.intValue());
System.out.println("N = P*Q is:"+n.intValue());
while(n1.gcd(e).intValue()>1 || e.compareTo(p1)!=-1)
e=e.add(new BigInteger("1"));
System.out.println("Public key is("+n.intValue()+","+e.intValue()+")");
Output:
Practical 9: Advanced Encryption Standard (AES)
Algorithm:
Step 1: Derive the set of round keys from the cipher key.
Step 2: Initialize the state array with the block data (plaintext).
Step 3:Add the initial round key to the starting state array.
Step 4: Perform nine rounds of state manipulation.
Step 5: Perform the tenth and final round of state manipulation.
Step 6: Copy the final state array out as the encrypted data (ciphertext).
}
catch(Exception e)
{
}
}
public AES(SecretKey key)
{
try{
ecipher=Cipher.getInstance("AES");
ecipher.init(Cipher.ENCRYPT_MODE,key);
dcipher=Cipher.getInstance("AES");
dcipher.init(Cipher.DECRYPT_MODE,key);
}
catch(Exception e)
{
}
}
public String encrypt1(String str)
{
try
{
Base64.Encoder encoder = Base64.getEncoder();
byte[] utf8=str.getBytes("UTF8");
byte[] enc=ecipher.doFinal(utf8);
return encoder.encodeToString(enc);
}
catch(Exception e)
{
}
return null;
}
public String decrypt1(String str)
{
try
{
Base64.Decoder decoder = Base64.getDecoder();
byte[]dec =decoder.decode(str);
byte[] utf8=dcipher.doFinal(dec);
return new String(utf8,"UTF8");
}
catch(Exception e)
{
}
return null;
}
}
Output: