CNSlabmanual
CNSlabmanual
i. Ceaser cipher
ii. Playfair cipher
iii. Hill Cipher
iv. Vigenere cipher
i. Rail fence
ii. Row & Column Transformation
11
12 Defeating Malware
i. Building Trojans
ii. Rootkit Hunter
Ex. No : 1(a) Encryption and Decryption Using Ceaser Cipher
Date :
AIM:
To encrypt and decrypt the given message by using Ceaser Cipher
encryption algorithm.
ALGORITHMS:
PROGRAM:
CaesarCipher.java
import java.util.*;
class caesarCipher {
public static String encode(String enc, int offset) {
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder();
for (char i : enc.toCharArray()) {
if (Character.isLetter(i)) {
if (Character.isUpperCase(i)) {
encoded.append((char) ('A' + (i - 'A' + offset) % 26));
} else {
encoded.append((char) ('a' + (i - 'a' + offset) % 26));
}
} else {
encoded.append(i);
}
} return encoded.toString();
}
public static String decode(String enc, int offset) {
return encode(enc, 26 - offset);
}
public static void main(String[] args) throws java.lang.Exception {
Scanner x=new Scanner(System.in);
System.out.println(“Enter the Plain Text:”);
String msg =x.nextLine();
System.out.println("Simulating Caesar Cipher\n------------------------");
System.out.println("Input : " + msg);
System.out.printf("Encrypted Message : ");
System.out.println(caesarCipher.encode(msg, 3));
System.out.printf("Decrypted Message : ");
System.out.println(caesarCipher.decode(caesarCipher.encode(msg, 3), 3));
}
}
OUTPUT:
RESULT:
Thus the program for ceaser cipher encryption and decryption algorithm has
been implemented and the output verified successfully.
Ex.No :1(a) Playfair Cipher
Date :
AIM:
To implement a program to encrypt a plain text and decrypt a cipher text
using play fair Cipher substitution technique.
ALGORITHM:
1. To encrypt a message, one would break the message into digrams (groups of
2 letters)
2. For example, "HelloWorld" becomes "HE LL OW OR LD".
3. These digrams will be substituted using the key table.
4. Since encryption requires pairs of letters, messages with an odd number of
characters usually append an uncommon letter, such as "X", to complete the
final digram.
5. The two letters of the digram are considered opposite corners of a rectangle
in the key table. To perform the substitution, apply the following 4 rules, in
order, to each pair of letters in the plaintext:
PROGRAM:
playfairCipher.java
import java.awt.Point;
import java.util.*;
class playfairCipher {
private static char[][] charTable;
private static Point[] positions;
private static String prepareText(String s, boolean chgJtoI) {
s = s.toUpperCase().replaceAll("[^A-Z]", "");
return chgJtoI ? s.replace("J", "I") : s.replace("Q", "");
}
private static void createTbl(String key, boolean chgJtoI) {
charTable = new char[5][5];
positions = new Point[26];
String s = prepareText(key + "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
chgJtoI);
int len = s.length();
for (int i = 0, k = 0; i < len; i++) {
char c = s.charAt(i); if (positions[c - 'A'] == null) {
charTable[k / 5][k % 5] = c;
positions[c - 'A'] = new Point(k % 5, k / 5);
k++;
}
}
}
private static String codec(StringBuilder txt, int dir) {
int len = txt.length();
for (int i = 0; i < len; i += 2) {
char a = txt.charAt(i);
char b = txt.charAt(i + 1);
int row1 = positions[a - 'A'].y;
int row2 = positions[b - 'A'].y;
int col1 = positions[a - 'A'].x;
int col2 = positions[b - 'A'].x;
if (row1 == row2) {
col1 = (col1 + dir) % 5;
col2 = (col2 + dir) % 5;
} else if (col1 == col2) {
row1 = (row1 + dir) % 5;
row2 = (row2 + dir) % 5;
} else {
int tmp = col1;
col1 = col2;
col2 = tmp;
}
txt.setCharAt(i, charTable[row1][col1]);
txt.setCharAt(i + 1, charTable[row2][col2]);
}
return txt.toString();
}
private static String encode(String s) {
StringBuilder sb = new StringBuilder(s);
for (int i = 0; i < sb.length(); i += 2) {
if (i == sb.length() - 1) { sb.append(sb.length() % 2 == 1 ? 'X' : "");
} else if (sb.charAt(i) == sb.charAt(i + 1)) {
sb.insert(i + 1, 'X');
}
}
return codec(sb, 1);
}
private static String decode(String s) {
return codec(new StringBuilder(s), 4);
}
public static void main(String[] args) throws java.lang.Exception {
Scanner x=new Scanner(System.in);
System.out.println(“Enter the Plain Text:”);
String txt=x.nextLine();
System.out.println(“Enter the key:”);
String key=x.nextLine();
boolean chgJtoI = true;
createTbl(key, chgJtoI);
String enc = encode(prepareText(txt, chgJtoI));
System.out.println("Simulating Playfair Cipher\n----------------------");
System.out.println("Input Message : " + txt);
System.out.println("Encrypted Message : " + enc);
System.out.println("Decrypted Message : " + decode(enc));
}
}
OUTPUT:
Simulating Playfair Cipher
----------------------
Enter the Plain Text:Security Lab
Enter the Key:CSE
Encrypted Message : EABPUGYANSEZ
Decrypted Message : SECURITYLABX
RESULT:
Thus the program for playfair cipher encryption and decryption algorithm has been
implemented and the output verified successfully.
Ex. No1(c) Hill Cipher
Date :
AIM:
To implement a program to encrypt and decrypt using the Hill cipher
substitution technique
ALGORITHM:
1. In the Hill cipher Each letter is represented by a number modulo 26.
2. To encrypt a message, each block of n letters is multiplied by an invertible n
x n matrix, again modulus 26.
3. To decrypt the message, each block is multiplied by the inverse of the matrix
used for encryption.
4. The matrix used for encryption is the cipher key, and it should be chosen
randomly from the set of invertible n × n matrices (modulo 26).
5. The cipher can, be adapted to an alphabet with any number of letters.
6. All arithmetic just needs to be done modulo the number of letters instead of
modulo 26.
PROGRAM:
HillCipher.java
import java.util.*;
import java.io.*;
public class NewClass {
private static int inv(int d)
{
int i=2,m=0;
while(i<=26)
{
if((d*i)%26==1)
{
m=i; break;
}
i++;
}
return m;
}
private static int[][] adjk(int[][]b){
int [][]a=new int [3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=((b[(j+1)%3][(i+1)%3]*b[(j+2)%3][(i+2)%3])-
(b[(j+2)%3][(i+1)%3]*b[(j+1)%3][(i+2)%3]))%26;
}
}
return a;
}
private static int[] MUL_MATRIX(int []d,int[][]a,int len)
{
int []h=new int[30];
int []r=d;
int []q=new int [30];
int j=len;
int io=0;
int k=0;
while(len>io)
{
int x=0;
for(int i=0;i<3;i++)
{
for(int v=0;v<3;v++)
{
x+=r[v+k]*a[v][i];
}
q[i+k]=x;
x=0;
}
k=k+3;
io++;
for(int i=0;i<len*3;i++)
{
h[i] = (q[i]%26);
}
return h;
t=0;
System.out.println(j);
while(KEY.length()!=t)
{
c=alphabet.indexOf(j.charAt(t));
p[t]=c;
t++;
}
int sub=0;
for(int i=0;i<3;i++){
for(int v=0;v<3;v++)
{
a[i][v]= p[sub];
sub++;
}
}
for(int i=0;i<3;i++){
for(int v=0;v<3;v++)
{
System.out.print(" "+a[i][v]+" ");
}
System.out.println();
}
we=plain.toUpperCase();
int value=plain.length();
int mod=value%3;
if(mod==1)
{
suc=we.concat("ZZ");
}
else if(mod==2)
{
suc=we.concat("Z");
}
else
{
suc=we.concat("");
}
System.out.println("*****************");
int len=suc.length()/3;
t=0;
System.out.println("PLAINTEXT VALUE:");
while(suc.length()!=t)
{
c=alphabet.indexOf(suc.charAt(t));
r[t]=c;
t++;
}
for(int v=0;v<suc.length();v++)
{
System.out.print(r[v]+" ");
}
h=MUL_MATRIX(r,a,len);
t=0;
int rs=plain.length();
System.out.println("ENCRYPTION:==>");
while(rs>t)
{
l=alphabet.charAt(h[t]);
System.out.println(l);
t++;
}
int det1=(a[0][0]*((a[1][1]*a[2][2])-(a[2][1]*a[1][2])));
int det2=(a[0][1]*((a[1][0]*a[2][2])-(a[2][0]*a[1][2])));
int det3=(a[0][2]*((a[1][0]*a[2][1])-(a[2][0]*a[1][1])));
int det=det1-det2+det3;
int detv=det%26;
if (detv<0)
{
detv=detv+26;
}
int idetv=inv(detv);
System.out.println("DETERMINANT:===>"+idetv);
int [][]ad=new int[3][3];
ad=adjk(a);
for(int i=0;i<3;i++)
{
for(int n=0;n<3;n++)
{
if(ad[i][n]<0)
{
ad[i][n]+=26;
}
}
}
System.out.println("ADJACENT MATRIX: =>");
for(int f=0;f<3;f++){
for(int v=0;v<3;v++)
{
System.out.print(" "+ad[f][v]+" ");
}
System.out.println();
}
for(int i=0;i<3;i++)
{
for(int n=0;n<3;n++)
{
lp[i][n]=(ad[i][n]*idetv)%26;
}
}
int []pt=new int[30];
pt=MUL_MATRIX(h,lp,len);
t=0;
System.out.println("DECRYPTION:==>");
while(rs>t)
{
l=alphabet.charAt(pt[t]);
System.out.print(l);
t++;
}
}}
OUTPUT:
Simulating Hill Cipher
-------------------------------
Enter the Key : RRFYSVCCT
Enter the PlainText:Paymoremoney
Encrypted Message : RRLMWBKASVON
Decrypted Message : PAYMOREMONEY
RESULT:
Thus the program for hill cipher encryption and decryption algorithm has
been implemented and the output verified successfully.
Ex. No : 1(d) Vigenere Cipher
Date :
AIM:
To implement a program for encryption and decryption using vigenere
cipher substitution technique
ALGORITHM:
PROGRAM:
vigenereCipher.java
Import java.util.*;
public class vigenereCipher {
static String encode(String text, final String key) {
String res = "";
text = text.toUpperCase();
for (int i = 0, j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'A' || c > 'Z') {
continue;
}
res += (char) ((c + key.charAt(j) - 2 * 'A') % 26 + 'A');
j = ++j % key.length();
}
return res;
}
static String decode(String text, final String key) {
String res = "";
text = text.toUpperCase(); for (int i = 0, j = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (c < 'A' || c > 'Z') {
continue;
}
res += (char) ((c - key.charAt(j) + 26) % 26 + 'A');
j = ++j % key.length();
}
return res;
}
public static void main(String[] args) throws java.lang.Exception {
Scanner x=new Scanner(System.in);
System.out.println(“Enter the Plain Text:”);
String txt=x.nextLine();
System.out.println(“Enter the key:”);
String key=x.nextLine();
System.out.println("Simulating Vigenere Cipher\n------------------------");
System.out.println("Input Message : " + msg);
String enc = encode(msg, key);
System.out.println("Encrypted Message : " + enc);
System.out.println("Decrypted Message : " + decode(enc, key));
}
}
OUTPUT:
Simulating Vigenere Cipher
------------------------
Enter the Plain Text:SecurityLaboratory
Enter the Key:Vigenerecipher
Encrypted Message : NMIYEMKCNIQVVROWXC
Decrypted Message : SECURITYLABORATORY
RESULT:
Thus the program for vigenere cipher encryption and decryption algorithm
has been implemented and the output verified successfully.
Ex. No : 2(a) Rail Fence Cipher Transposition Technique
Date :
AIM:
ALGORITHM:
1. In the rail fence cipher, the plaintext is written downwards and diagonally on
successive "rails" of an imaginary fence, then moving up when we reach the
bottom rail.
2. When we reach the top rail, the message is written downwards again until
the whole plaintext is written out.
3. The message is then read off in rows.
PROGRAM:
railFenceCipher.java
import java.util.*;
public class RailFence {
public static void main(String arss[])
{
String al="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Scanner x=new Scanner(System.in);
System.out.println("enter the plain text");
String plain=x.nextLine();
String p=plain.toUpperCase();
int len=plain.length();
System.out.println("enter the depth");
int depth=x.nextInt();
char a[][]=new char[depth][len];
int j=0;
int i=0;
int flag=0;
while(j<len)
{
a[i][j]=p.charAt(j);
if(i==0)
{
i++;
flag=0;
}
else if(i==depth-1)
{
i--;
flag=1;
}
else if((i>0||i<depth-1)&&flag==0)
{
flag=0;
i++;
}
else
{
flag=1;
i--;
}
j++;
}
System.out.println("RAILFENCE: =>");
for(int f=0;f<depth;f++){
for(int v=0;v<len;v++)
{
String s="";
for(int f=0;f<depth;f++){
for(int v=0;v<len;v++)
{
s=s+a[f][v];
}
System.out.println();
}
System.out.println(s);
}
OUTPUT:
Simulating Railfence Cipher
----------------------------------------
Enter the PlainText: Anna University, Chennai
Enter the depth:3
Encrypted Message : An nvriy hnanaUiest,Ceni
Decrypted Message : Anna University, Chennai
RESULT:
Thus the java program for Rail Fence Transposition Technique has been
implemented and the output verified successfully.
Ex. No : 2(b) Row and Column Transformation Technique
Date :
AIM:
To implement a program for encryption and decryption by using row and
column transformation technique.
ALGORITHM:
1. Consider the plain text hello world, and let us apply the simple columnar
transposition technique as shown below
h e l
l o w
o r l
d
2. The plain text characters are placed horizontally and the cipher text is
created with vertical format as: holewdlo lr.
3. Now, the receiver has to use the same table to decrypt the cipher text to
plain text.
PROGRAM:
TransCipher.java
import java.util.*;
public class rowColumntranS {
{
Scanner s=new Scanner(System.in);
String mes="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("enter the value of rows");
int a=s.nextInt();
System.out.println("enter the value of columns");
int b=s.nextInt();
int arr [][]=new int [a][b];
int brr [][]=new int [a][b];
System.out.println("PLAINTEXT(the length of the plain text should be less than )"+a*b);
String plain=s.next();
int len=plain.length();
System.out.println(len);
int ba=a*b;
if(len<ba)
{
int y=ba-len;
plain=plain+mes.substring(26-y,26);
}
plain=plain.toUpperCase();
System.out.println(plain);
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
int o=mes.indexOf(plain.charAt(((i*b)+j)));
arr[i][j]=o;
}
}
System.out.println("row column matrix");
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
System.out.println();
}
System.out.println("enter the key from value 0 to"+(b-1));
int[] key=new int [40];
for(int i=0;i<b;i++)
{
key[i]=s.nextInt();
}
System.out.println("encrypted message");
String temp="";
for(int i=0;i<b;i++)
{
for(int j=0;j<a;j++)
{
char d=mes.charAt(arr[j][key[i]]);
temp+=d;
}}
System.out.println(temp);
// System.out.println("decryption");
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
int o=mes.indexOf(temp.charAt(((i*b)+j)));
brr[i][j]=o;
}
}
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
System.out.println();
}
}
}
OUTPUT:
Enter the plain text
Security Lab
SecurityLab
Sreictuy
RESULT:
Thus the java program for Row and Column Transposition Technique has
been implemented and the output verified successfully.
Ex. No : 3 Data Encryption Standard (DES) Algorithm
Date : (User Message Encryption )
AIM:
To use Data Encryption Standard (DES) Algorithm for a practical
application like User Message Encryption.
ALGORITHM:
PROGRAM:
DES.java
import java.security.InvalidKeyException;
import java.util.*;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class DES
{
public static void main(String[] argv) {
try{
System.out.println("Message Encryption Using DES Algorithm\n-------");
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher; desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] text = "Secret Information ".getBytes();
System.out.println("Message [Byte Format] : " + text);
System.out.println("Message : " + new String(text));
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("Encrypted Message: " + textEncrypted);
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("Decrypted Message: " + new
String(textDecrypted));
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IllegalBlockSizeException e){
e.printStackTrace();
}catch(BadPaddingException e){
e.printStackTrace();
}
}
}
OUTPUT:
Message Encryption Using DES Algorithm
------------------------------------------------------
Message [Byte Format] : [B@4dcbadb4
Message : Secret Information
Encrypted Message: [B@504bae78
Decrypted Message: Secret Information
RESULT:
Thus the java program for DES Algorithm has been implemented and the
output verified successfully.
Ex. No : 4 Advanced Encryption Standard (DES) Algorithm
Date : ( URL Encryption )
AIM:
To use Advanced Encryption Standard (AES) Algorithm for a practical
application like URL Encryption.
ALGORITHM:
PROGRAM:
AES.java
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class AES {
private static SecretKeySpec secretKey;
private static byte[] key;
public static void setKey(String myKey) {
MessageDigest sha = null;
try {
key = myKey.getBytes("UTF-8");
sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
secretKey = new SecretKeySpec(key, "AES");
} catch (NoSuchAlgorithmException e) { e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public static String encrypt(String strToEncrypt, String secret) {
try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return
Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF -8")));
} catch (Exception e) {
System.out.println("Error while encrypting: " + e.toString());
}
return null;
}
public static String decrypt(String strToDecrypt, String secret) {
try {
setKey(secret);
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return new
String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
} catch (Exception e) {
System.out.println("Error while decrypting: " + e.toString());
}
return null;
}
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
System.out.println(“Enter the Plain Text:”);
String txt=x.nextLine();
System.out.println(“Enter the key:”);
String encryptedString = AES.encrypt(originalString, secretKey);
String decryptedString = AES.decrypt(encryptedString, secretKey); System.out.println("URL
Encryption Using AES Algorithm\n------------");
System.out.println("Original URL : " + originalString);
System.out.println("Encrypted URL : " + encryptedString);
System.out.println("Decrypted URL : " + decryptedString);
}
}
OUTPUT:
URL Encryption Using AES Algorithm
-------------------------------------------------
Enter the Plain Text: www.annauniv.edu
Enter the Key: annaUniversity
Encrypted URL : vibpFJW6Cvs5Y+L7t4N6YWWe07+JzS1d3CU2h3mEvEg=
Decrypted URL : www.annauniv.edu
RESULT:
Thus the java program for AES Algorithm has been implemented for URL
Encryption and the output verified successfully.
Ex. No : 5 RSA Algorithm
Date :
AIM:
To implement RSA (Rivest–Shamir–Adleman) algorithm by using HTML
and Javascript.
ALGORITHM:
RESULT:
Thus the RSA algorithm has been implemented using HTML & CSS and the
output has been verified successfully.
Ex. No : 6 Diffie-Hellman key exchange algorithm
Date :
AIM:
To implement the Diffie-Hellman Key Exchange algorithm for a given
problem .
ALGORITHM:
1. Alice and Bob publicly agree to use a modulus p = 23 and base g = 5 (which
is a primitive root modulo 23).
2. Alice chooses a secret integer a = 4, then sends Bob A = ga mod p
o A = 54 mod 23 = 4
3. Bob chooses a secret integer b = 3, then sends Alice B = gb mod p
o B = 53 mod 23 = 10
4. Alice computes s = Ba mod p
o s = 104 mod 23 = 18
5. Bob computes s = Ab mod p
o s = 43 mod 23 = 18
7. Alice and Bob now share a secret (the number 18).
8.
PROGRAM:
DiffieHellman.java
import java.util.*;
class DiffieHellman {
public static void main(String args[]) {
Scanner k=new Scanner(System.in);
System.out.println(“Enter the Prime number”);
int p = k.nextInt(); /* publicly known (prime number) */
System.out.println(“Enter the Primitive root”);
int g = k.nextInt(); /* publicly known (primitive root) */
System.out.println(“Enter the Alice secret key”);
int x = k.nextInt(); /* only Alice knows this secret */
System.out.println(“Enter the Bob secret key”);
int y = k.nextInt(); /* only Bob knows this secret */
double aliceSends = (Math.pow(g, x)) % p;
double bobComputes = (Math.pow(aliceSends, y)) % p;
double bobSends = (Math.pow(g, y)) % p;
double aliceComputes = (Math.pow(bobSends, x)) % p;
double sharedSecret = (Math.pow(g, (x * y))) % p;
System.out.println("simulation of Diffie-Hellman key exchange algorithm\n--
-------------------------------------------");
System.out.println("Alice Sends : " + aliceSends);
System.out.println("Bob Computes : " + bobComputes);
System.out.println("Bob Sends : " + bobSends); System.out.println("Alice Computes : " +
aliceComputes);
System.out.println("Shared Secret : " + sharedSecret);
/* shared secrets should match and equality is transitive */
if ((aliceComputes == sharedSecret) && (aliceComputes == bobComputes))
System.out.println("Success: Shared Secrets Matches! " + sharedSecret);
else
System.out.println("Error: Shared Secrets does not Match");
}
}
OUTPUT:
simulation of Diffie-Hellman key exchange algorithm
-----------------------------------------------------------------
Enter the Prime number 23
Enter the Primitive root 5
Enter the Alice secret key 4
Enter the Bob secret key 3
Alice Sends : 4.0
Bob Computes : 18.0
Bob Sends : 10.0
Alice Computes : 18.0
Shared Secret : 18.0
Success: Shared Secrets Matches! 18.0
RESULT:
Thus the Diffie-Hellman key exchange algorithm has been implemented
using Java Program and the output has been verified successfully.
Ex. No : 7 SHA-1 Algorithm
Date :
AIM:
To Calculate the message digest of a text using the SHA-1 algorithm.
ALGORITHM:
PROGRAM:
sha1.java
import java.util.*;
import java.security.*;
public class sha1 {
public static void main(String[] a) {
try {
Scanner s=new Scanner(System.in);
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Message digest object info:\n-----------------");
System.out.println("Algorithm=" + md.getAlgorithm());
System.out.println("Provider=" + md.getProvider());
System.out.println("ToString=" + md.toString());
String input = "";
md.update(input.getBytes());
byte[] output = md.digest();
System.out.println();
System.out.println("SHA1(\"" + input + "\")=" + bytesToHex(output));
input = “abc;
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\"" + input + "\")=" + bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes()); output = md.digest();
System.out.println();
System.out.println("SHA1(\"" + input + "\")=" + bytesToHex(output));
System.out.println();
} catch (Exception e) {
System.out.println("Exception:" + e);
}
}
private static String bytesToHex(byte[] b) {
char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
StringBuffer buf = new StringBuffer();
for (byte aB : b) {
buf.append(hexDigit[(aB >> 4) & 0x0f]);
buf.append(hexDigit[aB & 0x0f]);
}
return buf.toString();
}
}
OUTPUT:
Message digest object info:
-------------------------------------
Algorithm=SHA1
Provider=SUN version 12
ToString=SHA1 Message Digest from SUN, <initialized>
SHA1("")=DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
SHA1("abc")=A9993E364706816ABA3E25717850C26C9CD0D89D
SHA1("abcdefghijklmnopqrstuvwxyz")=32D10C7B8CF96570CA04CE37F2A19
D84240D3A89
RESULT:
Thus the Secure Hash Algorithm (SHA-1) has been implemented and the
output has been verified successfully.
Ex. No : 8 Digital Signature Standard
Date :
AIM:
To implement the SIGNATURE SCHEME - Digital Signature Standard.
ALGORITHM:
PROGRAM:
import java.util.*;
import java.math.BigInteger;
class dsaAlg {
final static BigInteger one = new BigInteger("1");
final static BigInteger zero = new BigInteger("0"); /* incrementally tries for next prime */
public static BigInteger getNextPrime(String ans) {
BigInteger test = new BigInteger(ans);
while (!test.isProbablePrime(99))
{
test = test.add(one);
}
return test;
} /* finds largest prime factor of n */
public static BigInteger findQ(BigInteger n)
{
BigInteger start = new BigInteger("2");
while (!n.isProbablePrime(99))
{
while (!((n.mod(start)).equals(zero)))
{
start = start.add(one);
}
68 n = n.divide(start);
}
return n;
} /* finds a generator mod p */
public static BigInteger getGen(BigInteger p, BigInteger q, Random r)
{
BigInteger h = new BigInteger(p.bitLength(), r);
h = h.mod(p);
return h.modPow((p.subtract(one)).divide(q), p);
}
public static void main (String[] args) throws java.lang.Exception {
Random randObj = new Random(); /* establish the global public key components */
BigInteger p = getNextPrime("10600"); /* approximate prime */
BigInteger q = findQ(p.subtract(one));
BigInteger g = getGen(p,q,randObj); /*publickeycomponents*/
System.out.println("Digital Signature Algorithm");
System.out.println("global public key components are:");
System.out.println("p is: " + p);
System.out.println("q is: " + q);
System.out.println("g is: " + g); /* find the private key */
BigInteger x = new BigInteger(q.bitLength(), randObj);
x = x.mod(q); /* corresponding public key */
BigInteger y = g.modPow(x,p); /* random value message */
BigInteger k = new BigInteger(q.bitLength(), randObj);
k = k.mod(q); /* randomly generated hash value and digital signature */
BigInteger r = (g.modPow(k,p)).mod(q);
BigInteger hashVal = new BigInteger(p.bitLength(), randObj);
BigInteger kInv = k.modInverse(q);
BigInteger s = kInv.multiply(hashVal.add(x.multiply(r)));
s = s.mod(q); 69 /* secret information */
System.out.println("secret information are:");
System.out.println("x (private) is: " + x);
System.out.println("k (secret) is: " + k);
System.out.println("y (public) is: " + y);
System.out.println("h (rndhash) is: " + hashVal);
System.out.println("Generating digital signature:");
System.out.println("r is : " + r);
System.out.println("s is : " + s); /*verify the digital signature */
BigInteger w = s.modInverse(q);
BigInteger u1 = (hashVal.multiply(w)).mod(q);
BigInteger u2 = (r.multiply(w)).mod(q);
BigInteger v = (g.modPow(u1,p)).multiply(y.modPow(u2,p));
v = (v.mod(p)).mod(q);
System.out.println("verifying digital signature (checkpoints):");
System.out.println("w is : " + w);
System.out.println("u1 is : " + u1);
System.out.println("u2 is : " + u2);
System.out.println("v is : " + v);
if (v.equals(r)) {
System.out.println("success: digital signature is verified! " + r);
}
else {
System.out.println("error: incorrect digital signature");
}}}
OUTPUT:
RESULT:
Thus the Digital Signature Standard Signature Scheme has been
implemented and the output has been verified successfully.
Ex. No : 9 Demonstration of Intrusion Detection System(IDS)
Date :
AIM:
To demonstrate Intrusion Detection System (IDS) using Snort software tool.
AIM:
To download the N-Stalker Vulnerability Assessment Tool and exploring the
features.
EXPLORING N-STALKER:
N-Stalker Web Application Security Scanner is a Web security assessment tool.
It incorporates with a well-known N-Stealth HTTP Security Scanner and 35,000
Web attack signature database.
This tool also comes in both free and paid version.
Before scanning the target, go to “License Manager” tab, perform the update.
Once update, you will note the status as up to date.
You need to download and install N-Stalker from www.nstalker.com.
1. Start N-Stalker from a Windows computer. The program is installed under
Start ➪ Programs ➪ N-Stalker ➪ N-Stalker Free Edition.
2. Enter a host address or a range of addresses to scan.
3. Click Start Scan.
4. After the scan completes, the N-Stalker Report Manager will prompt
5. you to select a format for the resulting report as choose Generate HTML.
6. Review the HTML report for vulnerabilities.
Now goto “Scan Session”, enter the target URL.
In scan policy, you can select from the four options,
Manual test which will crawl the website and will be waiting for manual
attacks.
full xss assessment
owasp policy
Web server infrastructure analysis.
Once, the option has been selected, next step is “Optimize settings” which will
crawl the whole website for further analysis.
In review option, you can get all the information like host information,
technologies used, policy name, etc.Once done, start the session and start the scan.
The scanner will crawl the whole website and will show the scripts, broken pages,
hidden fields, information leakage, web forms related information which helps to
analyze further.
Once the scan is completed, the NStalker scanner will show details like severity
level, vulnerability class, why is it an issue, the fix for the issue and the URL
which is vulnerable to the particular vulnerability?
RESULT:
Thus the N-Stalker Vulnerability Assessment tool has been downloaded,
installed and the features has been explored by using a vulnerable website.
Ex. No : 11(a) Defeating Malware - Building Trojans
Date :
AIM:
To build a Trojan and know the harmness of the trojan malwares in a
computer system.
PROCEDURE:
TROJAN:
In computing, a Trojan horse,or trojan, is any malware which misleads users
of its true intent.
Trojans are generally spread by some form of social engineering, for
example where a user is duped into executing an email attachment disguised
to appear not suspicious, (e.g., a routine form to be filled in), or by clicking
on some fake advertisement on social media or anywhere else.
Although their payload can be anything, many modern forms act as a
backdoor, contacting a controller which can then have unauthorized access
to the affected computer.
Trojans may allow an attacker to access users' personal information such as
banking information, passwords, or personal identity.
Example: Ransomware attacks are often carried out using a trojan.
CODE:
Trojan.bat
@echo off
:x
start mspaint
start notepad
start cmd
start explorer
start control
start calc
goto x
OUTPUT
(MS-Paint, Notepad, Command Prompt, Explorer will open infinitely)
RESULT:
Thus a trojan has been built and the harmness of the trojan viruses has been
explored.
Ex. No : 11(b) Defeating Malware - Rootkit hunter
Date :
AIM:
To install a rootkit hunter and find the malwares in a computer.
ROOTKIT HUNTER:
Step 1
Visit GMER's website (see Resources) and download the GMER executable.
Click the "Download EXE" button to download the program with a random file
name, as some rootkits will close “gmer.exe” before you can open it.
Step 2
Step 3
When the program completes its scan, select any program or file listed in red.
Right-click it and select "Delete."
If the red item is a service, it may be protected. Right-click the service and select
"Disable." Reboot your computer and run the scan again, this time selecting
"Delete" when that service is detected.
When your computer is free of Rootkits, close the program and restart your PC.
RESULT:
In this experiment a rootkit hunter software tool has been installed and the
rootkits have been detected